Completed
Push — master ( d67956...a656c9 )
by Piotr
11s
created

LogoutUserListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
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
namespace FSi\Bundle\AdminSecurityBundle\EventListener;
11
12
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
13
use FSi\Bundle\AdminSecurityBundle\Event\ChangePasswordEvent;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
18
class LogoutUserListener implements EventSubscriberInterface
19
{
20
    /**
21
     * @var RequestStack
22
     */
23
    private $requestStack;
24
25
    /**
26
     * @var TokenStorageInterface
27
     */
28
    private $tokenStorage;
29
30
    /**
31
     * @param RequestStack $requestStack
32
     * @param TokenStorageInterface $tokenStorage
33
     */
34
    public function __construct(RequestStack $requestStack, TokenStorageInterface $tokenStorage)
35
    {
36
        $this->requestStack = $requestStack;
37
        $this->tokenStorage = $tokenStorage;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public static function getSubscribedEvents()
44
    {
45
        return [
46
            AdminSecurityEvents::CHANGE_PASSWORD => 'onChangePassword'
47
        ];
48
    }
49
50
    /**
51
     * @param ChangePasswordEvent $event
52
     */
53
    public function onChangePassword(ChangePasswordEvent $event)
54
    {
55
        $token = $this->tokenStorage->getToken();
56
        if ($token && $token->getUser() === $event->getUser()) {
57
            $this->requestStack->getMasterRequest()->getSession()->invalidate();
58
            $this->tokenStorage->setToken(null);
59
        }
60
    }
61
}
62