Complex classes like JWTAuth 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 JWTAuth, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class JWTAuth |
||
13 | { |
||
14 | /** |
||
15 | * @var \Tymon\JWTAuth\JWTManager |
||
16 | */ |
||
17 | protected $manager; |
||
18 | |||
19 | /** |
||
20 | * @var \Tymon\JWTAuth\Providers\User\UserInterface |
||
21 | */ |
||
22 | protected $user; |
||
23 | |||
24 | /** |
||
25 | * @var \Tymon\JWTAuth\Providers\Auth\AuthInterface |
||
26 | */ |
||
27 | protected $auth; |
||
28 | |||
29 | /** |
||
30 | * @var \Illuminate\Http\Request |
||
31 | */ |
||
32 | protected $request; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $identifier = 'id'; |
||
38 | |||
39 | /** |
||
40 | * @var \Tymon\JWTAuth\Token |
||
41 | */ |
||
42 | protected $token; |
||
43 | |||
44 | |||
45 | /** |
||
46 | * @var \Illuminate\Contracts\Auth\Authenticatable |
||
47 | */ |
||
48 | protected $userModel = null; |
||
49 | |||
50 | /** |
||
51 | * @param \Tymon\JWTAuth\JWTManager $manager |
||
52 | * @param \Tymon\JWTAuth\Providers\User\UserInterface $user |
||
53 | * @param \Tymon\JWTAuth\Providers\Auth\AuthInterface $auth |
||
54 | * @param \Illuminate\Http\Request $request |
||
55 | */ |
||
56 | 54 | public function __construct(JWTManager $manager, UserInterface $user, AuthInterface $auth, Request $request) |
|
63 | |||
64 | /** |
||
65 | * @return \Illuminate\Contracts\Auth\Authenticatable |
||
66 | */ |
||
67 | 3 | public function getUserModel() |
|
71 | |||
72 | /** |
||
73 | * @param \Illuminate\Contracts\Auth\Authenticatable $userModel |
||
74 | */ |
||
75 | public function setUserModel(Authenticatable $userModel) |
||
79 | |||
80 | /** |
||
81 | * @param \stdClass $userModel |
||
82 | */ |
||
83 | 6 | private function setUserModelAsObject (\stdClass $userModel) |
|
87 | |||
88 | |||
89 | |||
90 | /** |
||
91 | * Find a user using the user identifier in the subject claim. |
||
92 | * |
||
93 | * @param bool|string $token |
||
94 | * |
||
95 | * @return mixed |
||
96 | */ |
||
97 | 9 | public function toUser($token = false) |
|
111 | |||
112 | /** |
||
113 | * Generate a token using the user identifier as the subject claim. |
||
114 | * |
||
115 | * @param mixed $user |
||
116 | * @param array $customClaims |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 6 | public function fromUser($user, array $customClaims = []) |
|
126 | |||
127 | /** |
||
128 | * Attempt to authenticate the user and return the token. |
||
129 | * |
||
130 | * @param array $credentials |
||
131 | * @param array $customClaims |
||
132 | * |
||
133 | * @return false|string |
||
134 | * @throws JWTException |
||
135 | */ |
||
136 | 6 | public function attempt(array $credentials = [], array $customClaims = []) |
|
144 | |||
145 | /** |
||
146 | * Authenticate a user via a token. |
||
147 | * |
||
148 | * @param mixed $token |
||
149 | * @param Array $custom custom claims that must be equals (all custom fields indicated must be equals in token, this doesn't entail that the token must have only these claims) |
||
150 | * @return mixed |
||
151 | */ |
||
152 | 6 | public function authenticate($token = false, $custom = []) |
|
173 | |||
174 | /** |
||
175 | * Refresh an expired token. |
||
176 | * |
||
177 | * @param mixed $token |
||
178 | * @param Array $custom |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | 3 | public function refresh($token = false, $custom = []) |
|
188 | |||
189 | /** |
||
190 | * Invalidate a token (add it to the blacklist). |
||
191 | * |
||
192 | * @param mixed $token |
||
193 | * |
||
194 | * @return boolean |
||
195 | */ |
||
196 | 3 | public function invalidate($token = false) |
|
202 | |||
203 | /** |
||
204 | * Get the token. |
||
205 | * |
||
206 | * @return boolean|string |
||
207 | */ |
||
208 | 12 | public function getToken() |
|
220 | |||
221 | /** |
||
222 | * Get the raw Payload instance. |
||
223 | * |
||
224 | * @param mixed $token |
||
225 | * |
||
226 | * @return \Tymon\JWTAuth\Payload |
||
227 | */ |
||
228 | 15 | public function getPayload($token = false) |
|
234 | |||
235 | /** |
||
236 | * Parse the token from the request. |
||
237 | * @param string $method |
||
238 | * @param string $header |
||
239 | * @param string $query |
||
240 | * @return JWTAuth |
||
241 | * @throws JWTException |
||
242 | */ |
||
243 | 15 | public function parseToken($method = 'bearer', $header = 'authorization', $query = 'token') |
|
253 | |||
254 | /** |
||
255 | * Parse token from the authorization header. |
||
256 | * |
||
257 | * @param string $header |
||
258 | * @param string $method |
||
259 | * |
||
260 | * @return false|string |
||
261 | */ |
||
262 | 15 | protected function parseAuthHeader($header = 'authorization', $method = 'bearer') |
|
272 | |||
273 | /** |
||
274 | * Create a Payload instance. |
||
275 | * |
||
276 | * @param mixed $subject |
||
277 | * @param array $customClaims |
||
278 | * |
||
279 | * @return \Tymon\JWTAuth\Payload |
||
280 | */ |
||
281 | 6 | protected function makePayload($subject, array $customClaims = []) |
|
287 | |||
288 | /** |
||
289 | * Set the identifier. |
||
290 | * |
||
291 | * @param string $identifier |
||
292 | * |
||
293 | * @return $this |
||
294 | */ |
||
295 | 3 | public function setIdentifier($identifier) |
|
301 | |||
302 | /** |
||
303 | * Get the identifier. |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | 3 | public function getIdentifier() |
|
311 | |||
312 | /** |
||
313 | * Set the token. |
||
314 | * |
||
315 | * @param string $token |
||
316 | * |
||
317 | * @return $this |
||
318 | */ |
||
319 | 27 | public function setToken($token) |
|
325 | |||
326 | /** |
||
327 | * Ensure that a token is available. |
||
328 | * |
||
329 | * @param mixed $token |
||
330 | * |
||
331 | * @return JWTAuth |
||
332 | * |
||
333 | * @throws \Tymon\JWTAuth\Exceptions\JWTException |
||
334 | */ |
||
335 | 21 | protected function requireToken($token) |
|
343 | |||
344 | /** |
||
345 | * Set the request instance. |
||
346 | * |
||
347 | * @param Request $request |
||
348 | * @return $this |
||
349 | */ |
||
350 | 3 | public function setRequest(Request $request) |
|
356 | |||
357 | /** |
||
358 | * Get the JWTManager instance. |
||
359 | * |
||
360 | * @return \Tymon\JWTAuth\JWTManager |
||
361 | */ |
||
362 | 3 | public function manager() |
|
366 | |||
367 | /** |
||
368 | * Magically call the JWT Manager. |
||
369 | * |
||
370 | * @param string $method |
||
371 | * @param array $parameters |
||
372 | * |
||
373 | * @return mixed |
||
374 | * |
||
375 | * @throws \BadMethodCallException |
||
376 | */ |
||
377 | 3 | public function __call($method, $parameters) |
|
385 | } |
||
386 |