<?php

namespace App\Repository\Category;

use App\Entity\Category;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

final class DoctrineCategoryRepository extends ServiceEntityRepository implements CategoryRepositoryInterface
{
    public function __construct(ManagerRegistry $registry)
    {
        // The second parameter is the Entity this Repository uses
        parent::__construct($registry, Category::class);
    }

    /**
     * @param int $categoryId
     *
     * @return Category
     */
    public function findById(int $categoryId): ?Category
    {
        return $this->findOneBy(['id' => $categoryId]);
    }

    /**
     * @param Category $category
     *
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function save(Category $category): void
    {
        $this->_em->persist($category);
        $this->_em->flush();
    }

    /**
     * @param Category $category
     *
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function delete(Category $category): void
    {
        $this->_em->remove($category);
        $this->_em->flush();
    }

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