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 |
||
42 | abstract class AbstractGrant implements GrantTypeInterface |
||
43 | { |
||
44 | use EmitterAwareTrait, CryptTrait; |
||
45 | |||
46 | const SCOPE_DELIMITER_STRING = ' '; |
||
47 | |||
48 | const MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS = 10; |
||
49 | |||
50 | /** |
||
51 | * @var ClientRepositoryInterface |
||
52 | */ |
||
53 | protected $clientRepository; |
||
54 | |||
55 | /** |
||
56 | * @var AccessTokenRepositoryInterface |
||
57 | */ |
||
58 | protected $accessTokenRepository; |
||
59 | |||
60 | /** |
||
61 | * @var ScopeRepositoryInterface |
||
62 | */ |
||
63 | protected $scopeRepository; |
||
64 | |||
65 | /** |
||
66 | * @var AuthCodeRepositoryInterface |
||
67 | */ |
||
68 | protected $authCodeRepository; |
||
69 | |||
70 | /** |
||
71 | * @var RefreshTokenRepositoryInterface |
||
72 | */ |
||
73 | protected $refreshTokenRepository; |
||
74 | |||
75 | /** |
||
76 | * @var UserRepositoryInterface |
||
77 | */ |
||
78 | protected $userRepository; |
||
79 | |||
80 | /** |
||
81 | * @var DateInterval |
||
82 | */ |
||
83 | protected $refreshTokenTTL; |
||
84 | |||
85 | /** |
||
86 | * @var CryptKey |
||
87 | */ |
||
88 | protected $privateKey; |
||
89 | |||
90 | /** |
||
91 | * @string |
||
92 | */ |
||
93 | protected $defaultScope; |
||
94 | |||
95 | /** |
||
96 | * @param ClientRepositoryInterface $clientRepository |
||
97 | */ |
||
98 | 71 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
|
102 | |||
103 | /** |
||
104 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
105 | */ |
||
106 | 46 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
|
110 | |||
111 | /** |
||
112 | * @param ScopeRepositoryInterface $scopeRepository |
||
113 | */ |
||
114 | 42 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
|
118 | |||
119 | /** |
||
120 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
121 | */ |
||
122 | 62 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
|
126 | |||
127 | /** |
||
128 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
129 | */ |
||
130 | 45 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
|
134 | |||
135 | /** |
||
136 | * @param UserRepositoryInterface $userRepository |
||
137 | */ |
||
138 | 6 | public function setUserRepository(UserRepositoryInterface $userRepository) |
|
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 2 | public function setRefreshTokenTTL(DateInterval $refreshTokenTTL) |
|
150 | |||
151 | /** |
||
152 | * Set the private key |
||
153 | * |
||
154 | * @param CryptKey $key |
||
155 | */ |
||
156 | 35 | public function setPrivateKey(CryptKey $key) |
|
160 | |||
161 | /** |
||
162 | * @param string $scope |
||
163 | */ |
||
164 | 17 | public function setDefaultScope($scope) |
|
168 | |||
169 | /** |
||
170 | * Validate the client. |
||
171 | * |
||
172 | * @param ServerRequestInterface $request |
||
173 | * |
||
174 | * @throws OAuthServerException |
||
175 | * |
||
176 | * @return ClientEntityInterface |
||
177 | */ |
||
178 | 41 | protected function validateClient(ServerRequestInterface $request) |
|
199 | |||
200 | /** |
||
201 | * Wrapper around ClientRepository::getClientEntity() that ensures we emit |
||
202 | * an event and throw an exception if the repo doesn't return a client |
||
203 | * entity. |
||
204 | * |
||
205 | * This is a bit of defensive coding because the interface contract |
||
206 | * doesn't actually enforce non-null returns/exception-on-no-client so |
||
207 | * getClientEntity might return null. By contrast, this method will |
||
208 | * always either return a ClientEntityInterface or throw. |
||
209 | * |
||
210 | * @param string $clientId |
||
211 | * @param ServerRequestInterface $request |
||
212 | * |
||
213 | * @return ClientEntityInterface |
||
214 | */ |
||
215 | 60 | protected function getClientEntityOrFail($clientId, ServerRequestInterface $request) |
|
226 | |||
227 | /** |
||
228 | * Gets the client credentials from the request from the request body or |
||
229 | * the Http Basic Authorization header |
||
230 | * |
||
231 | * @param ServerRequestInterface $request |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | 49 | protected function getClientCredentials(ServerRequestInterface $request) |
|
249 | |||
250 | /** |
||
251 | * Validate redirectUri from the request. |
||
252 | * If a redirect URI is provided ensure it matches what is pre-registered |
||
253 | * |
||
254 | * @param string $redirectUri |
||
255 | * @param ClientEntityInterface $client |
||
256 | * @param ServerRequestInterface $request |
||
257 | * |
||
258 | * @throws OAuthServerException |
||
259 | */ |
||
260 | 30 | protected function validateRedirectUri( |
|
277 | |||
278 | /** |
||
279 | * Validate scopes in the request. |
||
280 | * |
||
281 | * @param string|array $scopes |
||
282 | * @param string $redirectUri |
||
283 | * |
||
284 | * @throws OAuthServerException |
||
285 | * |
||
286 | * @return ScopeEntityInterface[] |
||
287 | */ |
||
288 | 38 | public function validateScopes($scopes, $redirectUri = null) |
|
308 | |||
309 | /** |
||
310 | * Converts a scopes query string to an array to easily iterate for validation. |
||
311 | * |
||
312 | * @param string $scopes |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | 24 | private function convertScopesQueryStringToArray($scopes) |
|
322 | |||
323 | /** |
||
324 | * Retrieve request parameter. |
||
325 | * |
||
326 | * @param string $parameter |
||
327 | * @param ServerRequestInterface $request |
||
328 | * @param mixed $default |
||
329 | * |
||
330 | * @return null|string |
||
331 | */ |
||
332 | 49 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
338 | |||
339 | /** |
||
340 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
341 | * of a request. First index of the returned array is the username, |
||
342 | * second is the password (so list() will work). If the header does |
||
343 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
344 | * [null, null]. |
||
345 | * |
||
346 | * @param ServerRequestInterface $request |
||
347 | * |
||
348 | * @return string[]|null[] |
||
349 | */ |
||
350 | 54 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
371 | |||
372 | /** |
||
373 | * Retrieve query string parameter. |
||
374 | * |
||
375 | * @param string $parameter |
||
376 | * @param ServerRequestInterface $request |
||
377 | * @param mixed $default |
||
378 | * |
||
379 | * @return null|string |
||
380 | */ |
||
381 | 21 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
385 | |||
386 | /** |
||
387 | * Retrieve cookie parameter. |
||
388 | * |
||
389 | * @param string $parameter |
||
390 | * @param ServerRequestInterface $request |
||
391 | * @param mixed $default |
||
392 | * |
||
393 | * @return null|string |
||
394 | */ |
||
395 | 1 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
399 | |||
400 | /** |
||
401 | * Retrieve server parameter. |
||
402 | * |
||
403 | * @param string $parameter |
||
404 | * @param ServerRequestInterface $request |
||
405 | * @param mixed $default |
||
406 | * |
||
407 | * @return null|string |
||
408 | */ |
||
409 | 20 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
413 | |||
414 | /** |
||
415 | * Issue an access token. |
||
416 | * |
||
417 | * @param DateInterval $accessTokenTTL |
||
418 | * @param ClientEntityInterface $client |
||
419 | * @param string|null $userIdentifier |
||
420 | * @param ScopeEntityInterface[] $scopes |
||
421 | * |
||
422 | * @throws OAuthServerException |
||
423 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
424 | * |
||
425 | * @return AccessTokenEntityInterface |
||
426 | */ |
||
427 | 22 | protected function issueAccessToken( |
|
452 | |||
453 | /** |
||
454 | * Issue an auth code. |
||
455 | * |
||
456 | * @param DateInterval $authCodeTTL |
||
457 | * @param ClientEntityInterface $client |
||
458 | * @param string $userIdentifier |
||
459 | * @param string|null $redirectUri |
||
460 | * @param ScopeEntityInterface[] $scopes |
||
461 | * |
||
462 | * @throws OAuthServerException |
||
463 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
464 | * |
||
465 | * @return AuthCodeEntityInterface |
||
466 | */ |
||
467 | 6 | protected function issueAuthCode( |
|
502 | |||
503 | /** |
||
504 | * @param AccessTokenEntityInterface $accessToken |
||
505 | * |
||
506 | * @throws OAuthServerException |
||
507 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
508 | * |
||
509 | * @return RefreshTokenEntityInterface|null |
||
510 | */ |
||
511 | 16 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
537 | |||
538 | /** |
||
539 | * Generate a new unique identifier. |
||
540 | * |
||
541 | * @param int $length |
||
542 | * |
||
543 | * @throws OAuthServerException |
||
544 | * |
||
545 | * @return string |
||
546 | */ |
||
547 | 30 | protected function generateUniqueIdentifier($length = 40) |
|
562 | |||
563 | /** |
||
564 | * {@inheritdoc} |
||
565 | */ |
||
566 | 5 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
575 | |||
576 | /** |
||
577 | * {@inheritdoc} |
||
578 | */ |
||
579 | 1 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
583 | |||
584 | /** |
||
585 | * {@inheritdoc} |
||
586 | */ |
||
587 | 1 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
591 | |||
592 | /** |
||
593 | * {@inheritdoc} |
||
594 | */ |
||
595 | 1 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
599 | } |
||
600 |