Completed
Pull Request — develop (#113)
by Boy
07:07 queued 03:37
created

HasPersonalRaLocationsListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A onKernelRequest() 0 17 3
A getSubscribedEvents() 0 7 1
1
<?php
2
3
namespace Surfnet\StepupRa\RaBundle\EventListener;
4
5
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
6
use Surfnet\StepupRa\RaBundle\Security\Authentication\Token\SamlToken;
7
use Surfnet\StepupRa\RaBundle\Service\InstitutionWithPersonalRaLocationsService;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
use Symfony\Component\Security\Core\SecurityContext;
12
use Twig_Environment;
13
14
final class HasPersonalRaLocationsListener implements EventSubscriberInterface
15
{
16
    /**
17
     * @var Twig_Environment
18
     */
19
    private $twig;
20
21
    /**
22
     * @var InstitutionWithPersonalRaLocationsService
23
     */
24
    private $service;
25
26
    /**
27
     * @var SecurityContext
28
     */
29
    private $securityContext;
30
31
    public function __construct(
32
        Twig_Environment $twig,
33
        InstitutionWithPersonalRaLocationsService $institutionWithPersonalRaLocationsService,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $institutionWithPersonalRaLocationsService exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
34
        SecurityContext $securityContext
35
    ) {
36
        $this->twig = $twig;
37
        $this->service = $institutionWithPersonalRaLocationsService;
38
        $this->securityContext = $securityContext;
39
    }
40
41
    public function onKernelRequest(GetResponseEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        $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...
44
45
        if (!$token instanceof SamlToken) {
46
            return;
47
        }
48
49
        $user = $token->getUser();
50
51
        if (!$user instanceof Identity) {
52
            return;
53
        }
54
55
        $hasPersonalRaLocations = $this->service->institutionHasPersonalRaLocations($user->institution);
56
        $this->twig->addGlobal('hasPersonalRaLocations', $hasPersonalRaLocations);
57
    }
58
59
    public static function getSubscribedEvents()
60
    {
61
        return [
62
            // The firewall, which makes the token available, listens at P8
63
            KernelEvents::REQUEST => ['onKernelRequest', 0],
64
        ];
65
    }
66
}
67