<?php
namespace App\Controller\v1\Survey;
use App\Entity\Survey\Survey;
use App\Repository\Survey\SurveyRepository;
use Firebase\JWT\JWT;
use JMS\Serializer\SerializationContext;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use OpenApi\Annotations as OA;
/**
* @Route("/api/v1/survey/{url}", name="get.survey", methods={"GET"})
* @OA\Response(
* response=200,
* description="Информация о опросе получена"
* )
* @OA\Tag(name="Survey")
*/
class GetSurveyController extends AbstractController
{
public function __construct(
public SurveyRepository $surveyRepository,
public SerializerInterface $serializer
)
{
}
/**
* @param Request $request
* @param string $url
* @return JsonResponse
*/
public function __invoke(Request $request, string $url): JsonResponse
{
$survey = $this->surveyRepository->findOneBy(['uri' => $url]);
if ($survey === null) {
return $this->json([
'success' => false,
'errors' => [
'Опрос не найден'
]
], Response::HTTP_BAD_REQUEST);
}
if ($survey->getStatus() !== Survey::OPEN) {
return $this->json([
'success' => false,
'errors' => [
'Опрос закрыт'
]
], Response::HTTP_CONFLICT);
}
$dateTimeNow = new \DateTime();
if (
($survey->getValidStart() !== null && $survey->getValidStart() > $dateTimeNow)
|| ($survey->getValidEnd() !== null && $survey->getValidEnd() < $dateTimeNow)
) {
return $this->json([
'success' => false,
'errors' => [
'Опрос не доступен по срокам'
]
], Response::HTTP_CONFLICT);
}
$surveyCollectionStr = $this->serializer
->serialize($survey, 'json', ['groups' => 'survey']);
$surveyCollection = json_decode($surveyCollectionStr, true);
return $this->json([
'success' => true,
'surveyCollection' => $surveyCollection
], Response::HTTP_OK);
}
}