src/Products/NotificationsBundle/Doctrine/Repository/Notifications/TemplateRepository.php line 28

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Doctrine\Repository\Notifications;
  3. use App\Doctrine\Repository\SearchableRepositoryInterface;
  4. use App\Doctrine\Repository\SearchableRepositoryTrait;
  5. use App\Model\Searching\AbstractSearch;
  6. use App\Util\Querying;
  7. use Doctrine\ORM\EntityRepository;
  8. use Doctrine\ORM\QueryBuilder;
  9. use Products\NotificationsBundle\Entity\Notifications\Template;
  10. use Products\NotificationsBundle\Model\Searching\TemplateSearch;
  11. /**
  12.  * @method Template find($id, $lockMode = null, $lockVersion = null)
  13.  * @method Template findOneBy(array $criteria, array $orderBy = null)
  14.  * @method array|Template[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15.  * @method array|Template[] findAll()
  16.  */
  17. final class TemplateRepository extends EntityRepository implements SearchableRepositoryInterface
  18. {
  19.     use SearchableRepositoryTrait;
  20.     /**
  21.      * @param bool $urgent
  22.      * @return Template|null
  23.      */
  24.     public function findSendableDefault(bool $urgent): ?Template
  25.     {
  26.         return $this->findOneBy([
  27.             'name' => ($urgent) ? Template::DEFAULT_URGENT_NAME Template::DEFAULT_GENERAL_NAME,
  28.             'status' => Template::STATUSES__SENDABLE,
  29.         ]);
  30.     }
  31.     /**
  32.      * @param bool $urgent
  33.      * @return Template|null
  34.      */
  35.     public function findDefault(bool $urgent): ?Template
  36.     {
  37.         return $this->findOneBy([
  38.             'name' => ($urgent) ? Template::DEFAULT_URGENT_NAME Template::DEFAULT_GENERAL_NAME,
  39.         ]);
  40.     }
  41.     /**
  42.      * {@inheritDoc}
  43.      * @param TemplateSearch $search
  44.      */
  45.     public function qbBySearch(
  46.         AbstractSearch $search,
  47.         ?QueryBuilder $qb null
  48.     ): QueryBuilder
  49.     {
  50.         if ( ! $search instanceof TemplateSearch) {
  51.             throw new \Exception();
  52.         }
  53.         $qb $qb ?: $this->createQueryBuilder('templates');
  54.         switch ($search->getFilter()) {
  55.             case TemplateSearch::FILTERS__URGENT:
  56.                 $qb
  57.                     ->andWhere('templates.urgent = :urgent')
  58.                     ->setParameter('urgent'true);
  59.                 break;
  60.             case TemplateSearch::FILTERS__GENERAL:
  61.                 $qb
  62.                     ->andWhere('templates.urgent = :urgent')
  63.                     ->setParameter('urgent'false);
  64.                 break;
  65.             case TemplateSearch::FILTERS__AUTOMATION:
  66.                 $qb
  67.                     ->andWhere('templates.forAutomations = :automations')
  68.                     ->setParameter('automations'true);
  69.                 break;
  70.             case TemplateSearch::FILTERS__DRAFTS:
  71.                 $qb
  72.                     ->andWhere('templates.status = :status')
  73.                     ->setParameter('status'Template::STATUSES__DRAFT);
  74.                 break;
  75.             case TemplateSearch::FILTERS__SENDABLE:
  76.                 $qb
  77.                     ->andWhere('templates.status = :status')
  78.                     ->setParameter('status'Template::STATUSES__SENDABLE);
  79.                 break;
  80.             case TemplateSearch::FILTERS__SENDABLE_GENERAL:
  81.                 $qb
  82.                     ->andWhere('templates.urgent = :urgent')
  83.                     ->setParameter('urgent'false)
  84.                     ->andWhere('templates.status = :status')
  85.                     ->setParameter('status'Template::STATUSES__SENDABLE);
  86.                 break;
  87.             case TemplateSearch::FILTERS__SENDABLE_URGENT:
  88.                 $qb
  89.                     ->andWhere('templates.urgent = :urgent')
  90.                     ->setParameter('urgent'true)
  91.                     ->andWhere('templates.status = :status')
  92.                     ->setParameter('status'Template::STATUSES__SENDABLE);
  93.                 break;
  94.         }
  95.         if ( ! $search->hasDefaults()) {
  96.             $qb
  97.                 ->andWhere(Querying::dqlLike(
  98.                     'templates.name NOT LIKE :default'
  99.                 ))
  100.                 ->setParameter('default'Querying::likeStart(Template::DEFAULT_PREFIX));
  101.         }
  102.         if ($lookup $search->getLookup()) {
  103.             $qb
  104.                 ->andWhere(Querying::dqlLike(
  105.                     'templates.name LIKE :name'
  106.                 ))
  107.                 ->setParameter('name'Querying::likeAny($lookup));
  108.         }
  109.         if (in_array($search->getType(), ['general''urgent'], true)) {
  110.             $qb
  111.                 ->andWhere('templates.urgent = :urgent')
  112.                 ->setParameter('urgent', ($search->getType() === 'urgent'));
  113.         }
  114.         if (is_bool($search->isForAutomation())) {
  115.             $qb
  116.                 ->andWhere('templates.forAutomations = :forAutomations')
  117.                 ->setParameter('forAutomations'$search->isForAutomation());
  118.         }
  119.         switch ($search->getSort()) {
  120.             case TemplateSearch::SORTS__NAME:
  121.                 $qb->addOrderBy('templates.name'$search->getDirection());
  122.                 break;
  123.             case TemplateSearch::SORTS__TYPE:
  124.                 $qb
  125.                     ->addOrderBy('templates.urgent'Querying::dirFlip($search->getDirection()))
  126.                     ->addOrderBy('templates.name''ASC');
  127.                 break;
  128.             case TemplateSearch::SORTS__TIMESTAMP:
  129.                 $qb
  130.                     ->addOrderBy('templates.touchedAt'$search->getDirection())
  131.                     ->addOrderBy('templates.name''ASC');
  132.                 break;
  133.         }
  134.         return $qb;
  135.     }
  136. }