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

PersistDoctrineUserListener   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 106
rs 10
c 1
b 0
f 0
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A onUserCreated() 0 3 1
A onPromoteUser() 0 3 1
A onChangePassword() 0 3 1
A flushUserObjectManager() 0 7 2
A onDeactivation() 0 3 1
A onActivation() 0 3 1
A onResetPasswordRequest() 0 3 1
A onInteractiveLogin() 0 3 1
A __construct() 0 3 1
A getSubscribedEvents() 0 11 1
A onDemoteUser() 0 3 1
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 Doctrine\Bundle\DoctrineBundle\Registry;
13
use Doctrine\Common\Persistence\ObjectManager;
14
use FSi\Bundle\AdminSecurityBundle\Event\ActivationEvent;
15
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
16
use FSi\Bundle\AdminSecurityBundle\Event\ChangePasswordEvent;
17
use FSi\Bundle\AdminSecurityBundle\Event\ResetPasswordRequestEvent;
18
use FSi\Bundle\AdminSecurityBundle\Event\UserEvent;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
21
use Symfony\Component\Security\Http\SecurityEvents;
22
23
class PersistDoctrineUserListener implements EventSubscriberInterface
24
{
25
    /**
26
     * @var \Doctrine\Bundle\DoctrineBundle\Registry
27
     */
28
    private $registry;
29
30
    /**
31
     * @param Registry $registry
32
     */
33
    function __construct(Registry $registry)
34
    {
35
        $this->registry = $registry;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function getSubscribedEvents()
42
    {
43
        return [
44
            AdminSecurityEvents::CHANGE_PASSWORD => 'onChangePassword',
45
            AdminSecurityEvents::RESET_PASSWORD_REQUEST => 'onResetPasswordRequest',
46
            AdminSecurityEvents::ACTIVATION => 'onActivation',
47
            AdminSecurityEvents::DEACTIVATION => 'onDeactivation',
48
            AdminSecurityEvents::USER_CREATED => 'onUserCreated',
49
            AdminSecurityEvents::PROMOTE_USER => 'onPromoteUser',
50
            AdminSecurityEvents::DEMOTE_USER => 'onDemoteUser',
51
            SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin'
52
        ];
53
    }
54
55
    /**
56
     * @param ActivationEvent $event
57
     */
58
    public function onActivation(ActivationEvent $event)
59
    {
60
        $this->flushUserObjectManager($event->getUser());
61
    }
62
63
    /**
64
     * @param ActivationEvent $event
65
     */
66
    public function onDeactivation(ActivationEvent $event)
67
    {
68
        $this->flushUserObjectManager($event->getUser());
69
    }
70
71
    /**
72
     * @param ChangePasswordEvent $event
73
     */
74
    public function onChangePassword(ChangePasswordEvent $event)
75
    {
76
        $this->flushUserObjectManager($event->getUser());
77
    }
78
79
    /**
80
     * @param ResetPasswordRequestEvent $event
81
     */
82
    public function onResetPasswordRequest(ResetPasswordRequestEvent $event)
83
    {
84
        $this->flushUserObjectManager($event->getUser());
85
    }
86
87
    /**
88
     * @param UserEvent $event
89
     */
90
    public function onUserCreated(UserEvent $event)
91
    {
92
        $this->flushUserObjectManager($event->getUser());
93
    }
94
95
    /**
96
     * @param UserEvent $event
97
     */
98
    public function onPromoteUser(UserEvent $event)
99
    {
100
        $this->flushUserObjectManager($event->getUser());
101
    }
102
103
    /**
104
     * @param UserEvent $event
105
     */
106
    public function onDemoteUser(UserEvent $event)
107
    {
108
        $this->flushUserObjectManager($event->getUser());
109
    }
110
111
    /**
112
     * @param InteractiveLoginEvent $event
113
     */
114
    public function onInteractiveLogin(InteractiveLoginEvent $event)
115
    {
116
        $this->flushUserObjectManager($event->getAuthenticationToken()->getUser());
117
    }
118
119
    /**
120
     * @param object $user
121
     */
122
    private function flushUserObjectManager($user)
123
    {
124
        $objectManager = $this->registry->getManagerForClass(get_class($user));
125
126
        if ($objectManager instanceof ObjectManager) {
127
            $objectManager->persist($user);
128
            $objectManager->flush();
129
        }
130
    }
131
}
132