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 |
||
| 45 | abstract class AbstractGrant implements GrantTypeInterface |
||
| 46 | { |
||
| 47 | use EmitterAwareTrait, CryptTrait; |
||
| 48 | |||
| 49 | const SCOPE_DELIMITER_STRING = ' '; |
||
| 50 | |||
| 51 | const MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS = 10; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var ClientRepositoryInterface |
||
| 55 | */ |
||
| 56 | protected $clientRepository; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var AccessTokenRepositoryInterface |
||
| 60 | */ |
||
| 61 | protected $accessTokenRepository; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var ScopeRepositoryInterface |
||
| 65 | */ |
||
| 66 | protected $scopeRepository; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var AuthCodeRepositoryInterface |
||
| 70 | */ |
||
| 71 | protected $authCodeRepository; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var DeviceCodeRepositoryInterface |
||
| 75 | */ |
||
| 76 | protected $deviceCodeRepository; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var RefreshTokenRepositoryInterface |
||
| 80 | */ |
||
| 81 | protected $refreshTokenRepository; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var UserRepositoryInterface |
||
| 85 | */ |
||
| 86 | protected $userRepository; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var DateInterval |
||
| 90 | */ |
||
| 91 | protected $refreshTokenTTL; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var CryptKey |
||
| 95 | */ |
||
| 96 | protected $privateKey; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @string |
||
| 100 | */ |
||
| 101 | protected $defaultScope; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param ClientRepositoryInterface $clientRepository |
||
| 105 | */ |
||
| 106 | 78 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
| 113 | */ |
||
| 114 | 48 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param ScopeRepositoryInterface $scopeRepository |
||
| 121 | */ |
||
| 122 | 47 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
| 129 | */ |
||
| 130 | 72 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
| 137 | */ |
||
| 138 | 46 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param DeviceCodeRepositoryInterface $deviceCodeRepository |
||
| 145 | */ |
||
| 146 | 9 | public function setDeviceCodeRepository(DeviceCodeRepositoryInterface $deviceCodeRepository) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param UserRepositoryInterface $userRepository |
||
| 153 | */ |
||
| 154 | 6 | public function setUserRepository(UserRepositoryInterface $userRepository) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | 2 | public function setRefreshTokenTTL(DateInterval $refreshTokenTTL) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Set the private key |
||
| 169 | * |
||
| 170 | * @param CryptKey $key |
||
| 171 | */ |
||
| 172 | 37 | public function setPrivateKey(CryptKey $key) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $scope |
||
| 179 | */ |
||
| 180 | 22 | public function setDefaultScope($scope) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Validate the client. |
||
| 187 | * |
||
| 188 | * @param ServerRequestInterface $request |
||
| 189 | * |
||
| 190 | * @throws OAuthServerException |
||
| 191 | * |
||
| 192 | * @return ClientEntityInterface |
||
| 193 | */ |
||
| 194 | 44 | protected function validateClient(ServerRequestInterface $request) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Wrapper around ClientRepository::getClientEntity() that ensures we emit |
||
| 218 | * an event and throw an exception if the repo doesn't return a client |
||
| 219 | * entity. |
||
| 220 | * |
||
| 221 | * This is a bit of defensive coding because the interface contract |
||
| 222 | * doesn't actually enforce non-null returns/exception-on-no-client so |
||
| 223 | * getClientEntity might return null. By contrast, this method will |
||
| 224 | * always either return a ClientEntityInterface or throw. |
||
| 225 | * |
||
| 226 | * @param string $clientId |
||
| 227 | * @param ServerRequestInterface $request |
||
| 228 | * |
||
| 229 | * @return ClientEntityInterface |
||
| 230 | */ |
||
| 231 | 65 | protected function getClientEntityOrFail($clientId, ServerRequestInterface $request) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Gets the client credentials from the request from the request body or |
||
| 245 | * the Http Basic Authorization header |
||
| 246 | * |
||
| 247 | * @param ServerRequestInterface $request |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | 53 | protected function getClientCredentials(ServerRequestInterface $request) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Validate redirectUri from the request. |
||
| 268 | * If a redirect URI is provided ensure it matches what is pre-registered |
||
| 269 | * |
||
| 270 | * @param string $redirectUri |
||
| 271 | * @param ClientEntityInterface $client |
||
| 272 | * @param ServerRequestInterface $request |
||
| 273 | * |
||
| 274 | * @throws OAuthServerException |
||
| 275 | */ |
||
| 276 | 30 | protected function validateRedirectUri( |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Validate scopes in the request. |
||
| 296 | * |
||
| 297 | * @param string|array $scopes |
||
| 298 | * @param string $redirectUri |
||
| 299 | * |
||
| 300 | * @throws OAuthServerException |
||
| 301 | * |
||
| 302 | * @return ScopeEntityInterface[] |
||
| 303 | */ |
||
| 304 | 41 | public function validateScopes($scopes, $redirectUri = null) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Converts a scopes query string to an array to easily iterate for validation. |
||
| 327 | * |
||
| 328 | * @param string $scopes |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | 27 | private function convertScopesQueryStringToArray($scopes) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Retrieve request parameter. |
||
| 341 | * |
||
| 342 | * @param string $parameter |
||
| 343 | * @param ServerRequestInterface $request |
||
| 344 | * @param mixed $default |
||
| 345 | * |
||
| 346 | * @return null|string |
||
| 347 | */ |
||
| 348 | 56 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
| 357 | * of a request. First index of the returned array is the username, |
||
| 358 | * second is the password (so list() will work). If the header does |
||
| 359 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
| 360 | * [null, null]. |
||
| 361 | * |
||
| 362 | * @param ServerRequestInterface $request |
||
| 363 | * |
||
| 364 | * @return string[]|null[] |
||
| 365 | */ |
||
| 366 | 58 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Retrieve query string parameter. |
||
| 390 | * |
||
| 391 | * @param string $parameter |
||
| 392 | * @param ServerRequestInterface $request |
||
| 393 | * @param mixed $default |
||
| 394 | * |
||
| 395 | * @return null|string |
||
| 396 | */ |
||
| 397 | 21 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Retrieve cookie parameter. |
||
| 404 | * |
||
| 405 | * @param string $parameter |
||
| 406 | * @param ServerRequestInterface $request |
||
| 407 | * @param mixed $default |
||
| 408 | * |
||
| 409 | * @return null|string |
||
| 410 | */ |
||
| 411 | 1 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Retrieve server parameter. |
||
| 418 | * |
||
| 419 | * @param string $parameter |
||
| 420 | * @param ServerRequestInterface $request |
||
| 421 | * @param mixed $default |
||
| 422 | * |
||
| 423 | * @return null|string |
||
| 424 | */ |
||
| 425 | 23 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Issue an access token. |
||
| 432 | * |
||
| 433 | * @param DateInterval $accessTokenTTL |
||
| 434 | * @param ClientEntityInterface $client |
||
| 435 | * @param string|null $userIdentifier |
||
| 436 | * @param ScopeEntityInterface[] $scopes |
||
| 437 | * |
||
| 438 | * @throws OAuthServerException |
||
| 439 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 440 | * |
||
| 441 | * @return AccessTokenEntityInterface |
||
| 442 | */ |
||
| 443 | 23 | protected function issueAccessToken( |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Issue an auth code. |
||
| 471 | * |
||
| 472 | * @param DateInterval $authCodeTTL |
||
| 473 | * @param ClientEntityInterface $client |
||
| 474 | * @param string $userIdentifier |
||
| 475 | * @param string|null $redirectUri |
||
| 476 | * @param ScopeEntityInterface[] $scopes |
||
| 477 | * |
||
| 478 | * @throws OAuthServerException |
||
| 479 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 480 | * |
||
| 481 | * @return AuthCodeEntityInterface |
||
| 482 | */ |
||
| 483 | 6 | protected function issueAuthCode( |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Issue a device code. |
||
| 521 | * |
||
| 522 | * @param DateInterval $deviceCodeTTL |
||
| 523 | * @param ClientEntityInterface $client |
||
| 524 | * @param string $verificationUri |
||
| 525 | * @param ScopeEntityInterface[] $scopes |
||
| 526 | * |
||
| 527 | * @return DeviceCodeEntityInterface |
||
| 528 | * |
||
| 529 | * @throws OAuthServerException |
||
| 530 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 531 | */ |
||
| 532 | 1 | protected function issueDeviceCode( |
|
| 563 | |||
| 564 | /** |
||
| 565 | * @param AccessTokenEntityInterface $accessToken |
||
| 566 | * |
||
| 567 | * @throws OAuthServerException |
||
| 568 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 569 | * |
||
| 570 | * @return RefreshTokenEntityInterface|null |
||
| 571 | */ |
||
| 572 | 17 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Generate a new unique identifier. |
||
| 601 | * |
||
| 602 | * @param int $length |
||
| 603 | * |
||
| 604 | * @throws OAuthServerException |
||
| 605 | * |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | 32 | protected function generateUniqueIdentifier($length = 40) |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Generate a new unique user code. |
||
| 626 | * |
||
| 627 | * @param int $length |
||
| 628 | * |
||
| 629 | * @return string |
||
| 630 | * |
||
| 631 | * @throws OAuthServerException |
||
| 632 | */ |
||
| 633 | 1 | protected function generateUniqueUserCode($length = 8) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * {@inheritdoc} |
||
| 656 | */ |
||
| 657 | 5 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * {@inheritdoc} |
||
| 669 | */ |
||
| 670 | 1 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
| 674 | |||
| 675 | /** |
||
| 676 | * {@inheritdoc} |
||
| 677 | */ |
||
| 678 | 1 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
| 682 | |||
| 683 | /** |
||
| 684 | * {@inheritdoc} |
||
| 685 | */ |
||
| 686 | 1 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
| 690 | |||
| 691 | /** |
||
| 692 | * {@inheritdoc} |
||
| 693 | */ |
||
| 694 | public function canRespondToDeviceAuthorizationRequest(ServerRequestInterface $request) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * {@inheritdoc} |
||
| 701 | */ |
||
| 702 | public function validateDeviceAuthorizationRequest(ServerRequestInterface $request) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * {@inheritdoc} |
||
| 709 | */ |
||
| 710 | public function completeDeviceAuthorizationRequest(DeviceAuthorizationRequest $deviceAuthorizationRequest) |
||
| 714 | } |
||
| 715 |