<?php

declare(strict_types=1);

namespace App\Module\Sites\Infrastructure\Doctrine;

use App\Module\Sites\Domain\Entity\Site;
use App\Module\Sites\Domain\Repository\SiteRepositoryInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<Site>
 */
class DoctrineSiteRepository extends ServiceEntityRepository implements SiteRepositoryInterface
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Site::class);
    }

    public function findById(int $id): ?Site
    {
        return $this->find($id);
    }

    public function findByName(string $name): ?Site
    {
        return $this->findOneBy(['name' => $name]);
    }

    /**
     * @return list<Site>
     */
    public function findAllOrderedByName(): array
    {
        // @var list<Site> $sites
        return $this->findBy([], ['name' => 'ASC']);
    }

    public function save(Site $site): void
    {
        $this->getEntityManager()->persist($site);
        $this->getEntityManager()->flush();
    }

    public function remove(Site $site): void
    {
        $this->getEntityManager()->remove($site);
        $this->getEntityManager()->flush();
    }
}
