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

KernelListener::onKernelResponse()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 11
nc 3
nop 1
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