<?php
namespace App\Controller\Frontend\Assay;
use App\Entity\Assay\Test;
use App\Repository\Assay\TestRepository;
use App\ViewModel\Frontend\TestViewModel;
use Doctrine\ORM\EntityManagerInterface;
use Nelmio\ApiDocBundle\Annotation\Security;
use OpenApi\Annotations as OA;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class TestsController
*
* @package App\Controller
* @Route(path="tests")
*/
class TestsController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var TestRepository
*/
private $testRepository;
/**
* TestsController constructor.
*
* @param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
$this->testRepository = $this->entityManager
->getRepository(Test::class);
}
/**
* Get the tests available
* @return JsonResponse
* @Route(methods={"GET"})
* @Security(name="Bearer")
* @OA\Response(
* response="200",
* description="The tests available",
* ref="#/components/schemas/Test"
* )
* @OA\Response(
* response="401",
* description="Authentication needed",
* ref="#/components/schemas/UnauthorizedResponseError"
* )
*/
public function indexAction(): JsonResponse
{
$user = $this->getUser();
if(strpos($user->getUsername(), '@stabvida.com') !== false) {
$tests = $this->testRepository->findBy(['showInAppTo' => ['0', '1']], ['position' => 'ASC']);
}
else {
$tests = $this->testRepository->findBy(['showInAppTo' => '0'], ['position' => 'ASC']);
}
return $this->json(
$tests,
200,
[],
TestViewModel::VIEW_CONTEXT
);
}
/**
* Get a test by ID
*
* @Route(path="/{testId}", requirements={"testId": "\d+"}, methods={"GET"})
* @param int $testId
*
* @return JsonResponse
*
* @Security(name="Bearer")
* @OA\Response(
* response="200",
* ref="#/components/schemas/Test"
* )
* @OA\Response(
* response="404",
* ref="#/components/schemas/NotFoundError"
* )
* @OA\Response(
* response="401",
* description="Authentication needed",
* ref="#/components/schemas/UnauthorizedResponseError"
* )
*/
public function getAction(int $testId): JsonResponse
{
$test = $this->testRepository->find($testId);
if (!$test) {
throw $this->createNotFoundException();
}
return $this->json(
$test,
200,
[],
TestViewModel::VIEW_CONTEXT
);
}
}