DebugListener::onKernelRequest()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the AfDontTranslateBundle package
5
 *
6
 * (c) Antoine Froger <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code
10
 */
11
12
namespace Af\Bundle\DontTranslateBundle\EventListener;
13
14
use Af\Bundle\DontTranslateBundle\ActivationMode\ActivationModeFactory;
15
use Af\Bundle\DontTranslateBundle\Translation\Translator;
16
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17
use Symfony\Component\Security\Core\SecurityContextInterface;
18
19
/**
20
 * Class DebugListener
21
 * 
22
 * @package Af\Bundle\DontTranslateBundle\EventListener
23
 * 
24
 * @author Antoine Froger <[email protected]>
25
 */
26
class DebugListener
27
{
28
    /** @var SecurityContextInterface */
29
    private $securityContext;
30
31
    /** @var Translator */
32
    private $translator;
33
34
    /** @var ActivationModeFactory */
35
    private $activationModeFactory;
36
37
    /** @var string */
38
    private $activationMode;
39
40
    /** @var string */
41
    private $requestParam;
42
43
    /** @var array */
44
    private $authorizedRoles;
45
46
    /**
47
     * Constructor
48
     *
49
     * @param SecurityContextInterface $securityContext
50
     * @param Translator               $translator
51
     * @param ActivationModeFactory    $activationModeFactory
52
     * @param string                   $requestParam
53
     * @param string                   $activationMode
54
     * @param array                    $authorizedRoles
55
     */
56
    public function __construct(
57
        SecurityContextInterface $securityContext,
58
        Translator $translator,
59
        ActivationModeFactory $activationModeFactory,
60
        $activationMode,
61
        $requestParam,
62
        array $authorizedRoles = array()
63
    ) {
64
        $this->securityContext       = $securityContext;
65
        $this->translator            = $translator;
66
        $this->activationModeFactory = $activationModeFactory;
67
        $this->activationMode        = $activationMode;
68
        $this->requestParam          = $requestParam;
69
        $this->authorizedRoles       = $authorizedRoles;
70
    }
71
72
    /**
73
     * @param GetResponseEvent $event
74
     */
75
    public function onKernelRequest(GetResponseEvent $event)
76
    {
77
        if (null === $this->securityContext->getToken()) {
78
            return;
79
        }
80
81
        $request = $event->getRequest();
82
83
        $activationMode = $this->activationModeFactory->build($this->activationMode);
84
85
        if ($activationMode->isEnabled($this->requestParam, $request) && $this->isGranted()) {
86
            $this->translator->setDebug(true);
87
        }
88
    }
89
90
    /**
91
     * Checks if the access is possible or not
92
     *
93
     * @return bool
94
     */
95
    private function isGranted()
96
    {
97
        if (empty($this->authorizedRoles)) {
98
            return true;
99
        }
100
101
        foreach ($this->authorizedRoles as $roleName) {
102
            if ($this->securityContext->isGranted($roleName)) {
103
                return true;
104
            }
105
        }
106
107
        return false;
108
    }
109
}
110