src/Controller/FavouritesController.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\FavouritesService;
  4. use MLDev\BaseBundle\Controller\FrontController;
  5. use MLDev\BaseBundle\Service\PageManager;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. /**
  11.  * Class FavouritesController
  12.  * @package MLDev\OrderBundle\Controller
  13.  */
  14. class FavouritesController extends FrontController
  15. {
  16.     /**
  17.      * @var Request
  18.      */
  19.     private $request;
  20.     /**
  21.      * @var FavouritesService
  22.      */
  23.     private $favouritesService;
  24.     /**
  25.      * BasketController constructor.
  26.      */
  27.     public function __construct(FavouritesService $favouritesServiceRequestStack $requestStack)
  28.     {
  29.         $this->request $requestStack->getCurrentRequest();
  30.         $this->favouritesService $favouritesService;
  31.     }
  32.     /**
  33.      * @Route("/favourites", methods={"GET"}, name="mldev-front-favourites-index")
  34.      * _IsGranted("ROLE_USER_ACCOUNT")
  35.      */
  36.     public function indexAction(PageManager $pageManager)
  37.     {
  38.         $entity $pageManager->createVirtualEntity('Избранное''/favourites');
  39.         return $this->renderCustomTemplate($entity'favourites/index.html.twig', [
  40.             'user' => $this->getUser(),
  41.             'favouritesItems' => $this->favouritesService->getFavouritesItems(),
  42.         ]);
  43.     }
  44.     /**
  45.      * @Route("/favourites/set", methods={"POST"}, name="mldev-front-favourites-set", defaults={"_format": "json"})
  46.      * @IsGranted("ROLE_USER_ACCOUNT")
  47.      */
  48.     public function setAction()
  49.     {
  50.         $data json_decode($this->request->getContent(), true);
  51.         $id = (isset($data['id']) ? (integer)$data['id'] : 0);
  52.         return $this->json(
  53.             $this->favouritesService->getStorage()->set($id$id)
  54.         );
  55.     }
  56.     /**
  57.      * @Route("/favourites/remove", methods={"POST"}, name="mldev-front-favourites-remove", defaults={"_format": "json"})
  58.      * @IsGranted("ROLE_USER_ACCOUNT")
  59.      */
  60.     public function removeAction()
  61.     {
  62.         $data json_decode($this->request->getContent(), true);
  63.         $id = (isset($data['id']) ? (integer)$data['id'] : 0);
  64.         return $this->json(
  65.             $this->favouritesService->getStorage()->remove($id)
  66.         );
  67.     }
  68.     /**
  69.      * @Route("/favourites/clear", methods={"POST"}, name="mldev-front-favourites-clear", defaults={"_format": "json"})
  70.      * @IsGranted("ROLE_USER_ACCOUNT")
  71.      */
  72.     public function clearAction()
  73.     {
  74.         $this->favouritesService->clearFavourites();
  75.         return $this->json([]);
  76.     }
  77. }