Completed
Push — master ( 82bcf8...197f8d )
by Alexis
03:41
created

UserManipulator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 206
Duplicated Lines 33.5 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 69
loc 206
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 15 1
A activate() 9 9 1
A deactivate() 9 9 1
A changePassword() 9 9 1
A promote() 9 9 1
A demote() 9 9 1
A addRole() 12 12 2
A removeRole() 12 12 2
A findUserByUsernameOrThrowException() 0 10 2
A getRequest() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\SilexUser\Util;
13
14
use AWurth\SilexUser\Event\Events;
15
use AWurth\SilexUser\Event\UserEvent;
16
use AWurth\SilexUser\Model\UserInterface;
17
use AWurth\SilexUser\Model\UserManagerInterface;
18
use InvalidArgumentException;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\RequestStack;
22
23
/**
24
 * Executes some manipulations on the users.
25
 *
26
 * @author Christophe Coevoet <[email protected]>
27
 * @author Luis Cordova <[email protected]>
28
 */
29
class UserManipulator
30
{
31
    /**
32
     * User manager.
33
     *
34
     * @var UserManagerInterface
35
     */
36
    private $userManager;
37
38
    /**
39
     * @var EventDispatcherInterface
40
     */
41
    private $dispatcher;
42
43
    /**
44
     * @var RequestStack
45
     */
46
    private $requestStack;
47
48
    /**
49
     * UserManipulator constructor.
50
     *
51
     * @param UserManagerInterface     $userManager
52
     * @param EventDispatcherInterface $dispatcher
53
     * @param RequestStack             $requestStack
54
     */
55
    public function __construct(UserManagerInterface $userManager, EventDispatcherInterface $dispatcher, RequestStack $requestStack)
56
    {
57
        $this->userManager = $userManager;
58
        $this->dispatcher = $dispatcher;
59
        $this->requestStack = $requestStack;
60
    }
61
62
    /**
63
     * Creates a user and returns it.
64
     *
65
     * @param string $username
66
     * @param string $password
67
     * @param string $email
68
     * @param bool   $active
69
     * @param bool   $superadmin
70
     *
71
     * @return UserInterface
72
     */
73
    public function create($username, $password, $email, $active, $superadmin)
74
    {
75
        $user = $this->userManager->createUser();
76
        $user->setUsername($username);
77
        $user->setEmail($email);
78
        $user->setPlainPassword($password);
79
        $user->setEnabled((bool) $active);
80
        $user->setSuperAdmin((bool) $superadmin);
81
        $this->userManager->updateUser($user);
82
83
        $event = new UserEvent($user, $this->getRequest());
84
        $this->dispatcher->dispatch(Events::USER_CREATED, $event);
85
86
        return $user;
87
    }
88
89
    /**
90
     * Activates the given user.
91
     *
92
     * @param string $username
93
     */
94 View Code Duplication
    public function activate($username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $user = $this->findUserByUsernameOrThrowException($username);
97
        $user->setEnabled(true);
98
        $this->userManager->updateUser($user);
99
100
        $event = new UserEvent($user, $this->getRequest());
101
        $this->dispatcher->dispatch(Events::USER_ACTIVATED, $event);
102
    }
103
104
    /**
105
     * Deactivates the given user.
106
     *
107
     * @param string $username
108
     */
109 View Code Duplication
    public function deactivate($username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $user = $this->findUserByUsernameOrThrowException($username);
112
        $user->setEnabled(false);
113
        $this->userManager->updateUser($user);
114
115
        $event = new UserEvent($user, $this->getRequest());
116
        $this->dispatcher->dispatch(Events::USER_DEACTIVATED, $event);
117
    }
118
119
    /**
120
     * Changes the password for the given user.
121
     *
122
     * @param string $username
123
     * @param string $password
124
     */
125 View Code Duplication
    public function changePassword($username, $password)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $user = $this->findUserByUsernameOrThrowException($username);
128
        $user->setPlainPassword($password);
129
        $this->userManager->updateUser($user);
130
131
        $event = new UserEvent($user, $this->getRequest());
132
        $this->dispatcher->dispatch(Events::USER_PASSWORD_CHANGED, $event);
133
    }
134
135
    /**
136
     * Promotes the given user.
137
     *
138
     * @param string $username
139
     */
140 View Code Duplication
    public function promote($username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $user = $this->findUserByUsernameOrThrowException($username);
143
        $user->setSuperAdmin(true);
144
        $this->userManager->updateUser($user);
145
146
        $event = new UserEvent($user, $this->getRequest());
147
        $this->dispatcher->dispatch(Events::USER_PROMOTED, $event);
148
    }
149
150
    /**
151
     * Demotes the given user.
152
     *
153
     * @param string $username
154
     */
155 View Code Duplication
    public function demote($username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        $user = $this->findUserByUsernameOrThrowException($username);
158
        $user->setSuperAdmin(false);
159
        $this->userManager->updateUser($user);
160
161
        $event = new UserEvent($user, $this->getRequest());
162
        $this->dispatcher->dispatch(Events::USER_DEMOTED, $event);
163
    }
164
165
    /**
166
     * Adds role to the given user.
167
     *
168
     * @param string $username
169
     * @param string $role
170
     *
171
     * @return bool true if role was added, false if user already had the role
172
     */
173 View Code Duplication
    public function addRole($username, $role)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
    {
175
        $user = $this->findUserByUsernameOrThrowException($username);
176
        if ($user->hasRole($role)) {
177
            return false;
178
        }
179
180
        $user->addRole($role);
181
        $this->userManager->updateUser($user);
182
183
        return true;
184
    }
185
186
    /**
187
     * Removes role from the given user.
188
     *
189
     * @param string $username
190
     * @param string $role
191
     *
192
     * @return bool true if role was removed, false if user didn't have the role
193
     */
194 View Code Duplication
    public function removeRole($username, $role)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
    {
196
        $user = $this->findUserByUsernameOrThrowException($username);
197
        if (!$user->hasRole($role)) {
198
            return false;
199
        }
200
201
        $user->removeRole($role);
202
        $this->userManager->updateUser($user);
203
204
        return true;
205
    }
206
207
    /**
208
     * Finds a user by his username and throws an exception if we can't find it.
209
     *
210
     * @param string $username
211
     *
212
     * @throws InvalidArgumentException When user does not exist
213
     *
214
     * @return UserInterface
215
     */
216
    private function findUserByUsernameOrThrowException($username)
217
    {
218
        $user = $this->userManager->findUserByUsername($username);
219
220
        if (!$user) {
221
            throw new InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
222
        }
223
224
        return $user;
225
    }
226
227
    /**
228
     * @return Request
229
     */
230
    private function getRequest()
231
    {
232
        return $this->requestStack->getCurrentRequest();
233
    }
234
}
235