<?php


namespace AutoAdmin\Bundle\Content\Repository;


use AutoAdmin\Bundle\Content\Entity\Domain;
use AutoAdmin\Bundle\Content\Entity\Site;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class SiteRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Site::class);
    }

    public function findByDomain(Domain $domain)
    {
        $qb = $this->createQueryBuilder('_');
        $qb->join('_.domains', 'd');
        $qb->andWhere('d.domain = :domain')
            ->setParameter('domain', $domain->getDomain());

        return $qb->getQuery()->execute()[0] ?? null;
    }
}
