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 | 64 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
| 99 | */ |
||
| 100 | 42 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param ScopeRepositoryInterface $scopeRepository |
||
| 107 | */ |
||
| 108 | 32 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
| 115 | */ |
||
| 116 | 56 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
| 123 | */ |
||
| 124 | 42 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param UserRepositoryInterface $userRepository |
||
| 131 | */ |
||
| 132 | 5 | public function setUserRepository(UserRepositoryInterface $userRepository) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | 1 | public function setRefreshTokenTTL(\DateInterval $refreshTokenTTL) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Set the private key |
||
| 147 | * |
||
| 148 | * @param \League\OAuth2\Server\CryptKey $key |
||
| 149 | */ |
||
| 150 | 20 | public function setPrivateKey(CryptKey $key) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $scope |
||
| 157 | */ |
||
| 158 | 16 | 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 | 42 | protected function validateClient(ServerRequestInterface $request) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Validate redirectUri from the request. |
||
| 207 | * If a redirect URI is provided ensure it matches what is pre-registered |
||
| 208 | * |
||
| 209 | * @param string $redirectUri |
||
| 210 | * @param ClientEntityInterface $client |
||
| 211 | * @param ServerRequestInterface $request |
||
| 212 | * |
||
| 213 | * @throws OAuthServerException |
||
| 214 | * |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | 34 | protected function validateRedirectUri( |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Validate scopes in the request. |
||
| 237 | * |
||
| 238 | * @param string $scopes |
||
| 239 | * @param string $redirectUri |
||
| 240 | * |
||
| 241 | * @throws OAuthServerException |
||
| 242 | * |
||
| 243 | * @return ScopeEntityInterface[] |
||
| 244 | */ |
||
| 245 | public function validateScopes($scopes, $redirectUri = null) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Retrieve request parameter. |
||
| 268 | * |
||
| 269 | * @param string $parameter |
||
| 270 | * @param ServerRequestInterface $request |
||
| 271 | * @param mixed $default |
||
| 272 | * |
||
| 273 | * @return null|string |
||
| 274 | */ |
||
| 275 | 42 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
| 284 | * of a request. First index of the returned array is the username, |
||
| 285 | * second is the password (so list() will work). If the header does |
||
| 286 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
| 287 | * [null, null]. |
||
| 288 | * |
||
| 289 | * @param ServerRequestInterface $request |
||
| 290 | * |
||
| 291 | * @return string[]|null[] |
||
| 292 | */ |
||
| 293 | 47 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Retrieve query string parameter. |
||
| 317 | * |
||
| 318 | * @param string $parameter |
||
| 319 | * @param ServerRequestInterface $request |
||
| 320 | * @param mixed $default |
||
| 321 | * |
||
| 322 | * @return null|string |
||
| 323 | */ |
||
| 324 | 21 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Retrieve cookie parameter. |
||
| 331 | * |
||
| 332 | * @param string $parameter |
||
| 333 | * @param ServerRequestInterface $request |
||
| 334 | * @param mixed $default |
||
| 335 | * |
||
| 336 | * @return null|string |
||
| 337 | */ |
||
| 338 | 1 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Retrieve server parameter. |
||
| 345 | * |
||
| 346 | * @param string $parameter |
||
| 347 | * @param ServerRequestInterface $request |
||
| 348 | * @param mixed $default |
||
| 349 | * |
||
| 350 | * @return null|string |
||
| 351 | */ |
||
| 352 | 20 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Issue an access token. |
||
| 359 | * |
||
| 360 | * @param \DateInterval $accessTokenTTL |
||
| 361 | * @param ClientEntityInterface $client |
||
| 362 | * @param string|null $userIdentifier |
||
| 363 | * @param ScopeEntityInterface[] $scopes |
||
| 364 | * |
||
| 365 | * @throws OAuthServerException |
||
| 366 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 367 | * |
||
| 368 | * @return AccessTokenEntityInterface |
||
| 369 | */ |
||
| 370 | 17 | protected function issueAccessToken( |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Issue an auth code. |
||
| 403 | * |
||
| 404 | * @param \DateInterval $authCodeTTL |
||
| 405 | * @param ClientEntityInterface $client |
||
| 406 | * @param string $userIdentifier |
||
| 407 | * @param string|null $redirectUri |
||
| 408 | * @param ScopeEntityInterface[] $scopes |
||
| 409 | * |
||
| 410 | * @throws OAuthServerException |
||
| 411 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 412 | * |
||
| 413 | * @return AuthCodeEntityInterface |
||
| 414 | */ |
||
| 415 | 6 | protected function issueAuthCode( |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @param AccessTokenEntityInterface $accessToken |
||
| 453 | * |
||
| 454 | * @throws OAuthServerException |
||
| 455 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 456 | * |
||
| 457 | * @return RefreshTokenEntityInterface |
||
| 458 | */ |
||
| 459 | 10 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Generate a new unique identifier. |
||
| 483 | * |
||
| 484 | * @param int $length |
||
| 485 | * |
||
| 486 | * @throws OAuthServerException |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | 25 | protected function generateUniqueIdentifier($length = 40) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * {@inheritdoc} |
||
| 508 | */ |
||
| 509 | 5 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * {@inheritdoc} |
||
| 521 | */ |
||
| 522 | 1 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * {@inheritdoc} |
||
| 529 | */ |
||
| 530 | 1 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * {@inheritdoc} |
||
| 537 | */ |
||
| 538 | 1 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
|
| 542 | } |
||
| 543 |