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 | * @var 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( |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Issue an auth code. |
||
| 390 | * |
||
| 391 | * @param \DateInterval $authCodeTTL |
||
| 392 | * @param ClientEntityInterface $client |
||
| 393 | * @param string $userIdentifier |
||
| 394 | * @param string $redirectUri |
||
| 395 | * @param ScopeEntityInterface[] $scopes |
||
| 396 | * |
||
| 397 | * @throws OAuthServerException |
||
| 398 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 399 | * |
||
| 400 | * @return AuthCodeEntityInterface |
||
| 401 | */ |
||
| 402 | protected function issueAuthCode( |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param AccessTokenEntityInterface $accessToken |
||
| 437 | * |
||
| 438 | * @throws OAuthServerException |
||
| 439 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 440 | * |
||
| 441 | * @return RefreshTokenEntityInterface |
||
| 442 | */ |
||
| 443 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Generate a new unique identifier. |
||
| 468 | * |
||
| 469 | * @param int $length |
||
| 470 | * |
||
| 471 | * @throws OAuthServerException |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | protected function generateUniqueIdentifier($length = 40) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * {@inheritdoc} |
||
| 493 | */ |
||
| 494 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * {@inheritdoc} |
||
| 506 | */ |
||
| 507 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * {@inheritdoc} |
||
| 514 | */ |
||
| 515 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * {@inheritdoc} |
||
| 522 | */ |
||
| 523 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
| 527 | } |
||
| 528 |