src/Validator/ConstraintValidator/SurveyDataConstraintValidator.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Validator\ConstraintValidator;
  3. use App\Entity\Security\ServiceSession;
  4. use App\Entity\Survey\Survey;
  5. use App\Entity\Survey\SurveyResult;
  6. use App\Entity\User\User;
  7. use App\Form\SearchFormType;
  8. use App\Repository\Survey\AnswerRepository;
  9. use App\Repository\Survey\QuestionRepository;
  10. use App\Repository\Survey\SurveyRepository;
  11. use App\Repository\Survey\SurveyResultRepository;
  12. use App\Services\ElasticSearch\ElasticSearchService;
  13. use App\Services\Survey\SurveyResultDataService;
  14. use App\Services\Survey\SurveyResultService;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Validator\Constraint;
  19. use Symfony\Component\Validator\ConstraintValidator;
  20. class SurveyDataConstraintValidator extends ConstraintValidator
  21. {
  22.     public function __construct(
  23.         public EntityManagerInterface  $em,
  24.         public SurveyResultService     $surveyResultService,
  25.         public SurveyRepository        $surveyRepository,
  26.         public QuestionRepository      $questionRepository,
  27.         public AnswerRepository        $answerRepository,
  28.         public SurveyResultDataService $surveyResultDataService,
  29.         public SurveyResultRepository $surveyResultRepository
  30.     )
  31.     {
  32.     }
  33.     public function validate($formDataConstraint $constraint): void
  34.     {
  35.         $surveyCollections $this->surveyRepository->find($formData["survey_id"]);
  36.         if ($surveyCollections === null) {
  37.             $this->context
  38.                 ->buildViolation(sprintf('На найден опрос с ID: %d'$formData["survey_id"]))
  39.                 ->addViolation();
  40.         }
  41.         if ($surveyCollections->getType() === Survey::TYPE_PRIVATE) {
  42.             if (empty($formData["user_email"])) {
  43.                 $this->context
  44.                     ->buildViolation('Пользователь не авторизован!')
  45.                     ->addViolation();
  46.             } else {
  47.                 $user $this->em->getRepository(User::class)->findOneBy(['email' => $formData["user_email"]]);
  48.                 if (empty($user)) {
  49.                     $this->context
  50.                         ->buildViolation('Пользователь не авторизован!')
  51.                         ->addViolation();
  52.                 } else {
  53.                     $session $this->em->getRepository(ServiceSession::class)
  54.                         ->findBy(['userId' => $user->getId(), 'active' => true]);
  55.                     if (empty($session)) {
  56.                         $this->context
  57.                             ->buildViolation('Пользователь не авторизован!')
  58.                             ->addViolation();
  59.                     }
  60.                     if ($surveyCollections->getRefresh() === Survey::REFRESH_DISABLED) {
  61.                         $result $this->surveyResultRepository->findPassingUserInValidityPeriod(
  62.                             $user->getId(),
  63.                             $surveyCollections->getId(),
  64.                             $surveyCollections->getValidStart(),
  65.                             $surveyCollections->getValidEnd()
  66.                         );
  67.                         if (!empty($result)) {
  68.                             $this->context
  69.                                 ->buildViolation('Вы уже голосовали!')
  70.                                 ->addViolation();
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }