<?php

declare(strict_types=1);

namespace App\Service\Domain;

use App\Entity\Domain\MajesticTopic;
use Doctrine\ORM\EntityNotFoundException;

final class MajesticTopicService
{
    /**
     * @var MajesticTopicRepositoryInterface
     */
    private $repository;

    /**
     * MajesticTopicService constructor.
     *
     * @param MajesticTopicRepositoryInterface $repository
     */
    public function __construct(MajesticTopicRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }

    /**
     * @param int $id
     *
     * @return MajesticTopic
     *
     * @throws EntityNotFoundException
     */
    public function get(int $id): MajesticTopic
    {
        $majesticTopic = $this->repository->findById($id);
        if (!$majesticTopic) {
            throw new EntityNotFoundException('MajesticTopic with id '.$id.' does not exist!');
        }

        return $majesticTopic;
    }

    /**
     * @return array|null
     */
    public function getAll(): ?array
    {
        return $this->repository->findAll();
    }

    /**
     * @param MajesticTopic $majesticTopic
     *
     * @return MajesticTopic
     */
    public function save(MajesticTopic $majesticTopic): MajesticTopic
    {
        $this->repository->save($majesticTopic);

        return $majesticTopic;
    }

    /**
     * @param MajesticTopic $majesticTopic
     */
    public function delete(MajesticTopic $majesticTopic): void
    {
        $this->repository->delete($majesticTopic);
    }

    public function getByTitle(string $title)
    {
        return $this->repository->findOneBy(['title' => $title]);
    }

    public function search(string $query)
    {
        return $this->repository->search($query);
    }
}
