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 |
||
36 | abstract class AbstractGrant implements GrantTypeInterface |
||
37 | { |
||
38 | use EmitterAwareTrait, CryptTrait; |
||
39 | |||
40 | const SCOPE_DELIMITER_STRING = ' '; |
||
41 | |||
42 | const MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS = 10; |
||
43 | |||
44 | /** |
||
45 | * @var ClientRepositoryInterface |
||
46 | */ |
||
47 | protected $clientRepository; |
||
48 | |||
49 | /** |
||
50 | * @var AccessTokenRepositoryInterface |
||
51 | */ |
||
52 | protected $accessTokenRepository; |
||
53 | |||
54 | /** |
||
55 | * @var ScopeRepositoryInterface |
||
56 | */ |
||
57 | protected $scopeRepository; |
||
58 | |||
59 | /** |
||
60 | * @var AuthCodeRepositoryInterface |
||
61 | */ |
||
62 | protected $authCodeRepository; |
||
63 | |||
64 | /** |
||
65 | * @var RefreshTokenRepositoryInterface |
||
66 | */ |
||
67 | protected $refreshTokenRepository; |
||
68 | |||
69 | /** |
||
70 | * @var UserRepositoryInterface |
||
71 | */ |
||
72 | protected $userRepository; |
||
73 | |||
74 | /** |
||
75 | * @var \DateInterval |
||
76 | */ |
||
77 | protected $refreshTokenTTL; |
||
78 | |||
79 | /** |
||
80 | * @var \League\OAuth2\Server\CryptKey |
||
81 | */ |
||
82 | protected $privateKey; |
||
83 | |||
84 | /** |
||
85 | * @string |
||
86 | */ |
||
87 | protected $defaultScope; |
||
88 | |||
89 | /** |
||
90 | * @param ClientRepositoryInterface $clientRepository |
||
91 | */ |
||
92 | 64 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
|
96 | |||
97 | /** |
||
98 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
99 | */ |
||
100 | 42 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
|
104 | |||
105 | /** |
||
106 | * @param ScopeRepositoryInterface $scopeRepository |
||
107 | */ |
||
108 | 37 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
|
112 | |||
113 | /** |
||
114 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
115 | */ |
||
116 | 56 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
|
120 | |||
121 | /** |
||
122 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
123 | */ |
||
124 | 42 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
|
128 | |||
129 | /** |
||
130 | * @param UserRepositoryInterface $userRepository |
||
131 | */ |
||
132 | 5 | public function setUserRepository(UserRepositoryInterface $userRepository) |
|
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | 1 | public function setRefreshTokenTTL(\DateInterval $refreshTokenTTL) |
|
144 | |||
145 | /** |
||
146 | * Set the private key |
||
147 | * |
||
148 | * @param \League\OAuth2\Server\CryptKey $key |
||
149 | */ |
||
150 | 20 | public function setPrivateKey(CryptKey $key) |
|
154 | |||
155 | /** |
||
156 | * @param string $scope |
||
157 | */ |
||
158 | 16 | public function setDefaultScope($scope) |
|
162 | |||
163 | /** |
||
164 | * Validate the client. |
||
165 | * |
||
166 | * @param ServerRequestInterface $request |
||
167 | * |
||
168 | * @throws OAuthServerException |
||
169 | * |
||
170 | * @return ClientEntityInterface |
||
171 | */ |
||
172 | 42 | protected function validateClient(ServerRequestInterface $request) |
|
204 | |||
205 | /** |
||
206 | * Validate redirectUri from the request. |
||
207 | * If a redirect URI is provided ensure it matches what is pre-registered |
||
208 | * |
||
209 | * @param string $redirectUri |
||
210 | * @param ClientEntityInterface $client |
||
211 | * @param ServerRequestInterface $request |
||
212 | * |
||
213 | * @throws OAuthServerException |
||
214 | */ |
||
215 | 34 | protected function validateRedirectUri( |
|
232 | |||
233 | /** |
||
234 | * Validate scopes in the request. |
||
235 | * |
||
236 | * @param string|array $scopes |
||
237 | * @param string $redirectUri |
||
238 | * |
||
239 | * @return ScopeEntityInterface[] |
||
240 | * @throws OAuthServerException |
||
241 | */ |
||
242 | 34 | public function validateScopes($scopes, $redirectUri = null) |
|
262 | |||
263 | /** |
||
264 | * Converts a scopes query string to an array to easily iterate for validation. |
||
265 | * |
||
266 | * @param string $scopes |
||
267 | * |
||
268 | * @return array |
||
269 | */ |
||
270 | private function convertScopesQueryStringToArray($scopes) |
||
276 | |||
277 | /** |
||
278 | * Retrieve request parameter. |
||
279 | * |
||
280 | * @param string $parameter |
||
281 | * @param ServerRequestInterface $request |
||
282 | * @param mixed $default |
||
283 | * |
||
284 | * @return null|string |
||
285 | */ |
||
286 | 42 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
292 | |||
293 | /** |
||
294 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
295 | * of a request. First index of the returned array is the username, |
||
296 | * second is the password (so list() will work). If the header does |
||
297 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
298 | * [null, null]. |
||
299 | * |
||
300 | * @param ServerRequestInterface $request |
||
301 | * |
||
302 | * @return string[]|null[] |
||
303 | */ |
||
304 | 47 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
325 | |||
326 | /** |
||
327 | * Retrieve query string parameter. |
||
328 | * |
||
329 | * @param string $parameter |
||
330 | * @param ServerRequestInterface $request |
||
331 | * @param mixed $default |
||
332 | * |
||
333 | * @return null|string |
||
334 | */ |
||
335 | 21 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
339 | |||
340 | /** |
||
341 | * Retrieve cookie parameter. |
||
342 | * |
||
343 | * @param string $parameter |
||
344 | * @param ServerRequestInterface $request |
||
345 | * @param mixed $default |
||
346 | * |
||
347 | * @return null|string |
||
348 | */ |
||
349 | 1 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
353 | |||
354 | /** |
||
355 | * Retrieve server parameter. |
||
356 | * |
||
357 | * @param string $parameter |
||
358 | * @param ServerRequestInterface $request |
||
359 | * @param mixed $default |
||
360 | * |
||
361 | * @return null|string |
||
362 | */ |
||
363 | 20 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
367 | |||
368 | /** |
||
369 | * Issue an access token. |
||
370 | * |
||
371 | * @param \DateInterval $accessTokenTTL |
||
372 | * @param ClientEntityInterface $client |
||
373 | * @param string|null $userIdentifier |
||
374 | * @param ScopeEntityInterface[] $scopes |
||
375 | * |
||
376 | * @return AccessTokenEntityInterface |
||
377 | * @throws OAuthServerException |
||
378 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
379 | */ |
||
380 | 17 | protected function issueAccessToken( |
|
410 | |||
411 | /** |
||
412 | * Issue an auth code. |
||
413 | * |
||
414 | * @param \DateInterval $authCodeTTL |
||
415 | * @param ClientEntityInterface $client |
||
416 | * @param string $userIdentifier |
||
417 | * @param string|null $redirectUri |
||
418 | * @param ScopeEntityInterface[] $scopes |
||
419 | * |
||
420 | * @throws OAuthServerException |
||
421 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
422 | * |
||
423 | * @return AuthCodeEntityInterface |
||
424 | */ |
||
425 | 6 | protected function issueAuthCode( |
|
460 | |||
461 | /** |
||
462 | * @param AccessTokenEntityInterface $accessToken |
||
463 | * |
||
464 | * @throws OAuthServerException |
||
465 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
466 | * |
||
467 | * @return RefreshTokenEntityInterface |
||
468 | */ |
||
469 | 10 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
490 | |||
491 | /** |
||
492 | * Generate a new unique identifier. |
||
493 | * |
||
494 | * @param int $length |
||
495 | * |
||
496 | * @return string |
||
497 | * @throws OAuthServerException |
||
498 | */ |
||
499 | 25 | protected function generateUniqueIdentifier($length = 40) |
|
514 | |||
515 | /** |
||
516 | * {@inheritdoc} |
||
517 | */ |
||
518 | 5 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
527 | |||
528 | /** |
||
529 | * {@inheritdoc} |
||
530 | */ |
||
531 | 1 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
535 | |||
536 | /** |
||
537 | * {@inheritdoc} |
||
538 | */ |
||
539 | 1 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
543 | |||
544 | /** |
||
545 | * {@inheritdoc} |
||
546 | */ |
||
547 | 1 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
551 | } |
||
552 |