src/Controller/v1/Survey/GetSurveyController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller\v1\Survey;
  3. use App\Entity\Survey\Survey;
  4. use App\Repository\Survey\SurveyRepository;
  5. use Firebase\JWT\JWT;
  6. use JMS\Serializer\SerializationContext;
  7. use Symfony\Component\Serializer\SerializerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use OpenApi\Annotations as OA;
  15. /**
  16.  * @Route("/api/v1/survey/{url}", name="get.survey", methods={"GET"})
  17.  * @OA\Response(
  18.  *     response=200,
  19.  *     description="Информация о опросе получена"
  20.  * )
  21.  * @OA\Tag(name="Survey")
  22.  */
  23. class GetSurveyController extends AbstractController
  24. {
  25.     public function __construct(
  26.         public SurveyRepository    $surveyRepository,
  27.         public SerializerInterface $serializer
  28.     )
  29.     {
  30.     }
  31.     /**
  32.      * @param Request $request
  33.      * @param string $url
  34.      * @return JsonResponse
  35.      */
  36.     public function __invoke(Request $requeststring $url): JsonResponse
  37.     {
  38.         $survey $this->surveyRepository->findOneBy(['uri' => $url]);
  39.         if ($survey === null) {
  40.             return $this->json([
  41.                 'success' => false,
  42.                 'errors' => [
  43.                     'Опрос не найден'
  44.                 ]
  45.             ], Response::HTTP_BAD_REQUEST);
  46.         }
  47.         if ($survey->getStatus() !== Survey::OPEN) {
  48.             return $this->json([
  49.                 'success' => false,
  50.                 'errors' => [
  51.                     'Опрос закрыт'
  52.                 ]
  53.             ], Response::HTTP_CONFLICT);
  54.         }
  55.         $dateTimeNow = new \DateTime();
  56.         if (
  57.             ($survey->getValidStart() !== null && $survey->getValidStart() > $dateTimeNow)
  58.             || ($survey->getValidEnd() !== null && $survey->getValidEnd() < $dateTimeNow)
  59.         ) {
  60.             return $this->json([
  61.                 'success' => false,
  62.                 'errors' => [
  63.                     'Опрос не доступен по срокам'
  64.                 ]
  65.             ], Response::HTTP_CONFLICT);
  66.         }
  67.         $surveyCollectionStr $this->serializer
  68.             ->serialize($survey'json', ['groups' => 'survey']);
  69.         $surveyCollection json_decode($surveyCollectionStrtrue);
  70.         return $this->json([
  71.             'success' => true,
  72.             'surveyCollection' => $surveyCollection
  73.         ], Response::HTTP_OK);
  74.     }
  75. }