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 | 32 | 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) |
|
216 | |||
217 | /** |
||
218 | * Validate scopes in the request. |
||
219 | * |
||
220 | * @param string $scopes |
||
221 | * @param string $redirectUri |
||
222 | * |
||
223 | * @throws OAuthServerException |
||
224 | * |
||
225 | * @return ScopeEntityInterface[] |
||
226 | */ |
||
227 | public function validateScopes($scopes, $redirectUri = null) |
||
247 | |||
248 | /** |
||
249 | * Retrieve request parameter. |
||
250 | * |
||
251 | * @param string $parameter |
||
252 | * @param ServerRequestInterface $request |
||
253 | * @param mixed $default |
||
254 | * |
||
255 | * @return null|string |
||
256 | */ |
||
257 | 42 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
263 | |||
264 | /** |
||
265 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
266 | * of a request. First index of the returned array is the username, |
||
267 | * second is the password (so list() will work). If the header does |
||
268 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
269 | * [null, null]. |
||
270 | * |
||
271 | * @param ServerRequestInterface $request |
||
272 | * |
||
273 | * @return string[]|null[] |
||
274 | */ |
||
275 | 47 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
296 | |||
297 | /** |
||
298 | * Retrieve query string parameter. |
||
299 | * |
||
300 | * @param string $parameter |
||
301 | * @param ServerRequestInterface $request |
||
302 | * @param mixed $default |
||
303 | * |
||
304 | * @return null|string |
||
305 | */ |
||
306 | 21 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
310 | |||
311 | /** |
||
312 | * Retrieve cookie parameter. |
||
313 | * |
||
314 | * @param string $parameter |
||
315 | * @param ServerRequestInterface $request |
||
316 | * @param mixed $default |
||
317 | * |
||
318 | * @return null|string |
||
319 | */ |
||
320 | 1 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
324 | |||
325 | /** |
||
326 | * Retrieve server parameter. |
||
327 | * |
||
328 | * @param string $parameter |
||
329 | * @param ServerRequestInterface $request |
||
330 | * @param mixed $default |
||
331 | * |
||
332 | * @return null|string |
||
333 | */ |
||
334 | 20 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
338 | |||
339 | /** |
||
340 | * Issue an access token. |
||
341 | * |
||
342 | * @param \DateInterval $accessTokenTTL |
||
343 | * @param ClientEntityInterface $client |
||
344 | * @param string|null $userIdentifier |
||
345 | * @param ScopeEntityInterface[] $scopes |
||
346 | * |
||
347 | * @throws OAuthServerException |
||
348 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
349 | * |
||
350 | * @return AccessTokenEntityInterface |
||
351 | */ |
||
352 | 17 | protected function issueAccessToken( |
|
382 | |||
383 | /** |
||
384 | * Issue an auth code. |
||
385 | * |
||
386 | * @param \DateInterval $authCodeTTL |
||
387 | * @param ClientEntityInterface $client |
||
388 | * @param string $userIdentifier |
||
389 | * @param string $redirectUri |
||
390 | * @param ScopeEntityInterface[] $scopes |
||
391 | * |
||
392 | * @throws OAuthServerException |
||
393 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
394 | * |
||
395 | * @return AuthCodeEntityInterface |
||
396 | */ |
||
397 | 6 | protected function issueAuthCode( |
|
429 | |||
430 | /** |
||
431 | * @param AccessTokenEntityInterface $accessToken |
||
432 | * |
||
433 | * @throws OAuthServerException |
||
434 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
435 | * |
||
436 | * @return RefreshTokenEntityInterface |
||
437 | */ |
||
438 | 10 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
459 | |||
460 | /** |
||
461 | * Generate a new unique identifier. |
||
462 | * |
||
463 | * @param int $length |
||
464 | * |
||
465 | * @throws OAuthServerException |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | 25 | protected function generateUniqueIdentifier($length = 40) |
|
484 | |||
485 | /** |
||
486 | * {@inheritdoc} |
||
487 | */ |
||
488 | 5 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
497 | |||
498 | /** |
||
499 | * {@inheritdoc} |
||
500 | */ |
||
501 | 1 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
505 | |||
506 | /** |
||
507 | * {@inheritdoc} |
||
508 | */ |
||
509 | 1 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
513 | |||
514 | /** |
||
515 | * {@inheritdoc} |
||
516 | */ |
||
517 | 1 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
521 | } |
||
522 |