Complex classes like AbstractGrant 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 AbstractGrant, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | abstract class AbstractGrant implements GrantTypeInterface |
||
38 | { |
||
39 | use EmitterAwareTrait, CryptTrait; |
||
40 | |||
41 | const SCOPE_DELIMITER_STRING = ' '; |
||
42 | |||
43 | const MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS = 10; |
||
44 | |||
45 | /** |
||
46 | * @var ClientRepositoryInterface |
||
47 | */ |
||
48 | protected $clientRepository; |
||
49 | |||
50 | /** |
||
51 | * @var AccessTokenRepositoryInterface |
||
52 | */ |
||
53 | protected $accessTokenRepository; |
||
54 | |||
55 | /** |
||
56 | * @var ScopeRepositoryInterface |
||
57 | */ |
||
58 | protected $scopeRepository; |
||
59 | |||
60 | /** |
||
61 | * @var AuthCodeRepositoryInterface |
||
62 | */ |
||
63 | protected $authCodeRepository; |
||
64 | |||
65 | /** |
||
66 | * @var RefreshTokenRepositoryInterface |
||
67 | */ |
||
68 | protected $refreshTokenRepository; |
||
69 | |||
70 | /** |
||
71 | * @var UserRepositoryInterface |
||
72 | */ |
||
73 | protected $userRepository; |
||
74 | |||
75 | /** |
||
76 | * @var \DateInterval |
||
77 | */ |
||
78 | protected $refreshTokenTTL; |
||
79 | |||
80 | /** |
||
81 | * @var \League\OAuth2\Server\CryptKey |
||
82 | */ |
||
83 | protected $privateKey; |
||
84 | |||
85 | /** |
||
86 | * @string |
||
87 | */ |
||
88 | protected $defaultScope; |
||
89 | |||
90 | /** |
||
91 | * @param ClientRepositoryInterface $clientRepository |
||
92 | */ |
||
93 | 64 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
|
97 | |||
98 | /** |
||
99 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
100 | */ |
||
101 | 42 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
|
105 | |||
106 | /** |
||
107 | * @param ScopeRepositoryInterface $scopeRepository |
||
108 | */ |
||
109 | 33 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
|
113 | |||
114 | /** |
||
115 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
116 | */ |
||
117 | 56 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
|
121 | |||
122 | /** |
||
123 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
124 | */ |
||
125 | 42 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
|
129 | |||
130 | /** |
||
131 | * @param UserRepositoryInterface $userRepository |
||
132 | */ |
||
133 | 5 | public function setUserRepository(UserRepositoryInterface $userRepository) |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 1 | public function setRefreshTokenTTL(\DateInterval $refreshTokenTTL) |
|
145 | |||
146 | /** |
||
147 | * Set the private key |
||
148 | * |
||
149 | * @param \League\OAuth2\Server\CryptKey $key |
||
150 | */ |
||
151 | 20 | public function setPrivateKey(CryptKey $key) |
|
155 | |||
156 | /** |
||
157 | * @param string $scope |
||
158 | */ |
||
159 | 16 | public function setDefaultScope($scope) |
|
163 | |||
164 | /** |
||
165 | * Validate the client. |
||
166 | * |
||
167 | * @param ServerRequestInterface $request |
||
168 | * |
||
169 | * @throws OAuthServerException |
||
170 | * |
||
171 | * @return ClientEntityInterface |
||
172 | */ |
||
173 | 42 | protected function validateClient(ServerRequestInterface $request) |
|
217 | |||
218 | /** |
||
219 | * Validate scopes in the request. |
||
220 | * |
||
221 | * @param ClientEntityInterface $client |
||
222 | * @param string $scopes |
||
223 | * @param string $redirectUri |
||
224 | * |
||
225 | * @return ScopeEntityInterface[] |
||
226 | * @throws OAuthServerException |
||
227 | */ |
||
228 | public function validateScopes(ClientEntityInterface $client, $scopes, $redirectUri = null) |
||
255 | |||
256 | /** |
||
257 | * Retrieve the scope identifiers from an Entity. |
||
258 | * |
||
259 | * @param ScopeInterface $entity |
||
260 | * @return string[] |
||
261 | */ |
||
262 | 1 | private function getScopeIdentifiers(ScopeInterface $entity) |
|
271 | |||
272 | /** |
||
273 | * Retrieve request parameter. |
||
274 | * |
||
275 | * @param string $parameter |
||
276 | * @param ServerRequestInterface $request |
||
277 | * @param mixed $default |
||
278 | * |
||
279 | * @return null|string |
||
280 | */ |
||
281 | 42 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
287 | |||
288 | /** |
||
289 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
290 | * of a request. First index of the returned array is the username, |
||
291 | * second is the password (so list() will work). If the header does |
||
292 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
293 | * [null, null]. |
||
294 | * |
||
295 | * @param ServerRequestInterface $request |
||
296 | * |
||
297 | * @return string[]|null[] |
||
298 | */ |
||
299 | 47 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
320 | |||
321 | /** |
||
322 | * Retrieve query string parameter. |
||
323 | * |
||
324 | * @param string $parameter |
||
325 | * @param ServerRequestInterface $request |
||
326 | * @param mixed $default |
||
327 | * |
||
328 | * @return null|string |
||
329 | */ |
||
330 | 22 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
334 | |||
335 | /** |
||
336 | * Retrieve cookie parameter. |
||
337 | * |
||
338 | * @param string $parameter |
||
339 | * @param ServerRequestInterface $request |
||
340 | * @param mixed $default |
||
341 | * |
||
342 | * @return null|string |
||
343 | */ |
||
344 | 1 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
348 | |||
349 | /** |
||
350 | * Retrieve server parameter. |
||
351 | * |
||
352 | * @param string $parameter |
||
353 | * @param ServerRequestInterface $request |
||
354 | * @param mixed $default |
||
355 | * |
||
356 | * @return null|string |
||
357 | */ |
||
358 | 21 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
362 | |||
363 | /** |
||
364 | * Issue an access token. |
||
365 | * |
||
366 | * @param \DateInterval $accessTokenTTL |
||
367 | * @param ClientEntityInterface $client |
||
368 | * @param string|null $userIdentifier |
||
369 | * @param ScopeEntityInterface[] $scopes |
||
370 | * |
||
371 | * @throws OAuthServerException |
||
372 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
373 | * |
||
374 | * @return AccessTokenEntityInterface |
||
375 | */ |
||
376 | 17 | protected function issueAccessToken( |
|
406 | |||
407 | /** |
||
408 | * Issue an auth code. |
||
409 | * |
||
410 | * @param \DateInterval $authCodeTTL |
||
411 | * @param ClientEntityInterface $client |
||
412 | * @param string $userIdentifier |
||
413 | * @param string $redirectUri |
||
414 | * @param ScopeEntityInterface[] $scopes |
||
415 | * |
||
416 | * @throws OAuthServerException |
||
417 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
418 | * |
||
419 | * @return AuthCodeEntityInterface |
||
420 | */ |
||
421 | 6 | protected function issueAuthCode( |
|
453 | |||
454 | /** |
||
455 | * @param AccessTokenEntityInterface $accessToken |
||
456 | * |
||
457 | * @throws OAuthServerException |
||
458 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
459 | * |
||
460 | * @return RefreshTokenEntityInterface |
||
461 | */ |
||
462 | 10 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
483 | |||
484 | /** |
||
485 | * Generate a new unique identifier. |
||
486 | * |
||
487 | * @param int $length |
||
488 | * |
||
489 | * @throws OAuthServerException |
||
490 | * |
||
491 | * @return string |
||
492 | */ |
||
493 | 25 | protected function generateUniqueIdentifier($length = 40) |
|
508 | |||
509 | /** |
||
510 | * {@inheritdoc} |
||
511 | */ |
||
512 | 5 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
521 | |||
522 | /** |
||
523 | * {@inheritdoc} |
||
524 | */ |
||
525 | 1 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
529 | |||
530 | /** |
||
531 | * {@inheritdoc} |
||
532 | */ |
||
533 | 1 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
537 | |||
538 | /** |
||
539 | * {@inheritdoc} |
||
540 | */ |
||
541 | 1 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
545 | } |
||
546 |