<?php


namespace AutoAdmin\Bundle\Content\Repository;


use Doctrine\ORM\NonUniqueResultException;

class DefaultPageRepository extends DefaultTranslatableContentRepository
{
    public function findHomepage(?string $locale)
    {
        $qb = $this->createQueryBuilder('p');

        $this->applySiteContext($qb);

        $qb->andWhere('p.homepage = :homepage')
            ->setParameter('homepage', true);

        $qb->join('p.translations', 'ptr');

        $qb->join('ptr.locale', 'l');
        $qb->andWhere('l.code = :locale')
            ->setParameter('locale', $locale);

        try {
            $data = $qb->getQuery()->getOneOrNullResult();
        } catch (NonUniqueResultException $e) {
            $data = null;
        }

        return $data;
    }
}