src/Contact/Repository/DoctrineContactAccessRepository.php line 43

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2019 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2019 TECLA Consulting Group oü
  16.  */
  17. namespace App\Contact\Repository;
  18. use App\Contact\Doctrine\DBAL\Types\ContactAccessRoleType;
  19. use App\Contact\Doctrine\DBAL\Types\ContactIdType;
  20. use App\Contact\Enum\ContactAccessRole;
  21. use App\Contact\Model\ContactAccess;
  22. use App\Contact\Model\ContactAccessInterface;
  23. use App\Contact\Model\ContactInterface;
  24. use App\Contact\Model\Identity\ContactIdInterface;
  25. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  26. use Doctrine\ORM\Query\Expr\Join;
  27. use Doctrine\ORM\QueryBuilder;
  28. use Doctrine\Persistence\ManagerRegistry;
  29. class DoctrineContactAccessRepository extends ServiceEntityRepository
  30. {
  31.     public function __construct(ManagerRegistry $registry)
  32.     {
  33.         parent::__construct($registryContactAccess::class);
  34.     }
  35.     /**
  36.      * @return ContactAccessInterface[]
  37.      */
  38.     public function findAllAccessors(ContactIdInterface $contactId, array $criteria null, array $orderBy nullint $limit nullint $offset null): ?array
  39.     {
  40.         return $this
  41.             ->createAllQueryBuilder($contactIdfalse$criteria$orderBy$limit$offset)
  42.             ->getQuery()
  43.             ->getResult()
  44.         ;
  45.     }
  46.     public function countAllAccessors(ContactIdInterface $contactId, array $criteria null): int
  47.     {
  48.         return (int) ($this
  49.             ->createAllQueryBuilder($contactIdtrue$criteria)
  50.             ->getQuery()
  51.             ->getSingleScalarResult())
  52.         ;
  53.     }
  54.     private function createAllQueryBuilder(ContactIdInterface $contactIdbool $isCount, array $criteria null, array $orderBy nullint $limit nullint $offset null): QueryBuilder
  55.     {
  56.         $addContactQuery false;
  57.         $addContactFullNameQuery false;
  58.         $addContactEmailQuery false;
  59.         $qb $this->createQueryBuilder('ca');
  60.         if (true === $isCount) {
  61.             $qb->select($qb->expr()->count('ca.id'));
  62.         } else {
  63.             if (null !== $limit) {
  64.                 $qb->setMaxResults($limit);
  65.             }
  66.             if (null !== $offset) {
  67.                 $qb->setFirstResult($offset);
  68.             }
  69.             $addContactQuery true;
  70.             $addContactEmailQuery true;
  71.         }
  72.         $qb
  73.             ->where($qb->expr()->eq('ca.contact'':contact'))
  74.             ->setParameter('contact'$contactIdContactIdType::NAME)
  75.         ;
  76.         if (false === empty($criteria)) {
  77.             foreach ($criteria as $criterion => $values) {
  78.                 if (true === empty($values)) {
  79.                     continue;
  80.                 }
  81.                 if ('search' === $criterion) {
  82.                     if (true === $isCount) {
  83.                         $qb
  84.                             ->andWhere(
  85.                                 $qb->expr()->orX(
  86.                                     $qb->expr()->like('c.name'':search'),
  87.                                     $qb->expr()->like("CONCAT(c.lastName, ' ', c.firstName)"':search'),
  88.                                     $qb->expr()->like('ce.email'':search')
  89.                                 )
  90.                             )
  91.                         ;
  92.                     } else {
  93.                         $qb
  94.                             ->having(
  95.                                 $qb->expr()->orX(
  96.                                     $qb->expr()->like('fullName'':search'),
  97.                                     $qb->expr()->like('ce.email'':search')
  98.                                 )
  99.                             )
  100.                         ;
  101.                     }
  102.                     $qb->setParameter('search''%' $values '%');
  103.                     $addContactFullNameQuery true;
  104.                     $addContactEmailQuery true;
  105.                 }
  106.                 if ('role' === $criterion) {
  107.                     if (true === \is_array($values)) {
  108.                         $qb->andWhere($qb->expr()->in('ca.role'':role'));
  109.                         foreach ($values as $k => $v) {
  110.                             $values[$k] = ContactAccessRoleType::transformToDatabaseValue(ContactAccessRole::tryFrom($v));
  111.                         }
  112.                     } else {
  113.                         $values ContactAccessRoleType::transformToDatabaseValue(ContactAccessRole::tryFrom($values));
  114.                         $qb->andWhere($qb->expr()->eq('ca.role'':role'));
  115.                     }
  116.                     $qb->setParameter('role'$values);
  117.                 }
  118.             }
  119.         }
  120.         if (false === $isCount) {
  121.             if (false === empty($orderBy)) {
  122.                 foreach ($orderBy as $column => $direction) {
  123.                     if ('fullName' === $column) {
  124.                         $qb->addOrderBy('fullName'$direction);
  125.                         $addContactFullNameQuery true;
  126.                     }
  127.                     if ('email' === $column) {
  128.                         $qb->addOrderBy('ce.email'$direction);
  129.                         $addContactEmailQuery true;
  130.                     }
  131.                     if ('role' === $column) {
  132.                         $qb->addOrderBy('ca.role'$direction);
  133.                     }
  134.                 }
  135.             }
  136.         }
  137.         if (true === $addContactQuery || true === $addContactFullNameQuery || true === $addContactEmailQuery) {
  138.             if (false === $isCount) {
  139.                 $qb->addSelect('c');
  140.             }
  141.             $qb
  142.                 ->innerJoin('ca.accessor''c'Join::WITH$qb->expr()->eq('c.id''ca.accessor'))
  143.             ;
  144.             if (true === $addContactFullNameQuery && false === $isCount) {
  145.                 $qb
  146.                     ->addSelect(
  147.                         "CASE WHEN c.type = 1 THEN c.name WHEN c.type = 2 THEN CONCAT(c.lastName, ' ', c.firstName) ELSE '' END AS HIDDEN fullName"
  148.                     )
  149.                 ;
  150.             }
  151.             if (true === $addContactEmailQuery) {
  152.                 if (false === $isCount) {
  153.                     $qb->addSelect('ce');
  154.                 }
  155.                 $qb
  156.                     ->innerJoin('c.contactEmail''ce'Join::WITH$qb->expr()->eq('ce.id''c.contactEmail'))
  157.                 ;
  158.             }
  159.         }
  160.         return $qb;
  161.     }
  162. }