src/Controller/TestController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Project;
  4. use App\Entity\Record;
  5. use App\Entity\UserProject;
  6. use Doctrine\ORM\EntityManager;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\ORM\Exception\ORMException;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Twig\Environment;
  16. use Twig\Error\LoaderError;
  17. use Twig\Error\RuntimeError;
  18. use Twig\Error\SyntaxError;
  19. class TestController extends AbstractController
  20. {
  21.     /**
  22.      * @Route("/",name="home")
  23.      *
  24.      * @throws RuntimeError
  25.      * @throws LoaderError
  26.      * @throws SyntaxError
  27.      */
  28.     public function indexAction(Environment $twig): Response
  29.     {
  30.         return new Response($twig->render('index.html.twig'));
  31.     }
  32.     /**
  33.      * @Route("/api", name="test")
  34.      */
  35.     public function api(): JsonResponse
  36.     {
  37.         $response = new JsonResponse();
  38.         $response->setContent(json_encode([
  39.             'userIdentifier' => $this->getUser()->getUserIdentifier(),
  40.             'roles' => $this->getUser()->getRoles(),
  41.         ]));
  42.         return $response;
  43.     }
  44.     /**
  45.      * @throws NotFoundHttpException
  46.      *
  47.      * @Route("/api/projectRoles/{id_project}", name="projectRoles")
  48.      */
  49.     public function projectRoles(int $id_projectManagerRegistry $doctrine): JsonResponse
  50.     {
  51.         $project $doctrine->getRepository(Project::class)->find($id_project);
  52.         if (!$project) {
  53.             throw $this->createNotFoundException('Project not found');
  54.         }
  55.         $userProject $doctrine
  56.             ->getRepository(UserProject::class)
  57.             ->findOneBy(['user' => $this->getUser(), 'project' => $project]);
  58.         return new JsonResponse($userProject->getRole());
  59.     }
  60.     /**
  61.      * @param EntityManager $manager
  62.      *
  63.      * @throws ORMException
  64.      *
  65.      * @Route("/purge")
  66.      */
  67.     public function purgeAction(EntityManagerInterface $manager): JsonResponse
  68.     {
  69.         $dateExpiration = new \DateTime('now');
  70.         $records $manager->getRepository(Record::class)
  71.             ->createQueryBuilder('r')
  72.             ->where('r.recordTime <= :dateExpiration')
  73.             ->setParameter('dateExpiration'$dateExpiration)
  74.             ->getQuery()
  75.             ->getResult();
  76.         $recordCount count($records);
  77.         $out '';
  78.         while ($record array_shift($records)) {
  79.             $manager->remove($record);
  80.             $product $record->getProduct();
  81.             $manager->flush();
  82.             $leftRecords $manager->getRepository(Record::class)->findBy(['product' => $product]);
  83.             if (== count($leftRecords)) {
  84.                 $manager->remove($product);
  85.                 $manager->flush();
  86.             }
  87.         }
  88.         return new JsonResponse(['record_count' => $recordCount]);
  89.     }
  90. }