Completed
Push — master ( ad1dd8...443a61 )
by Benjamin
15:24 queued 03:47
created

KernelListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B onKernelResponse() 0 15 5
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Listener;
4
5
use Symfony\Component\HttpFoundation\Cookie;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
8
use Symfony\Component\Security\Core\SecurityContext;
9
10
class KernelListener
11
{
12
    private $requestStack;
13
    private $securityContext;
14
    private $secret;
15
16
    public function __construct(RequestStack $requestStack, SecurityContext $securityContext, $secret)
17
    {
18
        $this->securityContext = $securityContext;
19
        $this->secret = $secret;
20
        $this->requestStack = $requestStack;
21
    }
22
23
    public function onKernelResponse(FilterResponseEvent $event)
24
    {
25
        $response = $event->getResponse();
26
        $request = $this->requestStack->getMasterRequest();
27
        $route = $request->attributes->get('_route');
28
        $cookies = $request->cookies;
29
        $token = $this->securityContext->getToken();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Securi...rityContext::getToken() has been deprecated with message: since version 2.6, to be removed in 3.0. Use TokenStorageInterface::getToken() instead. {@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
30
31
        if ($token !== null && $this->securityContext->isGranted('ROLE_ADMIN')) {
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Securi...ityContext::isGranted() has been deprecated with message: since version 2.6, to be removed in 3.0. Use AuthorizationCheckerInterface::isGranted() instead. {@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
32
            $cookie = new Cookie('can_edit', hash('sha256', 'can_edit'.$this->secret), 0, '/', null, false, false);
33
            $response->headers->setCookie($cookie);
34
        } else if (!in_array($route, ['_profiler', '_wdt']) && $cookies->has('can_edit2')) {
35
            $response->headers->clearCookie('can_edit', '/');
36
        }
37
    }
38
}
39