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 |
||
41 | abstract class AbstractGrant implements GrantTypeInterface |
||
42 | { |
||
43 | use EmitterAwareTrait, CryptTrait; |
||
44 | |||
45 | const SCOPE_DELIMITER_STRING = ' '; |
||
46 | |||
47 | const MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS = 10; |
||
48 | |||
49 | /** |
||
50 | * @var ClientRepositoryInterface |
||
51 | */ |
||
52 | protected $clientRepository; |
||
53 | |||
54 | /** |
||
55 | * @var AccessTokenRepositoryInterface |
||
56 | */ |
||
57 | protected $accessTokenRepository; |
||
58 | |||
59 | /** |
||
60 | * @var ScopeRepositoryInterface |
||
61 | */ |
||
62 | protected $scopeRepository; |
||
63 | |||
64 | /** |
||
65 | * @var AuthCodeRepositoryInterface |
||
66 | */ |
||
67 | protected $authCodeRepository; |
||
68 | |||
69 | /** |
||
70 | * @var RefreshTokenRepositoryInterface |
||
71 | */ |
||
72 | protected $refreshTokenRepository; |
||
73 | |||
74 | /** |
||
75 | * @var UserRepositoryInterface |
||
76 | */ |
||
77 | protected $userRepository; |
||
78 | |||
79 | /** |
||
80 | * @var DateInterval |
||
81 | */ |
||
82 | protected $refreshTokenTTL; |
||
83 | |||
84 | /** |
||
85 | * @var CryptKey |
||
86 | */ |
||
87 | protected $privateKey; |
||
88 | |||
89 | /** |
||
90 | * @string |
||
91 | */ |
||
92 | protected $defaultScope; |
||
93 | |||
94 | /** |
||
95 | * @var IdentifierGeneratorInterface |
||
96 | */ |
||
97 | protected $identifierGenerator; |
||
98 | |||
99 | /** |
||
100 | * @param ClientRepositoryInterface $clientRepository |
||
101 | */ |
||
102 | 13 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
|
106 | |||
107 | /** |
||
108 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
109 | */ |
||
110 | 5 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
|
114 | |||
115 | /** |
||
116 | * @param ScopeRepositoryInterface $scopeRepository |
||
117 | */ |
||
118 | 5 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
|
122 | |||
123 | /** |
||
124 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
125 | */ |
||
126 | 3 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
|
130 | |||
131 | /** |
||
132 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
133 | */ |
||
134 | 3 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
|
138 | |||
139 | /** |
||
140 | * @param UserRepositoryInterface $userRepository |
||
141 | */ |
||
142 | public function setUserRepository(UserRepositoryInterface $userRepository) |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function setRefreshTokenTTL(DateInterval $refreshTokenTTL) |
||
154 | |||
155 | /** |
||
156 | * Set the private key |
||
157 | * |
||
158 | * @param CryptKey $key |
||
159 | */ |
||
160 | 5 | public function setPrivateKey(CryptKey $key) |
|
164 | |||
165 | /** |
||
166 | * @param string $scope |
||
167 | */ |
||
168 | 5 | public function setDefaultScope($scope) |
|
172 | |||
173 | 5 | public function setIdentifierGenerator(IdentifierGeneratorInterface $identifierGenerator) |
|
177 | |||
178 | /** |
||
179 | * Validate the client. |
||
180 | * |
||
181 | * @param ServerRequestInterface $request |
||
182 | * |
||
183 | * @throws OAuthServerException |
||
184 | * |
||
185 | * @return ClientEntityInterface |
||
186 | */ |
||
187 | 9 | protected function validateClient(ServerRequestInterface $request) |
|
219 | |||
220 | /** |
||
221 | * Validate redirectUri from the request. |
||
222 | * If a redirect URI is provided ensure it matches what is pre-registered |
||
223 | * |
||
224 | * @param string $redirectUri |
||
225 | * @param ClientEntityInterface $client |
||
226 | * @param ServerRequestInterface $request |
||
227 | * |
||
228 | * @throws OAuthServerException |
||
229 | */ |
||
230 | 3 | protected function validateRedirectUri( |
|
247 | |||
248 | /** |
||
249 | * Validate scopes in the request. |
||
250 | * |
||
251 | * @param string|array $scopes |
||
252 | * @param string $redirectUri |
||
253 | * |
||
254 | * @throws OAuthServerException |
||
255 | * |
||
256 | * @return ScopeEntityInterface[] |
||
257 | */ |
||
258 | 2 | public function validateScopes($scopes, $redirectUri = null) |
|
278 | |||
279 | /** |
||
280 | * Converts a scopes query string to an array to easily iterate for validation. |
||
281 | * |
||
282 | * @param string $scopes |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | 2 | private function convertScopesQueryStringToArray($scopes) |
|
292 | |||
293 | /** |
||
294 | * Retrieve request parameter. |
||
295 | * |
||
296 | * @param string $parameter |
||
297 | * @param ServerRequestInterface $request |
||
298 | * @param mixed $default |
||
299 | * |
||
300 | * @return null|string |
||
301 | */ |
||
302 | 9 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
308 | |||
309 | /** |
||
310 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
311 | * of a request. First index of the returned array is the username, |
||
312 | * second is the password (so list() will work). If the header does |
||
313 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
314 | * [null, null]. |
||
315 | * |
||
316 | * @param ServerRequestInterface $request |
||
317 | * |
||
318 | * @return string[]|null[] |
||
319 | */ |
||
320 | 14 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
341 | |||
342 | /** |
||
343 | * Retrieve query string parameter. |
||
344 | * |
||
345 | * @param string $parameter |
||
346 | * @param ServerRequestInterface $request |
||
347 | * @param mixed $default |
||
348 | * |
||
349 | * @return null|string |
||
350 | */ |
||
351 | 2 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
355 | |||
356 | /** |
||
357 | * Retrieve cookie parameter. |
||
358 | * |
||
359 | * @param string $parameter |
||
360 | * @param ServerRequestInterface $request |
||
361 | * @param mixed $default |
||
362 | * |
||
363 | * @return null|string |
||
364 | */ |
||
365 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
||
369 | |||
370 | /** |
||
371 | * Retrieve server parameter. |
||
372 | * |
||
373 | * @param string $parameter |
||
374 | * @param ServerRequestInterface $request |
||
375 | * @param mixed $default |
||
376 | * |
||
377 | * @return null|string |
||
378 | */ |
||
379 | 2 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
383 | |||
384 | /** |
||
385 | * Issue an access token. |
||
386 | * |
||
387 | * @param DateInterval $accessTokenTTL |
||
388 | * @param ClientEntityInterface $client |
||
389 | * @param string|null $userIdentifier |
||
390 | * @param ScopeEntityInterface[] $scopes |
||
391 | * |
||
392 | * @throws OAuthServerException |
||
393 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
394 | * |
||
395 | * @return AccessTokenEntityInterface |
||
396 | */ |
||
397 | 1 | protected function issueAccessToken( |
|
427 | |||
428 | /** |
||
429 | * Issue an auth code. |
||
430 | * |
||
431 | * @param DateInterval $authCodeTTL |
||
432 | * @param ClientEntityInterface $client |
||
433 | * @param string $userIdentifier |
||
434 | * @param string|null $redirectUri |
||
435 | * @param ScopeEntityInterface[] $scopes |
||
436 | * |
||
437 | * @throws OAuthServerException |
||
438 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
439 | * |
||
440 | * @return AuthCodeEntityInterface |
||
441 | */ |
||
442 | 1 | protected function issueAuthCode( |
|
477 | |||
478 | /** |
||
479 | * @param AccessTokenEntityInterface $accessToken |
||
480 | * |
||
481 | * @throws OAuthServerException |
||
482 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
483 | * |
||
484 | * @return RefreshTokenEntityInterface |
||
485 | */ |
||
486 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
507 | |||
508 | /** |
||
509 | * {@inheritdoc} |
||
510 | */ |
||
511 | 3 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
520 | |||
521 | /** |
||
522 | * {@inheritdoc} |
||
523 | */ |
||
524 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
528 | |||
529 | /** |
||
530 | * {@inheritdoc} |
||
531 | */ |
||
532 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
536 | |||
537 | /** |
||
538 | * {@inheritdoc} |
||
539 | */ |
||
540 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
544 | } |
||
545 |