Completed
Push — master ( cd5eb5...60c610 )
by WEBEWEB
08:13
created

KernelEventListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 4
dl 0
loc 91
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRequest() 0 3 1
A getUser() 0 12 4
A onKernelRequest() 0 8 1
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Bundle\BootstrapBundle\EventListener;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
use WBW\Bundle\BootstrapBundle\Provider\ProvidersManager;
19
20
/**
21
 * Kernel event listener.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\BootstrapBundle\EventListener
25
 * @final
26
 */
27
final class KernelEventListener {
28
29
    /**
30
     * Service name.
31
     *
32
     * @var string
33
     */
34
    const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.eventlistener.kernel";
35
36
    /**
37
     * Providers manager.
38
     *
39
     * @var ProvidersManager
40
     */
41
    private $providersManager;
42
43
    /**
44
     * Request.
45
     *
46
     * @var Request
47
     */
48
    private static $request;
49
50
    /**
51
     * Token storage.
52
     *
53
     * @var TokenStorageInterface
54
     */
55
    private $tokenStorage;
56
57
    /**
58
     * User.
59
     *
60
     * @var UserInterface
61
     */
62
    private $user;
63
64
    /**
65
     * Constructor.
66
     *
67
     * @param type $tokenStorage The token storage service.
68
     * @param ProvidersManager The providers manager service.
69
     */
70
    public function __construct(TokenStorageInterface $tokenStorage, ProvidersManager $providersManager) {
71
        $this->providersManager = $providersManager;
72
        $this->tokenStorage     = $tokenStorage;
73
    }
74
75
    /**
76
     * Get the request.
77
     *
78
     * @return Request Returns the request.
79
     */
80
    public function getRequest() {
81
        return self::$request;
82
    }
83
84
    /**
85
     * Get the current user.
86
     *
87
     * @return UserInterface Returns the current user in case of success, null otherwise.
88
     */
89
    public function getUser() {
90
        if (null === $this->user) {
91
            $token = $this->tokenStorage->getToken();
92
            if (null !== $token) {
93
                $this->user = $token->getUser();
94
            }
95
        }
96
        if (true === ($this->user instanceof UserInterface)) {
97
            return $this->user;
98
        }
99
        return null;
100
    }
101
102
    /**
103
     * On kernel request.
104
     *
105
     * @param GetResponseEvent $event The event.
106
     * @return void
107
     */
108
    public function onKernelRequest(GetResponseEvent $event) {
109
110
        // Initialize the request.
111
        self::$request = $event->getRequest();
112
113
        // Register the providers.
114
        $this->providersManager->register();
115
    }
116
117
}
118