src/Contact/Repository/DoctrineContactAccessRepository.php line 43
<?phpdeclare(strict_types=1);/*** Copyright (c) 2019 TECLA Consulting Group oü.* All rights reserved.** This unpublished material is proprietary to TECLA Consulting Group oü.* All rights reserved. The methods and* techniques described herein are considered trade secrets* and/or confidential. Reproduction or distribution, in whole* or in part, is forbidden except by express written permission* of TECLA Consulting Group oü.** @author Matúš Sýkorjak <matus@tecla.no>* @copyright 2019 TECLA Consulting Group oü*/namespace App\Contact\Repository;use App\Contact\Doctrine\DBAL\Types\ContactAccessRoleType;use App\Contact\Doctrine\DBAL\Types\ContactIdType;use App\Contact\Enum\ContactAccessRole;use App\Contact\Model\ContactAccess;use App\Contact\Model\ContactAccessInterface;use App\Contact\Model\ContactInterface;use App\Contact\Model\Identity\ContactIdInterface;use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;use Doctrine\ORM\Query\Expr\Join;use Doctrine\ORM\QueryBuilder;use Doctrine\Persistence\ManagerRegistry;class DoctrineContactAccessRepository extends ServiceEntityRepository{public function __construct(ManagerRegistry $registry){parent::__construct($registry, ContactAccess::class);}/*** @return ContactAccessInterface[]*/public function findAllAccessors(ContactIdInterface $contactId, array $criteria = null, array $orderBy = null, int $limit = null, int $offset = null): ?array{return $this->createAllQueryBuilder($contactId, false, $criteria, $orderBy, $limit, $offset)->getQuery()->getResult();}public function countAllAccessors(ContactIdInterface $contactId, array $criteria = null): int{return (int) ($this->createAllQueryBuilder($contactId, true, $criteria)->getQuery()->getSingleScalarResult());}private function createAllQueryBuilder(ContactIdInterface $contactId, bool $isCount, array $criteria = null, array $orderBy = null, int $limit = null, int $offset = null): QueryBuilder{$addContactQuery = false;$addContactFullNameQuery = false;$addContactEmailQuery = false;$qb = $this->createQueryBuilder('ca');if (true === $isCount) {$qb->select($qb->expr()->count('ca.id'));} else {if (null !== $limit) {$qb->setMaxResults($limit);}if (null !== $offset) {$qb->setFirstResult($offset);}$addContactQuery = true;$addContactEmailQuery = true;}$qb->where($qb->expr()->eq('ca.contact', ':contact'))->setParameter('contact', $contactId, ContactIdType::NAME);if (false === empty($criteria)) {foreach ($criteria as $criterion => $values) {if (true === empty($values)) {continue;}if ('search' === $criterion) {if (true === $isCount) {$qb->andWhere($qb->expr()->orX($qb->expr()->like('c.name', ':search'),$qb->expr()->like("CONCAT(c.lastName, ' ', c.firstName)", ':search'),$qb->expr()->like('ce.email', ':search')));} else {$qb->having($qb->expr()->orX($qb->expr()->like('fullName', ':search'),$qb->expr()->like('ce.email', ':search')));}$qb->setParameter('search', '%' . $values . '%');$addContactFullNameQuery = true;$addContactEmailQuery = true;}if ('role' === $criterion) {if (true === \is_array($values)) {$qb->andWhere($qb->expr()->in('ca.role', ':role'));foreach ($values as $k => $v) {$values[$k] = ContactAccessRoleType::transformToDatabaseValue(ContactAccessRole::tryFrom($v));}} else {$values = ContactAccessRoleType::transformToDatabaseValue(ContactAccessRole::tryFrom($values));$qb->andWhere($qb->expr()->eq('ca.role', ':role'));}$qb->setParameter('role', $values);}}}if (false === $isCount) {if (false === empty($orderBy)) {foreach ($orderBy as $column => $direction) {if ('fullName' === $column) {$qb->addOrderBy('fullName', $direction);$addContactFullNameQuery = true;}if ('email' === $column) {$qb->addOrderBy('ce.email', $direction);$addContactEmailQuery = true;}if ('role' === $column) {$qb->addOrderBy('ca.role', $direction);}}}}if (true === $addContactQuery || true === $addContactFullNameQuery || true === $addContactEmailQuery) {if (false === $isCount) {$qb->addSelect('c');}$qb->innerJoin('ca.accessor', 'c', Join::WITH, $qb->expr()->eq('c.id', 'ca.accessor'));if (true === $addContactFullNameQuery && false === $isCount) {$qb->addSelect("CASE WHEN c.type = 1 THEN c.name WHEN c.type = 2 THEN CONCAT(c.lastName, ' ', c.firstName) ELSE '' END AS HIDDEN fullName");}if (true === $addContactEmailQuery) {if (false === $isCount) {$qb->addSelect('ce');}$qb->innerJoin('c.contactEmail', 'ce', Join::WITH, $qb->expr()->eq('ce.id', 'c.contactEmail'));}}return $qb;}}