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) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Validate scopes in the request. |
||
| 188 | * |
||
| 189 | * @param string $scopes |
||
| 190 | * @param string $redirectUri |
||
| 191 | * |
||
| 192 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
| 193 | * |
||
| 194 | * @return \League\OAuth2\Server\Entities\ScopeEntityInterface[] |
||
| 195 | */ |
||
| 196 | public function validateScopes( |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Retrieve request parameter. |
||
| 223 | * |
||
| 224 | * @param string $parameter |
||
| 225 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 226 | * @param mixed $default |
||
| 227 | * |
||
| 228 | * @return null|string |
||
| 229 | */ |
||
| 230 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
| 239 | * of a request. First index of the returned array is the username, |
||
| 240 | * second is the password (so list() will work). If the header does |
||
| 241 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
| 242 | * [null, null]. |
||
| 243 | * |
||
| 244 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 245 | * @return string[]|null[] |
||
| 246 | */ |
||
| 247 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Retrieve query string parameter. |
||
| 271 | * |
||
| 272 | * @param string $parameter |
||
| 273 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 274 | * @param mixed $default |
||
| 275 | * |
||
| 276 | * @return null|string |
||
| 277 | */ |
||
| 278 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Retrieve cookie parameter. |
||
| 285 | * |
||
| 286 | * @param string $parameter |
||
| 287 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 288 | * @param mixed $default |
||
| 289 | * |
||
| 290 | * @return null|string |
||
| 291 | */ |
||
| 292 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Retrieve server parameter. |
||
| 299 | * |
||
| 300 | * @param string $parameter |
||
| 301 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 302 | * @param mixed $default |
||
| 303 | * |
||
| 304 | * @return null|string |
||
| 305 | */ |
||
| 306 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Issue an access token. |
||
| 313 | * |
||
| 314 | * @param \DateInterval $accessTokenTTL |
||
| 315 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
||
| 316 | * @param string $userIdentifier |
||
| 317 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
| 318 | * |
||
| 319 | * @return \League\OAuth2\Server\Entities\AccessTokenEntityInterface |
||
| 320 | */ |
||
| 321 | protected function issueAccessToken( |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Issue an auth code. |
||
| 353 | * |
||
| 354 | * @param \DateInterval $authCodeTTL |
||
| 355 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
||
| 356 | * @param string $userIdentifier |
||
| 357 | * @param string $redirectUri |
||
| 358 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
| 359 | * |
||
| 360 | * @return \League\OAuth2\Server\Entities\AuthCodeEntityInterface |
||
| 361 | */ |
||
| 362 | protected function issueAuthCode( |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param \League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessToken |
||
| 396 | * |
||
| 397 | * @return \League\OAuth2\Server\Entities\RefreshTokenEntityInterface |
||
| 398 | */ |
||
| 399 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Generate a new unique identifier. |
||
| 422 | * |
||
| 423 | * @param int $length |
||
| 424 | * |
||
| 425 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | protected function generateUniqueIdentifier($length = 40) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * {@inheritdoc} |
||
| 447 | */ |
||
| 448 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * {@inheritdoc} |
||
| 460 | */ |
||
| 461 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * {@inheritdoc} |
||
| 468 | */ |
||
| 469 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * {@inheritdoc} |
||
| 476 | */ |
||
| 477 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
| 481 | } |
||
| 482 |