Complex classes like JwtGuard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JwtGuard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class JwtGuard implements Guard |
||
| 22 | { |
||
| 23 | |||
| 24 | use GuardHelpers; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $token; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $isTokenRefreshable = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $isTokenPresent = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var JwtService |
||
| 43 | */ |
||
| 44 | protected $jwtService; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Request |
||
| 48 | */ |
||
| 49 | protected $request; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The event dispatcher instance. |
||
| 53 | * |
||
| 54 | * @var Dispatcher |
||
| 55 | */ |
||
| 56 | protected $events; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Indicates if the logout method has been called. |
||
| 60 | * |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | protected $loggedOut = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * JwtGuard constructor |
||
| 67 | * |
||
| 68 | * @param UserProvider $provider |
||
| 69 | * @param JwtService $jwtService |
||
| 70 | * @param Request|null $request |
||
| 71 | */ |
||
| 72 | public function __construct( |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get the currently authenticated user. |
||
| 84 | * |
||
| 85 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
| 86 | */ |
||
| 87 | public function user() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Retrieve the user by the given payload. |
||
| 116 | * |
||
| 117 | * @param string $token |
||
| 118 | * @return AuthenticatableContract|null |
||
| 119 | * @throws InaccessibleException |
||
| 120 | * @throws MalformedException |
||
| 121 | * @throws TokenExpiredException |
||
| 122 | * @throws InvalidTokenException |
||
| 123 | */ |
||
| 124 | protected function getUserByToken($token) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Validate a user's credentials. |
||
| 138 | * |
||
| 139 | * @param array $credentials |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function validate(array $credentials = []) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Attempt to authenticate a user using the given credentials. |
||
| 149 | * |
||
| 150 | * @param array $credentials |
||
| 151 | * @param bool $login |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function attempt(array $credentials = [], $login = true) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Determine if the user matches the credentials. |
||
| 180 | * |
||
| 181 | * @param mixed $user |
||
| 182 | * @param array $credentials |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | protected function hasValidCredentials($user, $credentials) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Fire the attempt event with the arguments. |
||
| 192 | * |
||
| 193 | * @param array $credentials |
||
| 194 | * @param bool $login |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | protected function fireAttemptEvent(array $credentials, $login) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Register an authentication attempt event listener. |
||
| 208 | * |
||
| 209 | * @param mixed $callback |
||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | public function attempting($callback) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Log a user into the application. |
||
| 221 | * |
||
| 222 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
| 223 | * @return void |
||
| 224 | */ |
||
| 225 | public function login(AuthenticatableContract $user) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * generateTokenForUser method |
||
| 246 | * |
||
| 247 | * @param string $token |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | protected function refreshTokenForUser($token) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Fire the login event if the dispatcher is set. |
||
| 263 | * |
||
| 264 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
| 265 | * @param bool $remember |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | protected function fireLoginEvent($user, $remember = false) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Log the given user ID into the application. |
||
| 277 | * |
||
| 278 | * @param mixed $id |
||
| 279 | * @return \Illuminate\Contracts\Auth\Authenticatable |
||
| 280 | */ |
||
| 281 | public function loginUsingId($id) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Log the user out of the application. |
||
| 296 | * |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | public function logout() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * log this user out from every token |
||
| 316 | * |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | public function logoutAll() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * logoutCurrentUser method |
||
| 339 | */ |
||
| 340 | protected function logoutCurrentUser() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Refresh user token |
||
| 356 | * |
||
| 357 | * @return string|null |
||
| 358 | */ |
||
| 359 | public function refreshToken() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get the event dispatcher instance. |
||
| 372 | * |
||
| 373 | * @return Dispatcher |
||
| 374 | */ |
||
| 375 | public function getDispatcher() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Set the event dispatcher instance. |
||
| 382 | * |
||
| 383 | * @param Dispatcher $events |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | public function setDispatcher(Dispatcher $events) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * setToken method |
||
| 393 | * |
||
| 394 | * @param string $token |
||
| 395 | */ |
||
| 396 | public function setToken($token) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * getToken method |
||
| 403 | * |
||
| 404 | * @return null|string |
||
| 405 | */ |
||
| 406 | public function getToken() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * isTokenRefreshable method |
||
| 413 | * |
||
| 414 | * @return boolean |
||
| 415 | */ |
||
| 416 | public function isTokenRefreshable() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * isTokenPresent method |
||
| 423 | * |
||
| 424 | * @return boolean |
||
| 425 | */ |
||
| 426 | public function isTokenPresent() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * getBearerToken method |
||
| 433 | * |
||
| 434 | * @return string|null |
||
| 435 | */ |
||
| 436 | protected function getBearerToken() |
||
| 446 | |||
| 447 | } |