src/Repository/GuardianRegister/GuardianRegisterRepository.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Repository\GuardianRegister;
  3. use App\Entity\GuardianRegister\GuardianRegister;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\ORM\QueryBuilder;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. /**
  8. * @method GuardianRegister|null find($id, $lockMode = null, $lockVersion = null)
  9. * @method GuardianRegister|null findOneBy(array $criteria, array $orderBy = null)
  10. * @method GuardianRegister[] findAll()
  11. * @method GuardianRegister[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  12. */
  13. class GuardianRegisterRepository extends ServiceEntityRepository
  14. {
  15. public function __construct(ManagerRegistry $registry)
  16. {
  17. parent::__construct($registry, GuardianRegister::class);
  18. }
  19. /**
  20. * Creates a new QueryBuilder instance that is prepopulated for this entity name.
  21. *
  22. * @param string $alias
  23. * @param string $indexBy The index for the from.
  24. *
  25. * @return QueryBuilder
  26. */
  27. public function createQueryBuilder($alias, $indexBy = null)
  28. {
  29. return $this->_em->createQueryBuilder()
  30. ->select($alias)
  31. ->from($this->_entityName, $alias, $indexBy)
  32. ->andWhere("${alias}.isDeleted = 0");
  33. }
  34. public function countNewRegistrations(): int
  35. {
  36. return (int) $this->createQueryBuilder('gr')
  37. ->select('COUNT(gr.id)')
  38. ->andWhere('gr.status = :status')
  39. ->setParameter('status', GuardianRegister::STATUS_NEW['value'])
  40. ->getQuery()
  41. ->getSingleScalarResult();
  42. }
  43. /**
  44. * Finds other GuardianRegister rows for the same phone/email as $current
  45. * that never got their own Guardian linked and are still in an "open"
  46. * status. These are stale duplicate registration attempts left behind
  47. * once $current (or another sibling) converted into a Guardian.
  48. *
  49. * @return GuardianRegister[]
  50. */
  51. public function findOpenDuplicates(GuardianRegister $current, ?string $phone, ?string $email): array
  52. {
  53. $query = $this->createQueryBuilder('gr');
  54. $contactConditions = $query->expr()->orX();
  55. if ($phone) {
  56. $contactConditions->add('gr.phone = :phone');
  57. $contactConditions->add('gr.guardianPhone = :phone');
  58. $query->setParameter('phone', $phone);
  59. }
  60. if ($email) {
  61. $contactConditions->add('gr.email = :email');
  62. $contactConditions->add('gr.guardianEmail = :email');
  63. $query->setParameter('email', $email);
  64. }
  65. if (!$contactConditions->count()) {
  66. return [];
  67. }
  68. return $query
  69. ->andWhere($contactConditions)
  70. ->andWhere('gr.id <> :currentId')
  71. ->setParameter('currentId', $current->getId())
  72. ->andWhere('gr.guardian IS NULL')
  73. ->andWhere('gr.status NOT IN (:closedStatuses)')
  74. ->setParameter('closedStatuses', [
  75. GuardianRegister::STATUS_CONFIRMED['value'],
  76. GuardianRegister::STATUS_LOST['value'],
  77. GuardianRegister::STATUS_UNTARGETED['value'],
  78. GuardianRegister::STATUS_DUPLICATE['value'],
  79. ])
  80. ->getQuery()
  81. ->getResult();
  82. }
  83. /**
  84. * Finds the most recent registration created before $registration that
  85. * shares one of its emails/phones and is still being worked on.
  86. *
  87. * @param string[] $emails
  88. * @param string[] $phoneSuffixes trailing digits matched against stored phones
  89. */
  90. public function findOpenOriginal(GuardianRegister $registration, array $emails, array $phoneSuffixes): ?GuardianRegister
  91. {
  92. $query = $this->createQueryBuilder('gr');
  93. $contactConditions = $query->expr()->orX();
  94. foreach (array_values($emails) as $index => $email) {
  95. $contactConditions->add("gr.email = :email{$index}");
  96. $contactConditions->add("gr.guardianEmail = :email{$index}");
  97. $query->setParameter("email{$index}", $email);
  98. }
  99. foreach (array_values($phoneSuffixes) as $index => $phoneSuffix) {
  100. $contactConditions->add("gr.phone LIKE :phone{$index}");
  101. $contactConditions->add("gr.guardianPhone LIKE :phone{$index}");
  102. $query->setParameter("phone{$index}", "%{$phoneSuffix}");
  103. }
  104. if (!$contactConditions->count()) {
  105. return null;
  106. }
  107. return $query
  108. ->andWhere($contactConditions)
  109. ->andWhere('gr.id < :currentId')
  110. ->setParameter('currentId', $registration->getId())
  111. ->andWhere('gr.status IN (:openStatuses)')
  112. ->setParameter('openStatuses', GuardianRegister::OPEN_STATUSES_FOR_DUPLICATE_CHECK)
  113. ->orderBy('gr.id', 'DESC')
  114. ->setMaxResults(1)
  115. ->getQuery()
  116. ->getOneOrNullResult();
  117. }
  118. // /**
  119. // * @return GuardianRegister[] Returns an array of GuardianRegister objects
  120. // */
  121. /*
  122. public function findByExampleField($value)
  123. {
  124. return $this->createQueryBuilder('g')
  125. ->andWhere('g.exampleField = :val')
  126. ->setParameter('val', $value)
  127. ->orderBy('g.id', 'ASC')
  128. ->setMaxResults(10)
  129. ->getQuery()
  130. ->getResult()
  131. ;
  132. }
  133. */
  134. /*
  135. public function findOneBySomeField($value): ?GuardianRegister
  136. {
  137. return $this->createQueryBuilder('g')
  138. ->andWhere('g.exampleField = :val')
  139. ->setParameter('val', $value)
  140. ->getQuery()
  141. ->getOneOrNullResult()
  142. ;
  143. }
  144. */
  145. }