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 $scopes |
||
237 | * @param string $redirectUri |
||
238 | * |
||
239 | * @throws OAuthServerException |
||
240 | * |
||
241 | * @return ScopeEntityInterface[] |
||
242 | */ |
||
243 | 34 | public function validateScopes($scopes, $redirectUri = null) |
|
263 | |||
264 | /** |
||
265 | * Converts a scopes query string to an array to easily iterate for validation. |
||
266 | * |
||
267 | * @param string $scopes |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | private function convertScopesQueryStringToArray($scopes) |
||
277 | |||
278 | /** |
||
279 | * Retrieve request parameter. |
||
280 | * |
||
281 | * @param string $parameter |
||
282 | * @param ServerRequestInterface $request |
||
283 | * @param mixed $default |
||
284 | * |
||
285 | * @return null|string |
||
286 | */ |
||
287 | 42 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
293 | |||
294 | /** |
||
295 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
296 | * of a request. First index of the returned array is the username, |
||
297 | * second is the password (so list() will work). If the header does |
||
298 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
299 | * [null, null]. |
||
300 | * |
||
301 | * @param ServerRequestInterface $request |
||
302 | * |
||
303 | * @return string[]|null[] |
||
304 | */ |
||
305 | 47 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
326 | |||
327 | /** |
||
328 | * Retrieve query string parameter. |
||
329 | * |
||
330 | * @param string $parameter |
||
331 | * @param ServerRequestInterface $request |
||
332 | * @param mixed $default |
||
333 | * |
||
334 | * @return null|string |
||
335 | */ |
||
336 | 21 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
340 | |||
341 | /** |
||
342 | * Retrieve cookie parameter. |
||
343 | * |
||
344 | * @param string $parameter |
||
345 | * @param ServerRequestInterface $request |
||
346 | * @param mixed $default |
||
347 | * |
||
348 | * @return null|string |
||
349 | */ |
||
350 | 1 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
354 | |||
355 | /** |
||
356 | * Retrieve server parameter. |
||
357 | * |
||
358 | * @param string $parameter |
||
359 | * @param ServerRequestInterface $request |
||
360 | * @param mixed $default |
||
361 | * |
||
362 | * @return null|string |
||
363 | */ |
||
364 | 20 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
368 | |||
369 | /** |
||
370 | * Issue an access token. |
||
371 | * |
||
372 | * @param \DateInterval $accessTokenTTL |
||
373 | * @param ClientEntityInterface $client |
||
374 | * @param string|null $userIdentifier |
||
375 | * @param ScopeEntityInterface[] $scopes |
||
376 | * |
||
377 | * @return AccessTokenEntityInterface |
||
378 | * @throws OAuthServerException |
||
379 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
380 | */ |
||
381 | 17 | protected function issueAccessToken( |
|
411 | |||
412 | /** |
||
413 | * Issue an auth code. |
||
414 | * |
||
415 | * @param \DateInterval $authCodeTTL |
||
416 | * @param ClientEntityInterface $client |
||
417 | * @param string $userIdentifier |
||
418 | * @param string|null $redirectUri |
||
419 | * @param ScopeEntityInterface[] $scopes |
||
420 | * |
||
421 | * @throws OAuthServerException |
||
422 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
423 | * |
||
424 | * @return AuthCodeEntityInterface |
||
425 | */ |
||
426 | 6 | protected function issueAuthCode( |
|
461 | |||
462 | /** |
||
463 | * @param AccessTokenEntityInterface $accessToken |
||
464 | * |
||
465 | * @throws OAuthServerException |
||
466 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
467 | * |
||
468 | * @return RefreshTokenEntityInterface |
||
469 | */ |
||
470 | 10 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
491 | |||
492 | /** |
||
493 | * Generate a new unique identifier. |
||
494 | * |
||
495 | * @param int $length |
||
496 | * |
||
497 | * @throws OAuthServerException |
||
498 | * |
||
499 | * @return string |
||
500 | */ |
||
501 | 25 | protected function generateUniqueIdentifier($length = 40) |
|
516 | |||
517 | /** |
||
518 | * {@inheritdoc} |
||
519 | */ |
||
520 | 5 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
529 | |||
530 | /** |
||
531 | * {@inheritdoc} |
||
532 | */ |
||
533 | 1 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
537 | |||
538 | /** |
||
539 | * {@inheritdoc} |
||
540 | */ |
||
541 | 1 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
545 | |||
546 | /** |
||
547 | * {@inheritdoc} |
||
548 | */ |
||
549 | 1 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
553 | } |
||
554 |