src/Controller/ApiController/HistoriquePageController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ApiController;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use JMS\Serializer\SerializerBuilder;
  8. use JMS\Serializer\SerializationContext;
  9. use App\Entity\User;
  10. use App\Service\HistoriquePageService;
  11. /**
  12.  * @Route("/api/historique", name="api_historique")
  13.  */
  14. class HistoriquePageController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/user/{id}/refreshHistorique", name="refresh_historique_for_user", methods={"POST"}, requirements={"id"="\d+"})
  18.      */
  19.     public function refreshHistoriqueForUser(Request $requestUser $userHistoriquePageService $historiqueService)
  20.     {
  21.         $serializer SerializerBuilder::create()->build();
  22.         // Params
  23.         $pathInfo $request->request->get('pathInfo'"");
  24.         $requestParams json_decode($request->request->get('request'""));
  25.         $queryParams json_decode($request->request->get('query'""));
  26.         
  27.         if ($user) {
  28.             // Update des historiques
  29.             $historiqueService->sqlUpdateCurrentPage($user$pathInfo);
  30.             // Users connectés
  31.             $currentUsers $historiqueService->getCurrentPageConnectedUsers($user$pathInfo);
  32.        
  33.             $data $serializer->serialize($currentUsers'json'SerializationContext::create()->setSerializeNull(true));
  34.             $response = new Response($data201);
  35.             $response->headers->set('Content-Type''application/json');
  36.         } else {
  37.             $notFound 
  38.                 [
  39.                 "error" => "USER_NOT_FOUND",
  40.                 "error_description"  => "User cannot be found in database"
  41.                 ]
  42.             ;
  43.             $data $serializer->serialize($notFound'json'SerializationContext::create()->setSerializeNull(true));
  44.             $response = new Response($data404);
  45.             $response->headers->set('Content-Type''application/json');
  46.         }
  47.         return $response;
  48.     }
  49. }