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 |
||
33 | abstract class AbstractGrant implements GrantTypeInterface |
||
34 | { |
||
35 | use EmitterAwareTrait, CryptTrait; |
||
36 | |||
37 | const SCOPE_DELIMITER_STRING = ' '; |
||
38 | |||
39 | const MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS = 10; |
||
40 | |||
41 | /** |
||
42 | * @var ClientRepositoryInterface |
||
43 | */ |
||
44 | protected $clientRepository; |
||
45 | |||
46 | /** |
||
47 | * @var AccessTokenRepositoryInterface |
||
48 | */ |
||
49 | protected $accessTokenRepository; |
||
50 | |||
51 | /** |
||
52 | * @var ScopeRepositoryInterface |
||
53 | */ |
||
54 | protected $scopeRepository; |
||
55 | |||
56 | /** |
||
57 | * @var \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface |
||
58 | */ |
||
59 | protected $authCodeRepository; |
||
60 | |||
61 | /** |
||
62 | * @var \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface |
||
63 | */ |
||
64 | protected $refreshTokenRepository; |
||
65 | |||
66 | /** |
||
67 | * @var \League\OAuth2\Server\Repositories\UserRepositoryInterface |
||
68 | */ |
||
69 | protected $userRepository; |
||
70 | |||
71 | /** |
||
72 | * @var \DateInterval |
||
73 | */ |
||
74 | protected $refreshTokenTTL; |
||
75 | |||
76 | /** |
||
77 | * @param ClientRepositoryInterface $clientRepository |
||
78 | */ |
||
79 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
||
83 | |||
84 | /** |
||
85 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
86 | */ |
||
87 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
||
91 | |||
92 | /** |
||
93 | * @param ScopeRepositoryInterface $scopeRepository |
||
94 | */ |
||
95 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
||
99 | |||
100 | /** |
||
101 | * @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository |
||
102 | */ |
||
103 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
||
107 | |||
108 | /** |
||
109 | * @param \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface $authCodeRepository |
||
110 | */ |
||
111 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
||
115 | |||
116 | /** |
||
117 | * @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository |
||
118 | */ |
||
119 | public function setUserRepository(UserRepositoryInterface $userRepository) |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function setRefreshTokenTTL(\DateInterval $refreshTokenTTL) |
||
131 | |||
132 | /** |
||
133 | * Validate the client. |
||
134 | * |
||
135 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
136 | * |
||
137 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
138 | * |
||
139 | * @return \League\OAuth2\Server\Entities\ClientEntityInterface |
||
140 | */ |
||
141 | protected function validateClient(ServerRequestInterface $request) |
||
191 | |||
192 | /** |
||
193 | * Validate scopes in the request. |
||
194 | * |
||
195 | * @param string $scopes |
||
196 | * @param string $redirectUri |
||
197 | * |
||
198 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
199 | * |
||
200 | * @return \League\OAuth2\Server\Entities\ScopeEntityInterface[] |
||
201 | */ |
||
202 | public function validateScopes( |
||
226 | |||
227 | /** |
||
228 | * Retrieve request parameter. |
||
229 | * |
||
230 | * @param string $parameter |
||
231 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
232 | * @param mixed $default |
||
233 | * |
||
234 | * @return null|string |
||
235 | */ |
||
236 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
||
242 | |||
243 | /** |
||
244 | * Retrieve query string parameter. |
||
245 | * |
||
246 | * @param string $parameter |
||
247 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
248 | * @param mixed $default |
||
249 | * |
||
250 | * @return null|string |
||
251 | */ |
||
252 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
||
256 | |||
257 | /** |
||
258 | * Retrieve cookie parameter. |
||
259 | * |
||
260 | * @param string $parameter |
||
261 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
262 | * @param mixed $default |
||
263 | * |
||
264 | * @return null|string |
||
265 | */ |
||
266 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
||
270 | |||
271 | /** |
||
272 | * Retrieve server parameter. |
||
273 | * |
||
274 | * @param string $parameter |
||
275 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
276 | * @param mixed $default |
||
277 | * |
||
278 | * @return null|string |
||
279 | */ |
||
280 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
||
284 | |||
285 | /** |
||
286 | * Issue an access token. |
||
287 | * |
||
288 | * @param \DateInterval $accessTokenTTL |
||
289 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
||
290 | * @param string $userIdentifier |
||
291 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
292 | * |
||
293 | * @return \League\OAuth2\Server\Entities\AccessTokenEntityInterface |
||
294 | */ |
||
295 | protected function issueAccessToken( |
||
296 | \DateInterval $accessTokenTTL, |
||
297 | ClientEntityInterface $client, |
||
298 | $userIdentifier, |
||
299 | array $scopes = [] |
||
300 | ) { |
||
301 | $maxGenerationAttempts = self::MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS; |
||
302 | |||
303 | $accessToken = $this->accessTokenRepository->getNewToken($client, $scopes, $userIdentifier); |
||
304 | $accessToken->setClient($client); |
||
305 | $accessToken->setUserIdentifier($userIdentifier); |
||
306 | $accessToken->setExpiryDateTime((new \DateTime())->add($accessTokenTTL)); |
||
307 | |||
308 | foreach ($scopes as $scope) { |
||
309 | $accessToken->addScope($scope); |
||
310 | } |
||
311 | |||
312 | while ($maxGenerationAttempts-- > 0) { |
||
313 | $accessToken->setIdentifier($this->generateUniqueIdentifier()); |
||
314 | try { |
||
315 | $this->accessTokenRepository->persistNewAccessToken($accessToken); |
||
316 | return $accessToken; |
||
317 | } catch (UniqueTokenIdentifierConstraintViolationException $e) { |
||
318 | if ($maxGenerationAttempts === 0) { |
||
319 | throw $e; |
||
320 | } |
||
321 | } |
||
322 | } |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Issue an auth code. |
||
327 | * |
||
328 | * @param \DateInterval $authCodeTTL |
||
329 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
||
330 | * @param string $userIdentifier |
||
331 | * @param string $redirectUri |
||
332 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
333 | * |
||
334 | * @return \League\OAuth2\Server\Entities\AuthCodeEntityInterface |
||
335 | */ |
||
336 | protected function issueAuthCode( |
||
367 | |||
368 | /** |
||
369 | * @param \League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessToken |
||
370 | * |
||
371 | * @return \League\OAuth2\Server\Entities\RefreshTokenEntityInterface |
||
372 | */ |
||
373 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
393 | |||
394 | /** |
||
395 | * Generate a new unique identifier. |
||
396 | * |
||
397 | * @param int $length |
||
398 | * |
||
399 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | protected function generateUniqueIdentifier($length = 40) |
||
418 | |||
419 | /** |
||
420 | * {@inheritdoc} |
||
421 | */ |
||
422 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
||
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
447 | |||
448 | /** |
||
449 | * {@inheritdoc} |
||
450 | */ |
||
451 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
455 | } |
||
456 |