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 | /** |
||
| 40 | * @var ClientRepositoryInterface |
||
| 41 | */ |
||
| 42 | protected $clientRepository; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var AccessTokenRepositoryInterface |
||
| 46 | */ |
||
| 47 | protected $accessTokenRepository; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var ScopeRepositoryInterface |
||
| 51 | */ |
||
| 52 | protected $scopeRepository; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface |
||
| 56 | */ |
||
| 57 | protected $authCodeRepository; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface |
||
| 61 | */ |
||
| 62 | protected $refreshTokenRepository; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var \League\OAuth2\Server\Repositories\UserRepositoryInterface |
||
| 66 | */ |
||
| 67 | protected $userRepository; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var \DateInterval |
||
| 71 | */ |
||
| 72 | protected $refreshTokenTTL; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param ClientRepositoryInterface $clientRepository |
||
| 76 | */ |
||
| 77 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
| 84 | */ |
||
| 85 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param ScopeRepositoryInterface $scopeRepository |
||
| 92 | */ |
||
| 93 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository |
||
| 100 | */ |
||
| 101 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface $authCodeRepository |
||
| 108 | */ |
||
| 109 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository |
||
| 116 | */ |
||
| 117 | public function setUserRepository(UserRepositoryInterface $userRepository) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function setRefreshTokenTTL(\DateInterval $refreshTokenTTL) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Validate the client. |
||
| 132 | * |
||
| 133 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 134 | * |
||
| 135 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
| 136 | * |
||
| 137 | * @return \League\OAuth2\Server\Entities\ClientEntityInterface |
||
| 138 | */ |
||
| 139 | protected function validateClient(ServerRequestInterface $request) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Validate scopes in the request. |
||
| 192 | * |
||
| 193 | * @param string $scopes |
||
| 194 | * @param string $redirectUri |
||
| 195 | * |
||
| 196 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
| 197 | * |
||
| 198 | * @return \League\OAuth2\Server\Entities\ScopeEntityInterface[] |
||
| 199 | */ |
||
| 200 | public function validateScopes( |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Retrieve request parameter. |
||
| 227 | * |
||
| 228 | * @param string $parameter |
||
| 229 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 230 | * @param mixed $default |
||
| 231 | * |
||
| 232 | * @return null|string |
||
| 233 | */ |
||
| 234 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Retrieve query string parameter. |
||
| 243 | * |
||
| 244 | * @param string $parameter |
||
| 245 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 246 | * @param mixed $default |
||
| 247 | * |
||
| 248 | * @return null|string |
||
| 249 | */ |
||
| 250 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Retrieve cookie parameter. |
||
| 257 | * |
||
| 258 | * @param string $parameter |
||
| 259 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 260 | * @param mixed $default |
||
| 261 | * |
||
| 262 | * @return null|string |
||
| 263 | */ |
||
| 264 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Retrieve server 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 getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Issue an access token. |
||
| 285 | * |
||
| 286 | * @param \DateInterval $accessTokenTTL |
||
| 287 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
||
| 288 | * @param string $userIdentifier |
||
| 289 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
| 290 | * |
||
| 291 | * @return \League\OAuth2\Server\Entities\AccessTokenEntityInterface |
||
| 292 | */ |
||
| 293 | protected function issueAccessToken( |
||
| 294 | \DateInterval $accessTokenTTL, |
||
| 295 | ClientEntityInterface $client, |
||
| 296 | $userIdentifier, |
||
| 297 | array $scopes = [] |
||
| 298 | ) { |
||
| 299 | $maxGenerationAttempts = 10; |
||
| 300 | |||
| 301 | $accessToken = $this->accessTokenRepository->getNewToken($client, $scopes, $userIdentifier); |
||
| 302 | $accessToken->setClient($client); |
||
| 303 | $accessToken->setUserIdentifier($userIdentifier); |
||
| 304 | $accessToken->setExpiryDateTime((new \DateTime())->add($accessTokenTTL)); |
||
| 305 | |||
| 306 | foreach ($scopes as $scope) { |
||
| 307 | $accessToken->addScope($scope); |
||
| 308 | } |
||
| 309 | |||
| 310 | while ($maxGenerationAttempts-- > 0) { |
||
| 311 | $accessToken->setIdentifier($this->generateUniqueIdentifier()); |
||
| 312 | try { |
||
| 313 | $this->accessTokenRepository->persistNewAccessToken($accessToken); |
||
| 314 | return $accessToken; |
||
| 315 | } catch (UniqueAccessTokenIdentifierConstraintViolationException $e) { |
||
| 316 | if ($maxGenerationAttempts === 0) { |
||
| 317 | throw $e; |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Issue an auth code. |
||
| 325 | * |
||
| 326 | * @param \DateInterval $authCodeTTL |
||
| 327 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
||
| 328 | * @param string $userIdentifier |
||
| 329 | * @param string $redirectUri |
||
| 330 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
| 331 | * |
||
| 332 | * @return \League\OAuth2\Server\Entities\AuthCodeEntityInterface |
||
| 333 | */ |
||
| 334 | protected function issueAuthCode( |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param \League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessToken |
||
| 359 | * |
||
| 360 | * @return \League\OAuth2\Server\Entities\RefreshTokenEntityInterface |
||
| 361 | */ |
||
| 362 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Generate a new unique identifier. |
||
| 376 | * |
||
| 377 | * @param int $length |
||
| 378 | * |
||
| 379 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | protected function generateUniqueIdentifier($length = 40) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * {@inheritdoc} |
||
| 401 | */ |
||
| 402 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * {@inheritdoc} |
||
| 414 | */ |
||
| 415 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * {@inheritdoc} |
||
| 422 | */ |
||
| 423 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * {@inheritdoc} |
||
| 430 | */ |
||
| 431 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
| 435 | } |
||
| 436 |