2018-06-29 23:52:53 +00:00
|
|
|
<?php
|
2022-07-10 13:51:26 +00:00
|
|
|
declare(strict_types=1);
|
2018-06-29 23:52:53 +00:00
|
|
|
|
|
|
|
namespace App\Validator\Constraints;
|
|
|
|
|
|
|
|
use App\Entity\Invite;
|
|
|
|
use App\Repository\InviteRepository;
|
|
|
|
use Symfony\Component\Validator\{Constraint, ConstraintValidator};
|
|
|
|
|
|
|
|
class ValidInviteValidator extends ConstraintValidator
|
|
|
|
{
|
2022-07-10 13:51:26 +00:00
|
|
|
public function __construct(
|
|
|
|
private readonly InviteRepository $inviteRepo
|
|
|
|
) {
|
2018-06-29 23:52:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ValidInvite $constraint
|
|
|
|
*/
|
2022-07-10 13:51:26 +00:00
|
|
|
public function validate(mixed $value, Constraint $constraint)
|
2018-06-29 23:52:53 +00:00
|
|
|
{
|
|
|
|
/** @var Invite $invite */
|
|
|
|
if (null === $invite = $this->inviteRepo->findOneBy(['code' => $value])) {
|
|
|
|
$this->context->buildViolation($constraint->notFoundMessage)
|
|
|
|
->setParameter('{{ code }}', $value)
|
|
|
|
->addViolation()
|
|
|
|
;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $invite->getUsedBy()) {
|
|
|
|
$this->context->buildViolation($constraint->usedMessage)
|
|
|
|
->setParameter('{{ code }}', $value)
|
|
|
|
->addViolation()
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
2022-07-10 13:51:26 +00:00
|
|
|
}
|