Completed
Push — master ( 6f429a...4083cc )
by Alexis
06:02
created

AuthenticationListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 7 1
A authenticate() 0 9 2
1
<?php
2
3
namespace AWurth\SilexUser\EventListener;
4
5
use AWurth\SilexUser\Event\Events;
6
use AWurth\SilexUser\Event\FilterUserResponseEvent;
7
use AWurth\SilexUser\Security\LoginManager;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\Security\Core\Exception\AccountStatusException;
11
12
class AuthenticationListener implements EventSubscriberInterface
13
{
14
    /**
15
     * @var LoginManager
16
     */
17
    protected $loginManager;
18
19
    /**
20
     * @var string
21
     */
22
    protected $firewallName;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param LoginManager $loginManager
28
     * @param string       $firewallName
29
     */
30
    public function __construct(LoginManager $loginManager, $firewallName)
31
    {
32
        $this->loginManager = $loginManager;
33
        $this->firewallName = $firewallName;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public static function getSubscribedEvents()
40
    {
41
        return [
42
            Events::REGISTRATION_COMPLETED => 'authenticate',
43
            Events::REGISTRATION_CONFIRMED => 'authenticate'
44
        ];
45
    }
46
47
    /**
48
     * Authenticates the user.
49
     *
50
     * @param FilterUserResponseEvent $event
51
     */
52
    public function authenticate(FilterUserResponseEvent $event)
53
    {
54
        try {
55
            $this->loginManager->logInUser($this->firewallName, $event->getUser(), $event->getResponse());
56
        } catch (AccountStatusException $e) {
57
            // We simply do not authenticate users which do not pass the user
58
            // checker (not enabled, expired, etc.).
59
        }
60
    }
61
}
62