<?php

namespace ServiceCore\Application\Query;

use ServiceCore\Application\Exception\EntityNotFound;
use ServiceCore\Domain\Repository\QueryRepositoryInterface;

/**
 * Class AbstractByIdQueryHandler
 *
 * @package ServiceCore\Application\Query
 */
abstract class AbstractByIdQueryHandler implements QueryHandlerInterface
{
    /**
     * @var QueryContextInterface
     */
    protected $context;

    /**
     * AbstractByIdQueryHandler constructor.
     *
     * @param QueryContextInterface $context
     */
    public function __construct(QueryContextInterface $context)
    {
        $this->context = $context;
    }

    /**
     * @param QueryInterface $query
     *
     * @return array
     *
     * @throws EntityNotFound
     */
    public function execute(QueryInterface $query): array
    {
        $data = $this->getRepository()->getById($query->getId());
        if (!$data) {
            throw new EntityNotFound();
        }

        return $data;
    }

    abstract function getRepository(): QueryRepositoryInterface;
}