src/Controller/Frontend/Assay/TestsController.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend\Assay;
  3. use App\Entity\Assay\Test;
  4. use App\Repository\Assay\TestRepository;
  5. use App\ViewModel\Frontend\TestViewModel;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Nelmio\ApiDocBundle\Annotation\Security;
  8. use OpenApi\Annotations as OA;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * Class TestsController
  14.  *
  15.  * @package App\Controller
  16.  * @Route(path="tests")
  17.  */
  18. class TestsController extends AbstractController
  19. {
  20.     /**
  21.      * @var EntityManagerInterface
  22.      */
  23.     private $entityManager;
  24.     /**
  25.      * @var TestRepository
  26.      */
  27.     private $testRepository;
  28.     /**
  29.      * TestsController constructor.
  30.      *
  31.      * @param EntityManagerInterface $entityManager
  32.      */
  33.     public function __construct(EntityManagerInterface $entityManager)
  34.     {
  35.         $this->entityManager $entityManager;
  36.         $this->testRepository $this->entityManager
  37.             ->getRepository(Test::class);
  38.     }
  39.     /**
  40.      * Get the tests available
  41.      * @return JsonResponse
  42.      * @Route(methods={"GET"})
  43.      * @Security(name="Bearer")
  44.      * @OA\Response(
  45.      *     response="200",
  46.      *     description="The tests available",
  47.      *     ref="#/components/schemas/Test"
  48.      * )
  49.      * @OA\Response(
  50.      *     response="401",
  51.      *     description="Authentication needed",
  52.      *     ref="#/components/schemas/UnauthorizedResponseError"
  53.      * )
  54.      */
  55.     public function indexAction(): JsonResponse
  56.     {
  57.         $user $this->getUser();
  58.         if(strpos($user->getUsername(), '@stabvida.com') !== false) {
  59.             $tests $this->testRepository->findBy(['showInAppTo' => ['0''1']], ['position' => 'ASC']);
  60.         }
  61.         else {
  62.             $tests $this->testRepository->findBy(['showInAppTo' => '0'], ['position' => 'ASC']);
  63.         }
  64.         return $this->json(
  65.             $tests,
  66.             200,
  67.             [],
  68.             TestViewModel::VIEW_CONTEXT
  69.         );
  70.     }
  71.     /**
  72.      * Get a test by ID
  73.      *
  74.      * @Route(path="/{testId}", requirements={"testId": "\d+"}, methods={"GET"})
  75.      * @param int $testId
  76.      *
  77.      * @return JsonResponse
  78.      *
  79.      * @Security(name="Bearer")
  80.      * @OA\Response(
  81.      *     response="200",
  82.      *     ref="#/components/schemas/Test"
  83.      * )
  84.      * @OA\Response(
  85.      *     response="404",
  86.      *     ref="#/components/schemas/NotFoundError"
  87.      * )
  88.      * @OA\Response(
  89.      *     response="401",
  90.      *     description="Authentication needed",
  91.      *     ref="#/components/schemas/UnauthorizedResponseError"
  92.      * )
  93.      */
  94.     public function getAction(int $testId): JsonResponse
  95.     {
  96.         $test $this->testRepository->find($testId);
  97.         if (!$test) {
  98.             throw $this->createNotFoundException();
  99.         }
  100.         return $this->json(
  101.             $test,
  102.             200,
  103.             [],
  104.             TestViewModel::VIEW_CONTEXT
  105.         );
  106.     }
  107. }