Completed
Push — master ( 277c4a...f3eb66 )
by Nicolas
05:22
created

SecuritySubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Smart\AuthenticationBundle\Security\EventSubscriber;
4
5
use Smart\AuthenticationBundle\Security\LastLoginInterface;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
9
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
10
use Symfony\Component\Security\Http\SecurityEvents;
11
use Smart\AuthenticationBundle\Security\Processor\LastLoginProcessor;
12
13
/**
14
 * @author Mathieu Ducrot <[email protected]>
15
 */
16
class SecuritySubscriber implements EventSubscriberInterface
17
{
18
    /**
19
     * @var LastLoginProcessor
20
     */
21
    private $lastLoginProcessor;
22
23
    /**
24
     * @param LastLoginProcessor $lastLoginProcessor
25
     */
26
    public function __construct(LastLoginProcessor $lastLoginProcessor)
27
    {
28
        $this->lastLoginProcessor = $lastLoginProcessor;
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public static function getSubscribedEvents()
35
    {
36
        return [
37
            SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin'
38
        ];
39
    }
40
41
    /**
42
     * @param InteractiveLoginEvent $event
43
     */
44
    public function onInteractiveLogin(InteractiveLoginEvent $event)
45
    {
46
        $token = $event->getAuthenticationToken();
47
48
        if (!$token instanceof TokenInterface) {
49
            return;
50
        }
51
52
        $user = $token->getUser();
53
54
        if (!$user instanceof LastLoginInterface) {
55
            return;
56
        }
57
58
        // Impersonnate user must not update last_login date
59
        if ($event->getRequest()->get('switch_user') !== null) {
60
            return;
61
        }
62
63
        $this->lastLoginProcessor->process($user);
64
    }
65
}
66