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 | * @param ClientRepositoryInterface $clientRepository |
||
86 | */ |
||
87 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
||
91 | |||
92 | /** |
||
93 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
94 | */ |
||
95 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
||
99 | |||
100 | /** |
||
101 | * @param ScopeRepositoryInterface $scopeRepository |
||
102 | */ |
||
103 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
||
107 | |||
108 | /** |
||
109 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
110 | */ |
||
111 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
||
115 | |||
116 | /** |
||
117 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
118 | */ |
||
119 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
||
123 | |||
124 | /** |
||
125 | * @param UserRepositoryInterface $userRepository |
||
126 | */ |
||
127 | public function setUserRepository(UserRepositoryInterface $userRepository) |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function setRefreshTokenTTL(\DateInterval $refreshTokenTTL) |
||
139 | |||
140 | /** |
||
141 | * Set the private key |
||
142 | * |
||
143 | * @param \League\OAuth2\Server\CryptKey $key |
||
144 | */ |
||
145 | public function setPrivateKey(CryptKey $key) |
||
149 | |||
150 | /** |
||
151 | * Validate the client. |
||
152 | * |
||
153 | * @param ServerRequestInterface $request |
||
154 | * |
||
155 | * @throws OAuthServerException |
||
156 | * |
||
157 | * @return ClientEntityInterface |
||
158 | */ |
||
159 | protected function validateClient(ServerRequestInterface $request) |
||
203 | |||
204 | /** |
||
205 | * Validate scopes in the request. |
||
206 | * |
||
207 | * @param string $scopes |
||
208 | * @param string $redirectUri |
||
209 | * |
||
210 | * @throws OAuthServerException |
||
211 | * |
||
212 | * @return ScopeEntityInterface[] |
||
213 | */ |
||
214 | public function validateScopes( |
||
238 | |||
239 | /** |
||
240 | * Retrieve request parameter. |
||
241 | * |
||
242 | * @param string $parameter |
||
243 | * @param ServerRequestInterface $request |
||
244 | * @param mixed $default |
||
245 | * |
||
246 | * @return null|string |
||
247 | */ |
||
248 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
||
254 | |||
255 | /** |
||
256 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
257 | * of a request. First index of the returned array is the username, |
||
258 | * second is the password (so list() will work). If the header does |
||
259 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
260 | * [null, null]. |
||
261 | * |
||
262 | * @param ServerRequestInterface $request |
||
263 | * |
||
264 | * @return string[]|null[] |
||
265 | */ |
||
266 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
||
287 | |||
288 | /** |
||
289 | * Retrieve query string parameter. |
||
290 | * |
||
291 | * @param string $parameter |
||
292 | * @param ServerRequestInterface $request |
||
293 | * @param mixed $default |
||
294 | * |
||
295 | * @return null|string |
||
296 | */ |
||
297 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
||
301 | |||
302 | /** |
||
303 | * Retrieve cookie parameter. |
||
304 | * |
||
305 | * @param string $parameter |
||
306 | * @param ServerRequestInterface $request |
||
307 | * @param mixed $default |
||
308 | * |
||
309 | * @return null|string |
||
310 | */ |
||
311 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
||
315 | |||
316 | /** |
||
317 | * Retrieve server parameter. |
||
318 | * |
||
319 | * @param string $parameter |
||
320 | * @param ServerRequestInterface $request |
||
321 | * @param mixed $default |
||
322 | * |
||
323 | * @return null|string |
||
324 | */ |
||
325 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
||
329 | |||
330 | /** |
||
331 | * Issue an access token. |
||
332 | * |
||
333 | * @param \DateInterval $accessTokenTTL |
||
334 | * @param ClientEntityInterface $client |
||
335 | * @param string $userIdentifier |
||
336 | * @param ScopeEntityInterface[] $scopes |
||
337 | * |
||
338 | * @throws OAuthServerException |
||
339 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
340 | * |
||
341 | * @return AccessTokenEntityInterface |
||
342 | */ |
||
343 | protected function issueAccessToken( |
||
373 | |||
374 | /** |
||
375 | * Issue an auth code. |
||
376 | * |
||
377 | * @param \DateInterval $authCodeTTL |
||
378 | * @param ClientEntityInterface $client |
||
379 | * @param string $userIdentifier |
||
380 | * @param string $redirectUri |
||
381 | * @param ScopeEntityInterface[] $scopes |
||
382 | * |
||
383 | * @throws OAuthServerException |
||
384 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
385 | * |
||
386 | * @return AuthCodeEntityInterface |
||
387 | */ |
||
388 | protected function issueAuthCode( |
||
420 | |||
421 | /** |
||
422 | * @param AccessTokenEntityInterface $accessToken |
||
423 | * |
||
424 | * @throws OAuthServerException |
||
425 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
426 | * |
||
427 | * @return RefreshTokenEntityInterface |
||
428 | */ |
||
429 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
450 | |||
451 | /** |
||
452 | * Generate a new unique identifier. |
||
453 | * |
||
454 | * @param int $length |
||
455 | * |
||
456 | * @throws OAuthServerException |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | protected function generateUniqueIdentifier($length = 40) |
||
475 | |||
476 | /** |
||
477 | * {@inheritdoc} |
||
478 | */ |
||
479 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
||
488 | |||
489 | /** |
||
490 | * {@inheritdoc} |
||
491 | */ |
||
492 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
496 | |||
497 | /** |
||
498 | * {@inheritdoc} |
||
499 | */ |
||
500 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
504 | |||
505 | /** |
||
506 | * {@inheritdoc} |
||
507 | */ |
||
508 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
512 | } |
||
513 |