Skip to content

Commit 7f0abca

Browse files
committed
Extracting logic to a method in UserManipulator
1 parent ea2ead6 commit 7f0abca

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

Util/UserManipulator.php

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ public function create($username, $password, $email, $active, $superadmin)
6464
*/
6565
public function activate($username)
6666
{
67-
$user = $this->userManager->findUserByUsername($username);
68-
69-
if (!$user) {
70-
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
71-
}
67+
$user = $this->findUserByUsernameOrThrowException($username);
7268
$user->setEnabled(true);
7369
$this->userManager->updateUser($user);
7470
}
@@ -80,11 +76,7 @@ public function activate($username)
8076
*/
8177
public function deactivate($username)
8278
{
83-
$user = $this->userManager->findUserByUsername($username);
84-
85-
if (!$user) {
86-
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
87-
}
79+
$user = $this->findUserByUsernameOrThrowException($username);
8880
$user->setEnabled(false);
8981
$this->userManager->updateUser($user);
9082
}
@@ -97,11 +89,7 @@ public function deactivate($username)
9789
*/
9890
public function changePassword($username, $password)
9991
{
100-
$user = $this->userManager->findUserByUsername($username);
101-
102-
if (!$user) {
103-
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
104-
}
92+
$user = $this->findUserByUsernameOrThrowException($username);
10593
$user->setPlainPassword($password);
10694
$this->userManager->updateUser($user);
10795
}
@@ -113,11 +101,7 @@ public function changePassword($username, $password)
113101
*/
114102
public function promote($username)
115103
{
116-
$user = $this->userManager->findUserByUsername($username);
117-
118-
if (!$user) {
119-
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
120-
}
104+
$user = $this->findUserByUsernameOrThrowException($username);
121105
$user->setSuperAdmin(true);
122106
$this->userManager->updateUser($user);
123107
}
@@ -129,11 +113,7 @@ public function promote($username)
129113
*/
130114
public function demote($username)
131115
{
132-
$user = $this->userManager->findUserByUsername($username);
133-
134-
if (!$user) {
135-
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
136-
}
116+
$user = $this->findUserByUsernameOrThrowException($username);
137117
$user->setSuperAdmin(false);
138118
$this->userManager->updateUser($user);
139119
}
@@ -148,11 +128,7 @@ public function demote($username)
148128
*/
149129
public function addRole($username, $role)
150130
{
151-
$user = $this->userManager->findUserByUsername($username);
152-
153-
if (!$user) {
154-
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
155-
}
131+
$user = $this->findUserByUsernameOrThrowException($username);
156132
if ($user->hasRole($role)) {
157133
return false;
158134
}
@@ -161,6 +137,7 @@ public function addRole($username, $role)
161137

162138
return true;
163139
}
140+
164141
/**
165142
* Removes role from the given user.
166143
*
@@ -171,11 +148,7 @@ public function addRole($username, $role)
171148
*/
172149
public function removeRole($username, $role)
173150
{
174-
$user = $this->userManager->findUserByUsername($username);
175-
176-
if (!$user) {
177-
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
178-
}
151+
$user = $this->findUserByUsernameOrThrowException($username);
179152
if (!$user->hasRole($role)) {
180153
return false;
181154
}
@@ -184,4 +157,22 @@ public function removeRole($username, $role)
184157

185158
return true;
186159
}
160+
161+
/**
162+
* Finds a user by his username and throws an exception if we can't find it.
163+
*
164+
* @param string $username
165+
*
166+
* @return UserInterface
167+
*/
168+
private function findUserByUsernameOrThrowException($username)
169+
{
170+
$user = $this->userManager->findUserByUsername($username);
171+
172+
if (!$user) {
173+
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
174+
}
175+
176+
return $user;
177+
}
187178
}

0 commit comments

Comments
 (0)