<?php
namespace Products\NotificationsBundle\Doctrine\Repository\Notifications;
use App\Doctrine\Repository\SearchableRepositoryInterface;
use App\Doctrine\Repository\SearchableRepositoryTrait;
use App\Model\Searching\AbstractSearch;
use App\Util\Querying;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Products\NotificationsBundle\Entity\Notifications\Template;
use Products\NotificationsBundle\Model\Searching\TemplateSearch;
/**
* @method Template find($id, $lockMode = null, $lockVersion = null)
* @method Template findOneBy(array $criteria, array $orderBy = null)
* @method array|Template[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
* @method array|Template[] findAll()
*/
final class TemplateRepository extends EntityRepository implements SearchableRepositoryInterface
{
use SearchableRepositoryTrait;
/**
* @param bool $urgent
* @return Template|null
*/
public function findSendableDefault(bool $urgent): ?Template
{
return $this->findOneBy([
'name' => ($urgent) ? Template::DEFAULT_URGENT_NAME : Template::DEFAULT_GENERAL_NAME,
'status' => Template::STATUSES__SENDABLE,
]);
}
/**
* @param bool $urgent
* @return Template|null
*/
public function findDefault(bool $urgent): ?Template
{
return $this->findOneBy([
'name' => ($urgent) ? Template::DEFAULT_URGENT_NAME : Template::DEFAULT_GENERAL_NAME,
]);
}
/**
* {@inheritDoc}
* @param TemplateSearch $search
*/
public function qbBySearch(
AbstractSearch $search,
?QueryBuilder $qb = null
): QueryBuilder
{
if ( ! $search instanceof TemplateSearch) {
throw new \Exception();
}
$qb = $qb ?: $this->createQueryBuilder('templates');
switch ($search->getFilter()) {
case TemplateSearch::FILTERS__URGENT:
$qb
->andWhere('templates.urgent = :urgent')
->setParameter('urgent', true);
break;
case TemplateSearch::FILTERS__GENERAL:
$qb
->andWhere('templates.urgent = :urgent')
->setParameter('urgent', false);
break;
case TemplateSearch::FILTERS__AUTOMATION:
$qb
->andWhere('templates.forAutomations = :automations')
->setParameter('automations', true);
break;
case TemplateSearch::FILTERS__DRAFTS:
$qb
->andWhere('templates.status = :status')
->setParameter('status', Template::STATUSES__DRAFT);
break;
case TemplateSearch::FILTERS__SENDABLE:
$qb
->andWhere('templates.status = :status')
->setParameter('status', Template::STATUSES__SENDABLE);
break;
case TemplateSearch::FILTERS__SENDABLE_GENERAL:
$qb
->andWhere('templates.urgent = :urgent')
->setParameter('urgent', false)
->andWhere('templates.status = :status')
->setParameter('status', Template::STATUSES__SENDABLE);
break;
case TemplateSearch::FILTERS__SENDABLE_URGENT:
$qb
->andWhere('templates.urgent = :urgent')
->setParameter('urgent', true)
->andWhere('templates.status = :status')
->setParameter('status', Template::STATUSES__SENDABLE);
break;
}
if ( ! $search->hasDefaults()) {
$qb
->andWhere(Querying::dqlLike(
'templates.name NOT LIKE :default'
))
->setParameter('default', Querying::likeStart(Template::DEFAULT_PREFIX));
}
if ($lookup = $search->getLookup()) {
$qb
->andWhere(Querying::dqlLike(
'templates.name LIKE :name'
))
->setParameter('name', Querying::likeAny($lookup));
}
if (in_array($search->getType(), ['general', 'urgent'], true)) {
$qb
->andWhere('templates.urgent = :urgent')
->setParameter('urgent', ($search->getType() === 'urgent'));
}
if (is_bool($search->isForAutomation())) {
$qb
->andWhere('templates.forAutomations = :forAutomations')
->setParameter('forAutomations', $search->isForAutomation());
}
switch ($search->getSort()) {
case TemplateSearch::SORTS__NAME:
$qb->addOrderBy('templates.name', $search->getDirection());
break;
case TemplateSearch::SORTS__TYPE:
$qb
->addOrderBy('templates.urgent', Querying::dirFlip($search->getDirection()))
->addOrderBy('templates.name', 'ASC');
break;
case TemplateSearch::SORTS__TIMESTAMP:
$qb
->addOrderBy('templates.touchedAt', $search->getDirection())
->addOrderBy('templates.name', 'ASC');
break;
}
return $qb;
}
}