72 lines
2.4 KiB
PHP
72 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Validator\Constraints;
|
|
|
|
use App\Repository\DispositionRepository;
|
|
use Symfony\Component\Validator\Constraint;
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
|
|
class ApplicationValidator extends ConstraintValidator
|
|
{
|
|
public function __construct(private readonly DispositionRepository $dispositionRepository)
|
|
{
|
|
}
|
|
|
|
public function validate(mixed $value, Constraint $constraint): void
|
|
{
|
|
/** @var \App\Entity\Application $application */
|
|
$application = $value;
|
|
|
|
$teamer = $application->getTeamer();
|
|
|
|
if (true === $teamer->isAllowOverlappingApplications()) {
|
|
return;
|
|
}
|
|
|
|
$assignment = $application->getAssignment();
|
|
$destination = $assignment->getDestination();
|
|
|
|
$applicationDateFrom = $destination->getDateFrom();
|
|
$applicationDateTo = $destination->getDateTo();
|
|
|
|
$qb = $this
|
|
->dispositionRepository
|
|
->createQueryBuilder('disposition')
|
|
;
|
|
|
|
$conflictingCount = $qb
|
|
->select($qb->expr()->count('disposition'))
|
|
->innerJoin('disposition.assignment', 'assignment')
|
|
->innerJoin('assignment.destination', 'destination')
|
|
->where($qb->expr()->andX(
|
|
$qb->expr()->eq('disposition.teamer', ':teamer'),
|
|
$qb->expr()->isNull('assignment.deletedAt'),
|
|
$qb->expr()->isNull('destination.deletedAt'),
|
|
$qb->expr()->orX(
|
|
$qb->expr()->andX(
|
|
$qb->expr()->gt('destination.dateTo', ':application_date_from'),
|
|
$qb->expr()->lt('destination.dateTo', ':application_date_to'),
|
|
),
|
|
$qb->expr()->andX(
|
|
$qb->expr()->lt('destination.dateFrom', ':application_date_to'),
|
|
$qb->expr()->gt('destination.dateFrom', ':application_date_from'),
|
|
)
|
|
)
|
|
))
|
|
->setParameter('teamer', $teamer)
|
|
->setParameter('application_date_from', $applicationDateFrom)
|
|
->setParameter('application_date_to', $applicationDateTo)
|
|
->getQuery()
|
|
->getSingleScalarResult()
|
|
;
|
|
|
|
if (0 < $conflictingCount) {
|
|
$this
|
|
->context
|
|
->buildViolation($constraint->message)
|
|
->addViolation()
|
|
;
|
|
}
|
|
}
|
|
}
|