<?php
namespace App\Controller;
use App\Service\FavouritesService;
use MLDev\BaseBundle\Controller\FrontController;
use MLDev\BaseBundle\Service\PageManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class FavouritesController
* @package MLDev\OrderBundle\Controller
*/
class FavouritesController extends FrontController
{
/**
* @var Request
*/
private $request;
/**
* @var FavouritesService
*/
private $favouritesService;
/**
* BasketController constructor.
*/
public function __construct(FavouritesService $favouritesService, RequestStack $requestStack)
{
$this->request = $requestStack->getCurrentRequest();
$this->favouritesService = $favouritesService;
}
/**
* @Route("/favourites", methods={"GET"}, name="mldev-front-favourites-index")
* _IsGranted("ROLE_USER_ACCOUNT")
*/
public function indexAction(PageManager $pageManager)
{
$entity = $pageManager->createVirtualEntity('Избранное', '/favourites');
return $this->renderCustomTemplate($entity, 'favourites/index.html.twig', [
'user' => $this->getUser(),
'favouritesItems' => $this->favouritesService->getFavouritesItems(),
]);
}
/**
* @Route("/favourites/set", methods={"POST"}, name="mldev-front-favourites-set", defaults={"_format": "json"})
* @IsGranted("ROLE_USER_ACCOUNT")
*/
public function setAction()
{
$data = json_decode($this->request->getContent(), true);
$id = (isset($data['id']) ? (integer)$data['id'] : 0);
return $this->json(
$this->favouritesService->getStorage()->set($id, $id)
);
}
/**
* @Route("/favourites/remove", methods={"POST"}, name="mldev-front-favourites-remove", defaults={"_format": "json"})
* @IsGranted("ROLE_USER_ACCOUNT")
*/
public function removeAction()
{
$data = json_decode($this->request->getContent(), true);
$id = (isset($data['id']) ? (integer)$data['id'] : 0);
return $this->json(
$this->favouritesService->getStorage()->remove($id)
);
}
/**
* @Route("/favourites/clear", methods={"POST"}, name="mldev-front-favourites-clear", defaults={"_format": "json"})
* @IsGranted("ROLE_USER_ACCOUNT")
*/
public function clearAction()
{
$this->favouritesService->clearFavourites();
return $this->json([]);
}
}