<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\ApiPublic\Controller;

use KupShop\GraphQLBundle\ApiPublic\Types\Content\Fragment\Fragment;
use KupShop\GraphQLBundle\ApiPublic\Util\FragmentUtil;
use KupShop\GraphQLBundle\Exception\GraphQLValidationException;
use TheCodingMachine\GraphQLite\Annotations\Query;

class FragmentController
{
    private FragmentUtil $fragmentUtil;

    public function __construct(FragmentUtil $fragmentUtil)
    {
        $this->fragmentUtil = $fragmentUtil;
    }

    /**
     * Získání fragmentu dle kódu nebo id.
     *
     * @param string|null $code - kód fragmentu
     * @param int|null    $id   - id fragmentu
     *
     * @Query()
     */
    public function fragment(?string $code = null, ?int $id = null): ?Fragment
    {
        if (empty($code) && empty($id)) {
            throw new GraphQLValidationException('Parameter "code" or "id" is required!');
        }

        return $this->fragmentUtil->getFragment($code, $id);
    }
}
