| Conditions | 10 |
| Paths | 8 |
| Total Lines | 76 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 142 | public function login( |
||
| 143 | array $credentials = [], |
||
| 144 | bool $remeberMe = false, |
||
| 145 | bool $withPassword = true |
||
| 146 | ): array { |
||
| 147 | if (!isset($credentials['username'])) { |
||
| 148 | throw new MissingCredentialsException( |
||
| 149 | 'Missing username information', |
||
| 150 | 401 |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | |||
| 154 | if ($withPassword && !isset($credentials['password'])) { |
||
| 155 | throw new MissingCredentialsException( |
||
| 156 | 'Missing password information', |
||
| 157 | 401 |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | |||
| 161 | $username = $credentials['username']; |
||
| 162 | $password = $credentials['password'] ?? ''; |
||
| 163 | |||
| 164 | $user = $this->getUserEntity($username, $password, $withPassword); |
||
| 165 | if ($user === null) { |
||
| 166 | throw new AccountNotFoundException( |
||
| 167 | sprintf( |
||
| 168 | 'Can not find the user [%s]', |
||
| 169 | $username |
||
| 170 | ), |
||
| 171 | 401 |
||
| 172 | ); |
||
| 173 | } elseif ($user->status === UserStatus::LOCKED) { |
||
| 174 | throw new AccountLockedException( |
||
| 175 | sprintf('User [%s] is locked', $username), |
||
| 176 | 401 |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($withPassword && $this->hash->verify($password, $user->password) === false) { |
||
| 181 | $this->app->dispatch(new AuthInvalidPasswordEvent($user)); |
||
| 182 | |||
| 183 | throw new InvalidCredentialsException( |
||
| 184 | sprintf('Invalid credentials for user [%s]', $username), |
||
| 185 | 401 |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | |||
| 189 | $permissions = []; |
||
| 190 | $roles = $user->roles; |
||
| 191 | foreach ($roles as $role) { |
||
| 192 | $rolePermissions = $role->permissions; |
||
| 193 | foreach ($rolePermissions as $permission) { |
||
| 194 | $permissions[] = $permission->code; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | $data = [ |
||
| 199 | 'user' => [ |
||
| 200 | 'id' => $user->id, |
||
| 201 | 'username' => $user->username, |
||
| 202 | 'lastname' => $user->lastname, |
||
| 203 | 'firstname' => $user->firstname, |
||
| 204 | 'email' => $user->email, |
||
| 205 | 'status' => $user->status, |
||
| 206 | ], |
||
| 207 | 'permissions' => $permissions, |
||
| 208 | ]; |
||
| 209 | |||
| 210 | $loginData = array_merge($data, $this->getUserData($user)); |
||
| 211 | |||
| 212 | $this->session->set('auth', $loginData); |
||
| 213 | |||
| 214 | // Inform the system that the user just login successfully |
||
| 215 | $this->app->dispatch(new AuthLoginEvent($user)); |
||
| 216 | |||
| 217 | return $loginData; |
||
| 218 | } |
||
| 269 |