<?php
namespace App\Repository\GuardianRegister;
use App\Entity\GuardianRegister\GuardianRegister;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method GuardianRegister|null find($id, $lockMode = null, $lockVersion = null)
* @method GuardianRegister|null findOneBy(array $criteria, array $orderBy = null)
* @method GuardianRegister[] findAll()
* @method GuardianRegister[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class GuardianRegisterRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, GuardianRegister::class);
}
/**
* Creates a new QueryBuilder instance that is prepopulated for this entity name.
*
* @param string $alias
* @param string $indexBy The index for the from.
*
* @return QueryBuilder
*/
public function createQueryBuilder($alias, $indexBy = null)
{
return $this->_em->createQueryBuilder()
->select($alias)
->from($this->_entityName, $alias, $indexBy)
->andWhere("${alias}.isDeleted = 0");
}
public function countNewRegistrations(): int
{
return (int) $this->createQueryBuilder('gr')
->select('COUNT(gr.id)')
->andWhere('gr.status = :status')
->setParameter('status', GuardianRegister::STATUS_NEW['value'])
->getQuery()
->getSingleScalarResult();
}
/**
* Finds other GuardianRegister rows for the same phone/email as $current
* that never got their own Guardian linked and are still in an "open"
* status. These are stale duplicate registration attempts left behind
* once $current (or another sibling) converted into a Guardian.
*
* @return GuardianRegister[]
*/
public function findOpenDuplicates(GuardianRegister $current, ?string $phone, ?string $email): array
{
$query = $this->createQueryBuilder('gr');
$contactConditions = $query->expr()->orX();
if ($phone) {
$contactConditions->add('gr.phone = :phone');
$contactConditions->add('gr.guardianPhone = :phone');
$query->setParameter('phone', $phone);
}
if ($email) {
$contactConditions->add('gr.email = :email');
$contactConditions->add('gr.guardianEmail = :email');
$query->setParameter('email', $email);
}
if (!$contactConditions->count()) {
return [];
}
return $query
->andWhere($contactConditions)
->andWhere('gr.id <> :currentId')
->setParameter('currentId', $current->getId())
->andWhere('gr.guardian IS NULL')
->andWhere('gr.status NOT IN (:closedStatuses)')
->setParameter('closedStatuses', [
GuardianRegister::STATUS_CONFIRMED['value'],
GuardianRegister::STATUS_LOST['value'],
GuardianRegister::STATUS_UNTARGETED['value'],
])
->getQuery()
->getResult();
}
// /**
// * @return GuardianRegister[] Returns an array of GuardianRegister objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('g')
->andWhere('g.exampleField = :val')
->setParameter('val', $value)
->orderBy('g.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?GuardianRegister
{
return $this->createQueryBuilder('g')
->andWhere('g.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}