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 |
||
40 | abstract class AbstractGrant implements GrantTypeInterface |
||
41 | { |
||
42 | use EmitterAwareTrait, CryptTrait; |
||
43 | |||
44 | const SCOPE_DELIMITER_STRING = ' '; |
||
45 | |||
46 | const MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS = 10; |
||
47 | |||
48 | /** |
||
49 | * @var ClientRepositoryInterface |
||
50 | */ |
||
51 | protected $clientRepository; |
||
52 | |||
53 | /** |
||
54 | * @var AccessTokenRepositoryInterface |
||
55 | */ |
||
56 | protected $accessTokenRepository; |
||
57 | |||
58 | /** |
||
59 | * @var ScopeRepositoryInterface |
||
60 | */ |
||
61 | protected $scopeRepository; |
||
62 | |||
63 | /** |
||
64 | * @var AuthCodeRepositoryInterface |
||
65 | */ |
||
66 | protected $authCodeRepository; |
||
67 | |||
68 | /** |
||
69 | * @var RefreshTokenRepositoryInterface |
||
70 | */ |
||
71 | protected $refreshTokenRepository; |
||
72 | |||
73 | /** |
||
74 | * @var UserRepositoryInterface |
||
75 | */ |
||
76 | protected $userRepository; |
||
77 | |||
78 | /** |
||
79 | * @var DateInterval |
||
80 | */ |
||
81 | protected $refreshTokenTTL; |
||
82 | |||
83 | /** |
||
84 | * @var CryptKey |
||
85 | */ |
||
86 | protected $privateKey; |
||
87 | |||
88 | /** |
||
89 | * @string |
||
90 | */ |
||
91 | protected $defaultScope; |
||
92 | |||
93 | /** |
||
94 | * @var IdentifierGeneratorInterface |
||
95 | */ |
||
96 | protected $identifierGenerator; |
||
97 | |||
98 | /** |
||
99 | * @param ClientRepositoryInterface $clientRepository |
||
100 | */ |
||
101 | 64 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
|
105 | |||
106 | /** |
||
107 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
108 | */ |
||
109 | 42 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
|
113 | |||
114 | /** |
||
115 | * @param ScopeRepositoryInterface $scopeRepository |
||
116 | */ |
||
117 | 37 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
|
121 | |||
122 | /** |
||
123 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
124 | */ |
||
125 | 56 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
|
129 | |||
130 | /** |
||
131 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
132 | */ |
||
133 | 42 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
|
137 | |||
138 | /** |
||
139 | * @param UserRepositoryInterface $userRepository |
||
140 | */ |
||
141 | 5 | public function setUserRepository(UserRepositoryInterface $userRepository) |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 1 | public function setRefreshTokenTTL(DateInterval $refreshTokenTTL) |
|
153 | |||
154 | /** |
||
155 | * Set the private key |
||
156 | * |
||
157 | * @param CryptKey $key |
||
158 | */ |
||
159 | 20 | public function setPrivateKey(CryptKey $key) |
|
163 | |||
164 | /** |
||
165 | * @param string $scope |
||
166 | */ |
||
167 | 16 | public function setDefaultScope($scope) |
|
171 | |||
172 | /** |
||
173 | * @param IdentifierGeneratorInterface $identifierGenerator |
||
174 | */ |
||
175 | 28 | public function setIdentifierGenerator(IdentifierGeneratorInterface $identifierGenerator) |
|
179 | |||
180 | /** |
||
181 | * Validate the client. |
||
182 | * |
||
183 | * @param ServerRequestInterface $request |
||
184 | * |
||
185 | * @throws OAuthServerException |
||
186 | * |
||
187 | * @return ClientEntityInterface |
||
188 | */ |
||
189 | 42 | protected function validateClient(ServerRequestInterface $request) |
|
221 | |||
222 | /** |
||
223 | * Validate redirectUri from the request. |
||
224 | * If a redirect URI is provided ensure it matches what is pre-registered |
||
225 | * |
||
226 | * @param string $redirectUri |
||
227 | * @param ClientEntityInterface $client |
||
228 | * @param ServerRequestInterface $request |
||
229 | * |
||
230 | * @throws OAuthServerException |
||
231 | */ |
||
232 | 34 | protected function validateRedirectUri( |
|
249 | |||
250 | /** |
||
251 | * Validate scopes in the request. |
||
252 | * |
||
253 | * @param string|array $scopes |
||
254 | * @param string $redirectUri |
||
255 | * |
||
256 | * @throws OAuthServerException |
||
257 | * |
||
258 | * @return ScopeEntityInterface[] |
||
259 | */ |
||
260 | 34 | public function validateScopes($scopes, $redirectUri = null) |
|
280 | |||
281 | /** |
||
282 | * Converts a scopes query string to an array to easily iterate for validation. |
||
283 | * |
||
284 | * @param string $scopes |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | private function convertScopesQueryStringToArray($scopes) |
||
294 | |||
295 | /** |
||
296 | * Retrieve request parameter. |
||
297 | * |
||
298 | * @param string $parameter |
||
299 | * @param ServerRequestInterface $request |
||
300 | * @param mixed $default |
||
301 | * |
||
302 | * @return null|string |
||
303 | */ |
||
304 | 42 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
310 | |||
311 | /** |
||
312 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
313 | * of a request. First index of the returned array is the username, |
||
314 | * second is the password (so list() will work). If the header does |
||
315 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
316 | * [null, null]. |
||
317 | * |
||
318 | * @param ServerRequestInterface $request |
||
319 | * |
||
320 | * @return string[]|null[] |
||
321 | */ |
||
322 | 47 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
343 | |||
344 | /** |
||
345 | * Retrieve query string parameter. |
||
346 | * |
||
347 | * @param string $parameter |
||
348 | * @param ServerRequestInterface $request |
||
349 | * @param mixed $default |
||
350 | * |
||
351 | * @return null|string |
||
352 | */ |
||
353 | 21 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
357 | |||
358 | /** |
||
359 | * Retrieve cookie parameter. |
||
360 | * |
||
361 | * @param string $parameter |
||
362 | * @param ServerRequestInterface $request |
||
363 | * @param mixed $default |
||
364 | * |
||
365 | * @return null|string |
||
366 | */ |
||
367 | 1 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
371 | |||
372 | /** |
||
373 | * Retrieve server parameter. |
||
374 | * |
||
375 | * @param string $parameter |
||
376 | * @param ServerRequestInterface $request |
||
377 | * @param mixed $default |
||
378 | * |
||
379 | * @return null|string |
||
380 | */ |
||
381 | 20 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
385 | |||
386 | /** |
||
387 | * Issue an access token. |
||
388 | * |
||
389 | * @param DateInterval $accessTokenTTL |
||
390 | * @param ClientEntityInterface $client |
||
391 | * @param string|null $userIdentifier |
||
392 | * @param ScopeEntityInterface[] $scopes |
||
393 | * |
||
394 | * @throws OAuthServerException |
||
395 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
396 | * |
||
397 | * @return AccessTokenEntityInterface |
||
398 | */ |
||
399 | 17 | protected function issueAccessToken( |
|
429 | |||
430 | /** |
||
431 | * Issue an auth code. |
||
432 | * |
||
433 | * @param DateInterval $authCodeTTL |
||
434 | * @param ClientEntityInterface $client |
||
435 | * @param string $userIdentifier |
||
436 | * @param string|null $redirectUri |
||
437 | * @param ScopeEntityInterface[] $scopes |
||
438 | * |
||
439 | * @throws OAuthServerException |
||
440 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
441 | * |
||
442 | * @return AuthCodeEntityInterface |
||
443 | */ |
||
444 | 6 | protected function issueAuthCode( |
|
479 | |||
480 | /** |
||
481 | * @param AccessTokenEntityInterface $accessToken |
||
482 | * |
||
483 | * @throws OAuthServerException |
||
484 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
485 | * |
||
486 | * @return RefreshTokenEntityInterface |
||
487 | */ |
||
488 | 10 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
509 | |||
510 | /** |
||
511 | * {@inheritdoc} |
||
512 | */ |
||
513 | 5 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
522 | |||
523 | /** |
||
524 | * {@inheritdoc} |
||
525 | */ |
||
526 | 1 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
530 | |||
531 | /** |
||
532 | * {@inheritdoc} |
||
533 | */ |
||
534 | 1 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
538 | |||
539 | /** |
||
540 | * {@inheritdoc} |
||
541 | */ |
||
542 | 1 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
546 | } |
||
547 |