<?php
declare(strict_types=1);
namespace Slivki\Repository\Beauty\Masters;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Slivki\Entity\BeautyMaster;
use Slivki\Exception\Beauty\Master\MasterNotFoundException;
final class BeautyMasterRepository extends ServiceEntityRepository implements BeautyMasterRepositoryInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, BeautyMaster::class);
}
public function getById(int $id): BeautyMaster
{
$master = $this->findById($id);
if (!$master instanceof BeautyMaster) {
throw new MasterNotFoundException();
}
return $master;
}
public function findById(int $id): ?BeautyMaster
{
$queryBuilder = $this->createQueryBuilder('bm');
$expr = $queryBuilder->expr();
return $queryBuilder
->andWhere($expr->eq('bm.id', ':id'))
->setParameter('id', $id)
->getQuery()
->getOneOrNullResult();
}
public function findByGeoLocationId(int $locationId): array
{
$qb = $this->createQueryBuilder('bm');
$expr = $qb->expr();
return $qb
->innerJoin('bm.locations', 'l')
->andWhere($expr->eq('l.ID', ':locationId'))
->addOrderBy($expr->asc('bm.id'))
->setParameters([
'locationId' => $locationId,
])
->getQuery()
->getResult();
}
public function findByGeoLocationIds(array $locationIds): array
{
$qb = $this->createQueryBuilder('bm');
$expr = $qb->expr();
return $qb
->innerJoin('bm.locations', 'l')
->andWhere($expr->in('l.ID', ':locationIds'))
->addGroupBy('bm.id')
->addOrderBy($expr->asc('bm.id'))
->setParameters([
'locationIds' => $locationIds,
])
->getQuery()
->getResult();
}
public function findByCategoryId(int $categoryId): array
{
$qb = $this->createQueryBuilder('bm');
$expr = $qb->expr();
return $qb
->innerJoin('bm.categories', 'c')
->andWhere($expr->eq('c.ID', ':categoryId'))
->addOrderBy($expr->asc('bm.id'))
->setParameters([
'categoryId' => $categoryId,
])
->getQuery()
->getResult();
}
}