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) |
|
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 | 23 | public function validateScopes($scopes, $redirectUri = null) |
|
263 | |||
264 | /** |
||
265 | * Retrieve request parameter. |
||
266 | * |
||
267 | * @param string $parameter |
||
268 | * @param ServerRequestInterface $request |
||
269 | * @param mixed $default |
||
270 | * |
||
271 | * @return null|string |
||
272 | */ |
||
273 | 42 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
279 | |||
280 | /** |
||
281 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
282 | * of a request. First index of the returned array is the username, |
||
283 | * second is the password (so list() will work). If the header does |
||
284 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
285 | * [null, null]. |
||
286 | * |
||
287 | * @param ServerRequestInterface $request |
||
288 | * |
||
289 | * @return string[]|null[] |
||
290 | */ |
||
291 | 47 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
312 | |||
313 | /** |
||
314 | * Retrieve query string parameter. |
||
315 | * |
||
316 | * @param string $parameter |
||
317 | * @param ServerRequestInterface $request |
||
318 | * @param mixed $default |
||
319 | * |
||
320 | * @return null|string |
||
321 | */ |
||
322 | 21 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
326 | |||
327 | /** |
||
328 | * Retrieve cookie parameter. |
||
329 | * |
||
330 | * @param string $parameter |
||
331 | * @param ServerRequestInterface $request |
||
332 | * @param mixed $default |
||
333 | * |
||
334 | * @return null|string |
||
335 | */ |
||
336 | 1 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
340 | |||
341 | /** |
||
342 | * Retrieve server parameter. |
||
343 | * |
||
344 | * @param string $parameter |
||
345 | * @param ServerRequestInterface $request |
||
346 | * @param mixed $default |
||
347 | * |
||
348 | * @return null|string |
||
349 | */ |
||
350 | 20 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
354 | |||
355 | /** |
||
356 | * Issue an access token. |
||
357 | * |
||
358 | * @param \DateInterval $accessTokenTTL |
||
359 | * @param ClientEntityInterface $client |
||
360 | * @param string|null $userIdentifier |
||
361 | * @param ScopeEntityInterface[] $scopes |
||
362 | * |
||
363 | * @throws OAuthServerException |
||
364 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
365 | * |
||
366 | * @return AccessTokenEntityInterface |
||
367 | */ |
||
368 | 17 | protected function issueAccessToken( |
|
392 | |||
393 | /** |
||
394 | * Issue an auth code. |
||
395 | * |
||
396 | * @param \DateInterval $authCodeTTL |
||
397 | * @param ClientEntityInterface $client |
||
398 | * @param string $userIdentifier |
||
399 | * @param string|null $redirectUri |
||
400 | * @param ScopeEntityInterface[] $scopes |
||
401 | * |
||
402 | * @throws OAuthServerException |
||
403 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
404 | * |
||
405 | * @return AuthCodeEntityInterface |
||
406 | */ |
||
407 | 6 | protected function issueAuthCode( |
|
442 | |||
443 | /** |
||
444 | * @param AccessTokenEntityInterface $accessToken |
||
445 | * |
||
446 | * @throws OAuthServerException |
||
447 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
448 | * |
||
449 | * @return RefreshTokenEntityInterface |
||
450 | */ |
||
451 | 10 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
472 | |||
473 | /** |
||
474 | * Generate a new unique identifier. |
||
475 | * |
||
476 | * @param int $length |
||
477 | * |
||
478 | * @throws OAuthServerException |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | 25 | protected function generateUniqueIdentifier($length = 40) |
|
497 | |||
498 | /** |
||
499 | * {@inheritdoc} |
||
500 | */ |
||
501 | 5 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
510 | |||
511 | /** |
||
512 | * {@inheritdoc} |
||
513 | */ |
||
514 | 1 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
518 | |||
519 | /** |
||
520 | * {@inheritdoc} |
||
521 | */ |
||
522 | 1 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
526 | |||
527 | /** |
||
528 | * {@inheritdoc} |
||
529 | */ |
||
530 | 1 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
534 | } |
||
535 |