<?php
namespace App\Validator\ConstraintValidator;
use App\Entity\Security\ServiceSession;
use App\Entity\Survey\Survey;
use App\Entity\Survey\SurveyResult;
use App\Entity\User\User;
use App\Form\SearchFormType;
use App\Repository\Survey\AnswerRepository;
use App\Repository\Survey\QuestionRepository;
use App\Repository\Survey\SurveyRepository;
use App\Repository\Survey\SurveyResultRepository;
use App\Services\ElasticSearch\ElasticSearchService;
use App\Services\Survey\SurveyResultDataService;
use App\Services\Survey\SurveyResultService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class SurveyDataConstraintValidator extends ConstraintValidator
{
public function __construct(
public EntityManagerInterface $em,
public SurveyResultService $surveyResultService,
public SurveyRepository $surveyRepository,
public QuestionRepository $questionRepository,
public AnswerRepository $answerRepository,
public SurveyResultDataService $surveyResultDataService,
public SurveyResultRepository $surveyResultRepository
)
{
}
public function validate($formData, Constraint $constraint): void
{
$surveyCollections = $this->surveyRepository->find($formData["survey_id"]);
if ($surveyCollections === null) {
$this->context
->buildViolation(sprintf('На найден опрос с ID: %d', $formData["survey_id"]))
->addViolation();
}
if ($surveyCollections->getType() === Survey::TYPE_PRIVATE) {
if (empty($formData["user_email"])) {
$this->context
->buildViolation('Пользователь не авторизован!')
->addViolation();
} else {
$user = $this->em->getRepository(User::class)->findOneBy(['email' => $formData["user_email"]]);
if (empty($user)) {
$this->context
->buildViolation('Пользователь не авторизован!')
->addViolation();
} else {
$session = $this->em->getRepository(ServiceSession::class)
->findBy(['userId' => $user->getId(), 'active' => true]);
if (empty($session)) {
$this->context
->buildViolation('Пользователь не авторизован!')
->addViolation();
}
if ($surveyCollections->getRefresh() === Survey::REFRESH_DISABLED) {
$result = $this->surveyResultRepository->findPassingUserInValidityPeriod(
$user->getId(),
$surveyCollections->getId(),
$surveyCollections->getValidStart(),
$surveyCollections->getValidEnd()
);
if (!empty($result)) {
$this->context
->buildViolation('Вы уже голосовали!')
->addViolation();
}
}
}
}
}
}
}