Completed
Pull Request — master (#91)
by Piotr
03:42
created

SetEmailAsUsernameListener   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
wmc 3
lcom 0
cbo 2
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 6 1
A setEmailAsUsername() 0 9 2
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\UserEvent;
14
use FSi\Bundle\AdminSecurityBundle\Security\User\UserInterface;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17
class SetEmailAsUsernameListener implements EventSubscriberInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public static function getSubscribedEvents()
23
    {
24
        return [
25
            AdminSecurityEvents::USER_CREATED => 'setEmailAsUsername'
26
        ];
27
    }
28
29
    /**
30
     * @param UserEvent $event
31
     *
32
     * @throws \Exception
33
     */
34
    public function setEmailAsUsername(UserEvent $event)
35
    {
36
        $user = $event->getUser();
37
        if (!$user instanceof UserInterface) {
38
            throw new \Exception('User entity should implement UserInterface');
39
        }
40
41
        $user->setUsername($user->getEmail());
42
    }
43
}
44