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 JwtService |
||
38 | */ |
||
39 | protected $jwtService; |
||
40 | |||
41 | /** |
||
42 | * @var Request |
||
43 | */ |
||
44 | protected $request; |
||
45 | |||
46 | /** |
||
47 | * The event dispatcher instance. |
||
48 | * |
||
49 | * @var Dispatcher |
||
50 | */ |
||
51 | protected $events; |
||
52 | |||
53 | /** |
||
54 | * Indicates if the logout method has been called. |
||
55 | * |
||
56 | * @var bool |
||
57 | */ |
||
58 | protected $loggedOut = false; |
||
59 | |||
60 | /** |
||
61 | * JwtGuard constructor |
||
62 | * |
||
63 | * @param UserProvider $provider |
||
64 | * @param JwtService $jwtService |
||
65 | * @param Request|null $request |
||
66 | */ |
||
67 | public function __construct( |
||
76 | |||
77 | /** |
||
78 | * Get the currently authenticated user. |
||
79 | * |
||
80 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
81 | */ |
||
82 | public function user() |
||
106 | |||
107 | /** |
||
108 | * Retrieve the user by the given payload. |
||
109 | * |
||
110 | * @param string $token |
||
111 | * @return AuthenticatableContract|null |
||
112 | * @throws InaccessibleException |
||
113 | * @throws MalformedException |
||
114 | * @throws TokenExpiredException |
||
115 | * @throws InvalidTokenException |
||
116 | */ |
||
117 | protected function getUserByToken($token) |
||
128 | |||
129 | /** |
||
130 | * Validate a user's credentials. |
||
131 | * |
||
132 | * @param array $credentials |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function validate(array $credentials = []) |
||
139 | |||
140 | /** |
||
141 | * Attempt to authenticate a user using the given credentials. |
||
142 | * |
||
143 | * @param array $credentials |
||
144 | * @param bool $login |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function attempt(array $credentials = [], $login = true) |
||
170 | |||
171 | /** |
||
172 | * Determine if the user matches the credentials. |
||
173 | * |
||
174 | * @param mixed $user |
||
175 | * @param array $credentials |
||
176 | * @return bool |
||
177 | */ |
||
178 | protected function hasValidCredentials($user, $credentials) |
||
182 | |||
183 | /** |
||
184 | * Fire the attempt event with the arguments. |
||
185 | * |
||
186 | * @param array $credentials |
||
187 | * @param bool $login |
||
188 | * @return void |
||
189 | */ |
||
190 | protected function fireAttemptEvent(array $credentials, $login) |
||
198 | |||
199 | /** |
||
200 | * Register an authentication attempt event listener. |
||
201 | * |
||
202 | * @param mixed $callback |
||
203 | * @return void |
||
204 | */ |
||
205 | public function attempting($callback) |
||
211 | |||
212 | /** |
||
213 | * Log a user into the application. |
||
214 | * |
||
215 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
216 | * @return void |
||
217 | */ |
||
218 | public function login(AuthenticatableContract $user) |
||
236 | |||
237 | /** |
||
238 | * generateTokenForUser method |
||
239 | * |
||
240 | * @param string $token |
||
241 | * @return string |
||
242 | */ |
||
243 | protected function refreshTokenForUser($token) |
||
253 | |||
254 | /** |
||
255 | * Fire the login event if the dispatcher is set. |
||
256 | * |
||
257 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
258 | * @param bool $remember |
||
259 | * @return void |
||
260 | */ |
||
261 | protected function fireLoginEvent($user, $remember = false) |
||
267 | |||
268 | /** |
||
269 | * Log the given user ID into the application. |
||
270 | * |
||
271 | * @param mixed $id |
||
272 | * @return \Illuminate\Contracts\Auth\Authenticatable |
||
273 | */ |
||
274 | public function loginUsingId($id) |
||
286 | |||
287 | /** |
||
288 | * Log the user out of the application. |
||
289 | * |
||
290 | * @return void |
||
291 | */ |
||
292 | public function logout() |
||
306 | |||
307 | /** |
||
308 | * log this user out from every token |
||
309 | * |
||
310 | * @return void |
||
311 | */ |
||
312 | public function logoutAll() |
||
329 | |||
330 | /** |
||
331 | * logoutCurrentUser method |
||
332 | */ |
||
333 | protected function logoutCurrentUser() |
||
346 | |||
347 | /** |
||
348 | * Refresh user token |
||
349 | * |
||
350 | * @return string|null |
||
351 | */ |
||
352 | public function refreshToken() |
||
362 | |||
363 | /** |
||
364 | * Get the event dispatcher instance. |
||
365 | * |
||
366 | * @return Dispatcher |
||
367 | */ |
||
368 | public function getDispatcher() |
||
372 | |||
373 | /** |
||
374 | * Set the event dispatcher instance. |
||
375 | * |
||
376 | * @param Dispatcher $events |
||
377 | * @return void |
||
378 | */ |
||
379 | public function setDispatcher(Dispatcher $events) |
||
383 | |||
384 | /** |
||
385 | * setToken method |
||
386 | * |
||
387 | * @param string $token |
||
388 | */ |
||
389 | public function setToken($token) |
||
393 | |||
394 | /** |
||
395 | * getToken method |
||
396 | * |
||
397 | * @return null|string |
||
398 | */ |
||
399 | public function getToken() |
||
403 | |||
404 | /** |
||
405 | * isTokenRefreshable method |
||
406 | */ |
||
407 | public function isTokenRefreshable() |
||
411 | |||
412 | /** |
||
413 | * getBearerToken method |
||
414 | * |
||
415 | * @return string|null |
||
416 | */ |
||
417 | protected function getBearerToken() |
||
427 | |||
428 | } |