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. ])
  79. ->getQuery()
  80. ->getResult();
  81. }
  82. // /**
  83. // * @return GuardianRegister[] Returns an array of GuardianRegister objects
  84. // */
  85. /*
  86. public function findByExampleField($value)
  87. {
  88. return $this->createQueryBuilder('g')
  89. ->andWhere('g.exampleField = :val')
  90. ->setParameter('val', $value)
  91. ->orderBy('g.id', 'ASC')
  92. ->setMaxResults(10)
  93. ->getQuery()
  94. ->getResult()
  95. ;
  96. }
  97. */
  98. /*
  99. public function findOneBySomeField($value): ?GuardianRegister
  100. {
  101. return $this->createQueryBuilder('g')
  102. ->andWhere('g.exampleField = :val')
  103. ->setParameter('val', $value)
  104. ->getQuery()
  105. ->getOneOrNullResult()
  106. ;
  107. }
  108. */
  109. }