| Conditions | 8 |
| Paths | 7 |
| Total Lines | 51 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 125 | public function login(array $credentials = [], bool $remeberMe = false): bool |
||
| 126 | { |
||
| 127 | if (!isset($credentials['username']) || !isset($credentials['password'])) { |
||
| 128 | throw new MissingCredentialsException( |
||
| 129 | 'Missing username or password information', |
||
| 130 | 401 |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | $username = $credentials['username']; |
||
| 134 | $password = $credentials['password']; |
||
| 135 | $user = $this->userRepository |
||
| 136 | ->with('roles.permissions') |
||
| 137 | ->findBy(['username' => $username]); |
||
| 138 | if (!$user) { |
||
| 139 | throw new AccountNotFoundException('Can not find the user with the given information', 401); |
||
| 140 | } elseif ($user->status == 0) { |
||
| 141 | throw new AccountLockedException( |
||
| 142 | 'User is locked', |
||
| 143 | 401 |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | |||
| 147 | $hash = new BcryptHash(); |
||
| 148 | if (!$hash->verify($password, $user->password)) { |
||
| 149 | throw new InvalidCredentialsException( |
||
| 150 | 'Invalid credentials', |
||
| 151 | 401 |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | $permissions = []; |
||
| 156 | |||
| 157 | $roles = $user->roles; |
||
| 158 | foreach ($roles as $role) { |
||
| 159 | $rolePermissions = $role->permissions; |
||
| 160 | foreach ($rolePermissions as $permission) { |
||
| 161 | $permissions[] = $permission->code; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | $data = [ |
||
| 166 | 'id' => $user->id, |
||
| 167 | 'username' => $user->username, |
||
| 168 | 'lastname' => $user->lastname, |
||
| 169 | 'firstname' => $user->firstname, |
||
| 170 | 'permissions' => array_unique($permissions), |
||
| 171 | ]; |
||
| 172 | |||
| 173 | $this->session->set('user', $data); |
||
| 174 | |||
| 175 | return $this->isLogged(); |
||
| 176 | } |
||
| 186 |