-
Notifications
You must be signed in to change notification settings - Fork 2
/
RedirectionListener.php
47 lines (35 loc) · 1.46 KB
/
RedirectionListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Ecommerce\FrontBundle\EventListener;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class RedirectionListener
{
protected $session;
protected $securityContext;
protected $route;
public function __construct(ContainerInterface $containerInterface)
{
$this->session = $containerInterface->get('session');
$this->securityContext = $containerInterface->get('security.token_storage');
$this->route = $containerInterface->get('router');
}
/**
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
$route = $event->getRequest()->attributes->get('_route');
if($route == 'panier_livraison' || $route == 'panier_validation'){
if ($this->session->has('panier')) {
if (count($this->session->get('panier')) == 0)
$event->setResponse(new RedirectResponse($this->route->generate('panier_index')));
}
if (!is_object($this->securityContext->getToken()->getUser())) {
$this->session->getFlashBag()->add('notification', 'Vous devez vous identifier');
$event->setResponse(new RedirectResponse($this->route->generate('fos_user_security_login')));
}
}
}
}