LogoutUserListener::onChangePassword()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 2
b 0
f 0
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminSecurityBundle\EventListener;
13
14
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
15
use FSi\Bundle\AdminSecurityBundle\Event\ChangePasswordEvent;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
20
class LogoutUserListener implements EventSubscriberInterface
21
{
22
    /**
23
     * @var RequestStack
24
     */
25
    private $requestStack;
26
27
    /**
28
     * @var TokenStorageInterface
29
     */
30
    private $tokenStorage;
31
32
    public function __construct(RequestStack $requestStack, TokenStorageInterface $tokenStorage)
33
    {
34
        $this->requestStack = $requestStack;
35
        $this->tokenStorage = $tokenStorage;
36
    }
37
38
    public static function getSubscribedEvents(): array
39
    {
40
        return [
41
            AdminSecurityEvents::CHANGE_PASSWORD => 'onChangePassword'
42
        ];
43
    }
44
45
    public function onChangePassword(ChangePasswordEvent $event): void
46
    {
47
        $token = $this->tokenStorage->getToken();
48
        if ($token && $token->getUser() === $event->getUser()) {
49
            $this->requestStack->getMasterRequest()->getSession()->invalidate();
50
            $this->tokenStorage->setToken(null);
51
        }
52
    }
53
}
54