<?php
namespace App\Repository\Restaurants;
use App\Data\SearchrestaurantData;
use App\Entity\Restaurants\Restaurants;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\QueryBuilder;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Knp\Component\Pager\PaginatorInterface;
/**
* @method Restaurants|null find($id, $lockMode = null, $lockVersion = null)
* @method Restaurants|null findOneBy(array $criteria, array $orderBy = null)
* @method Restaurants[] findAll()
* @method Restaurants[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class RestaurantsRepository extends ServiceEntityRepository
{
/**
* @var PaginatorInterface
*/
private $paginator;
public function __construct(ManagerRegistry $registry, PaginatorInterface $paginator)
{
parent::__construct($registry, Restaurants::class);
$this->paginator = $paginator;
}
/**
* @param $page
* @return PaginationInterface
*/
public function findRestaurantsByActivated($page): PaginationInterface
{
$query = $this->createQueryBuilder('r')
->select('r')
->andWhere('r.activated = 1')
->andWhere('r.online = 1')
->orderBy('r.id', 'DESC')
->getQuery();
return $this->paginator->paginate(
$query,
$page,
16
);
}
/**
* @param SearchrestaurantData $search
* @return PaginationInterface
*/
public function findSearch(SearchrestaurantData $search): PaginationInterface
{
$query = $this->getSearchQuery($search)->getQuery();
return $this->paginator->paginate(
$query,
$search->page,
16
);
}
/**
* @param SearchrestaurantData $search
* @return QueryBuilder
*/
private function getSearchQuery(SearchrestaurantData $search): QueryBuilder{
$query = $this->createQueryBuilder('r')
->select('r', 'c', 's', 'cm')
->leftJoin('r.cities', 'c')
->leftJoin('r.specialites', 's')
->leftJoin('c.communes', 'cm')
->andWhere('r.activated = TRUE')
->andWhere('r.online = TRUE')
->orderBy('r.ordre', 'ASC')
;
if (!empty($search->q)){
$query = $query
->andWhere('r.name LIKE :q')
->setParameter('q', "%{$search->q}%")
;
}
if (!empty($search->villes)){
$query = $query
->andWhere('c.id = :citie')
->setParameter('citie', $search->villes->getId())
;
}
if (!empty($search->communes)){
$query = $query
->andWhere('cm.id = :commune')
->setParameter('commune', $search->communes->getId())
;
}
if (!empty($search->specialite)){
$query = $query
->andWhere('s.id IN (:specialite)')
->setParameter('specialite', $search->specialite)
;
}
return $query;
}
public function getRestaurantsRandom(Restaurants $restaurants, $limit)
{
return $this->createQueryBuilder('r')
->andWhere('r.id != :restaurant')
->setParameter('restaurant', $restaurants)
->orderBy('RAND()')
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
/**
* @return int|mixed|string|null
* @throws NonUniqueResultException
*/
public function featured()
{
return $this->createQueryBuilder('r')
->andWhere('r.online = 1')
->andWhere('r.activated = 1')
->orderBy('RAND()')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
}