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 | * @throws OAuthServerException |
||
226 | * |
||
227 | * @return ScopeEntityInterface[] |
||
228 | */ |
||
229 | public function validateScopes(ClientEntityInterface $client, $scopes, $redirectUri = null) |
||
256 | |||
257 | /** |
||
258 | * Retrieve the scope identifiers from an Entity. |
||
259 | * |
||
260 | * @param ScopeInterface $entity |
||
261 | * |
||
262 | * @return string[] |
||
263 | */ |
||
264 | 1 | private function getScopeIdentifiers(ScopeInterface $entity) |
|
274 | |||
275 | /** |
||
276 | * Retrieve request parameter. |
||
277 | * |
||
278 | * @param string $parameter |
||
279 | * @param ServerRequestInterface $request |
||
280 | * @param mixed $default |
||
281 | * |
||
282 | * @return null|string |
||
283 | */ |
||
284 | 42 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
290 | |||
291 | /** |
||
292 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
293 | * of a request. First index of the returned array is the username, |
||
294 | * second is the password (so list() will work). If the header does |
||
295 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
296 | * [null, null]. |
||
297 | * |
||
298 | * @param ServerRequestInterface $request |
||
299 | * |
||
300 | * @return string[]|null[] |
||
301 | */ |
||
302 | 47 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
323 | |||
324 | /** |
||
325 | * Retrieve query string parameter. |
||
326 | * |
||
327 | * @param string $parameter |
||
328 | * @param ServerRequestInterface $request |
||
329 | * @param mixed $default |
||
330 | * |
||
331 | * @return null|string |
||
332 | */ |
||
333 | 22 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
337 | |||
338 | /** |
||
339 | * Retrieve cookie parameter. |
||
340 | * |
||
341 | * @param string $parameter |
||
342 | * @param ServerRequestInterface $request |
||
343 | * @param mixed $default |
||
344 | * |
||
345 | * @return null|string |
||
346 | */ |
||
347 | 1 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
351 | |||
352 | /** |
||
353 | * Retrieve server parameter. |
||
354 | * |
||
355 | * @param string $parameter |
||
356 | * @param ServerRequestInterface $request |
||
357 | * @param mixed $default |
||
358 | * |
||
359 | * @return null|string |
||
360 | */ |
||
361 | 21 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
365 | |||
366 | /** |
||
367 | * Issue an access token. |
||
368 | * |
||
369 | * @param \DateInterval $accessTokenTTL |
||
370 | * @param ClientEntityInterface $client |
||
371 | * @param string|null $userIdentifier |
||
372 | * @param ScopeEntityInterface[] $scopes |
||
373 | * |
||
374 | * @throws OAuthServerException |
||
375 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
376 | * |
||
377 | * @return AccessTokenEntityInterface |
||
378 | */ |
||
379 | 17 | protected function issueAccessToken( |
|
409 | |||
410 | /** |
||
411 | * Issue an auth code. |
||
412 | * |
||
413 | * @param \DateInterval $authCodeTTL |
||
414 | * @param ClientEntityInterface $client |
||
415 | * @param string $userIdentifier |
||
416 | * @param string $redirectUri |
||
417 | * @param ScopeEntityInterface[] $scopes |
||
418 | * |
||
419 | * @throws OAuthServerException |
||
420 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
421 | * |
||
422 | * @return AuthCodeEntityInterface |
||
423 | */ |
||
424 | 6 | protected function issueAuthCode( |
|
456 | |||
457 | /** |
||
458 | * @param AccessTokenEntityInterface $accessToken |
||
459 | * |
||
460 | * @throws OAuthServerException |
||
461 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
462 | * |
||
463 | * @return RefreshTokenEntityInterface |
||
464 | */ |
||
465 | 10 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
486 | |||
487 | /** |
||
488 | * Generate a new unique identifier. |
||
489 | * |
||
490 | * @param int $length |
||
491 | * |
||
492 | * @throws OAuthServerException |
||
493 | * |
||
494 | * @return string |
||
495 | */ |
||
496 | 25 | protected function generateUniqueIdentifier($length = 40) |
|
511 | |||
512 | /** |
||
513 | * {@inheritdoc} |
||
514 | */ |
||
515 | 5 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
524 | |||
525 | /** |
||
526 | * {@inheritdoc} |
||
527 | */ |
||
528 | 1 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
532 | |||
533 | /** |
||
534 | * {@inheritdoc} |
||
535 | */ |
||
536 | 1 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
540 | |||
541 | /** |
||
542 | * {@inheritdoc} |
||
543 | */ |
||
544 | 1 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
548 | } |
||
549 |