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 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
||
96 | |||
97 | /** |
||
98 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
99 | */ |
||
100 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
||
104 | |||
105 | /** |
||
106 | * @param ScopeRepositoryInterface $scopeRepository |
||
107 | */ |
||
108 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
||
112 | |||
113 | /** |
||
114 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
115 | */ |
||
116 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
||
120 | |||
121 | /** |
||
122 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
123 | */ |
||
124 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
||
128 | |||
129 | /** |
||
130 | * @param UserRepositoryInterface $userRepository |
||
131 | */ |
||
132 | public function setUserRepository(UserRepositoryInterface $userRepository) |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function setRefreshTokenTTL(\DateInterval $refreshTokenTTL) |
||
144 | |||
145 | /** |
||
146 | * Set the private key |
||
147 | * |
||
148 | * @param \League\OAuth2\Server\CryptKey $key |
||
149 | */ |
||
150 | public function setPrivateKey(CryptKey $key) |
||
154 | |||
155 | /** |
||
156 | * @param string $scope |
||
157 | */ |
||
158 | 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 | 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) |
||
251 | |||
252 | /** |
||
253 | * Retrieve request parameter. |
||
254 | * |
||
255 | * @param string $parameter |
||
256 | * @param ServerRequestInterface $request |
||
257 | * @param mixed $default |
||
258 | * |
||
259 | * @return null|string |
||
260 | */ |
||
261 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
||
267 | |||
268 | /** |
||
269 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
270 | * of a request. First index of the returned array is the username, |
||
271 | * second is the password (so list() will work). If the header does |
||
272 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
273 | * [null, null]. |
||
274 | * |
||
275 | * @param ServerRequestInterface $request |
||
276 | * |
||
277 | * @return string[]|null[] |
||
278 | */ |
||
279 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
||
300 | |||
301 | /** |
||
302 | * Retrieve query string parameter. |
||
303 | * |
||
304 | * @param string $parameter |
||
305 | * @param ServerRequestInterface $request |
||
306 | * @param mixed $default |
||
307 | * |
||
308 | * @return null|string |
||
309 | */ |
||
310 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
||
314 | |||
315 | /** |
||
316 | * Retrieve cookie parameter. |
||
317 | * |
||
318 | * @param string $parameter |
||
319 | * @param ServerRequestInterface $request |
||
320 | * @param mixed $default |
||
321 | * |
||
322 | * @return null|string |
||
323 | */ |
||
324 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
||
328 | |||
329 | /** |
||
330 | * Retrieve server parameter. |
||
331 | * |
||
332 | * @param string $parameter |
||
333 | * @param ServerRequestInterface $request |
||
334 | * @param mixed $default |
||
335 | * |
||
336 | * @return null|string |
||
337 | */ |
||
338 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
||
342 | |||
343 | /** |
||
344 | * Issue an access token. |
||
345 | * |
||
346 | * @param \DateInterval $accessTokenTTL |
||
347 | * @param ClientEntityInterface $client |
||
348 | * @param string $userIdentifier |
||
349 | * @param ScopeEntityInterface[] $scopes |
||
350 | * |
||
351 | * @throws OAuthServerException |
||
352 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
353 | * |
||
354 | * @return AccessTokenEntityInterface |
||
355 | */ |
||
356 | protected function issueAccessToken( |
||
386 | |||
387 | /** |
||
388 | * Issue an auth code. |
||
389 | * |
||
390 | * @param \DateInterval $authCodeTTL |
||
391 | * @param ClientEntityInterface $client |
||
392 | * @param string $userIdentifier |
||
393 | * @param string $redirectUri |
||
394 | * @param ScopeEntityInterface[] $scopes |
||
395 | * |
||
396 | * @throws OAuthServerException |
||
397 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
398 | * |
||
399 | * @return AuthCodeEntityInterface |
||
400 | */ |
||
401 | protected function issueAuthCode( |
||
433 | |||
434 | /** |
||
435 | * @param AccessTokenEntityInterface $accessToken |
||
436 | * |
||
437 | * @throws OAuthServerException |
||
438 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
439 | * |
||
440 | * @return RefreshTokenEntityInterface |
||
441 | */ |
||
442 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
463 | |||
464 | /** |
||
465 | * Generate a new unique identifier. |
||
466 | * |
||
467 | * @param int $length |
||
468 | * |
||
469 | * @throws OAuthServerException |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | protected function generateUniqueIdentifier($length = 40) |
||
488 | |||
489 | /** |
||
490 | * {@inheritdoc} |
||
491 | */ |
||
492 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
||
501 | |||
502 | /** |
||
503 | * {@inheritdoc} |
||
504 | */ |
||
505 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
509 | |||
510 | /** |
||
511 | * {@inheritdoc} |
||
512 | */ |
||
513 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
517 | |||
518 | /** |
||
519 | * {@inheritdoc} |
||
520 | */ |
||
521 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
525 | } |
||
526 |