Completed
Push — master ( 96ea8b...1276b9 )
by Alexis
08:08
created

AuthenticationListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 54
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 11 2
1
<?php
2
3
/*
4
 * This file is part of the awurth/silex-user package.
5
 *
6
 * (c) Alexis Wurth <[email protected]>
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 AWurth\Silex\User\EventListener;
13
14
use AWurth\Silex\User\Event\Events;
15
use AWurth\Silex\User\Event\FilterUserResponseEvent;
16
use AWurth\Silex\User\Event\UserEvent;
17
use AWurth\Silex\User\Security\LoginManagerInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\Security\Core\Exception\AccountStatusException;
21
22
class AuthenticationListener implements EventSubscriberInterface
23
{
24
    /**
25
     * @var LoginManagerInterface
26
     */
27
    protected $loginManager;
28
29
    /**
30
     * @var string
31
     */
32
    protected $firewallName;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param LoginManagerInterface $loginManager
38
     * @param string                $firewallName
39
     */
40
    public function __construct(LoginManagerInterface $loginManager, $firewallName)
41
    {
42
        $this->loginManager = $loginManager;
43
        $this->firewallName = $firewallName;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public static function getSubscribedEvents()
50
    {
51
        return [
52
            Events::REGISTRATION_COMPLETED => 'authenticate',
53
            Events::REGISTRATION_CONFIRMED => 'authenticate'
54
        ];
55
    }
56
57
    /**
58
     * Authenticates the user.
59
     *
60
     * @param FilterUserResponseEvent  $event
61
     * @param string                   $eventName
62
     * @param EventDispatcherInterface $dispatcher
63
     */
64
    public function authenticate(FilterUserResponseEvent $event, $eventName, EventDispatcherInterface $dispatcher)
0 ignored issues
show
Unused Code introduced by
The parameter $eventName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        try {
67
            $this->loginManager->logInUser($this->firewallName, $event->getUser(), $event->getResponse());
68
69
            $dispatcher->dispatch(Events::SECURITY_IMPLICIT_LOGIN, new UserEvent($event->getUser(), $event->getRequest()));
70
        } catch (AccountStatusException $e) {
71
            // We simply do not authenticate users which do not pass the user
72
            // checker (not enabled, expired, etc.).
73
        }
74
    }
75
}
76