Complex classes like Oauth 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 Oauth, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Oauth |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * JWT helper |
||
| 30 | * @var \gplcart\modules\oauth\helpers\Jwt $jwt |
||
| 31 | */ |
||
| 32 | protected $jwt; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Hook class instance |
||
| 36 | * @var \gplcart\core\Hook $hook |
||
| 37 | */ |
||
| 38 | protected $hook; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Http model class instance |
||
| 42 | * @var \gplcart\core\models\Http $http |
||
| 43 | */ |
||
| 44 | protected $http; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * URL helper instance |
||
| 48 | * @var \gplcart\core\helpers\Url $url |
||
| 49 | */ |
||
| 50 | protected $url; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Session helper instance |
||
| 54 | * @var \gplcart\core\helpers\Session $session |
||
| 55 | */ |
||
| 56 | protected $session; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Oauth constructor. |
||
| 60 | * @param Hook $hook |
||
| 61 | * @param Http $http |
||
| 62 | * @param Jwt $jwt |
||
| 63 | * @param Session $session |
||
| 64 | * @param Url $url |
||
| 65 | */ |
||
| 66 | public function __construct(Hook $hook, Http $http, Jwt $jwt, Session $session, Url $url) |
||
| 67 | { |
||
| 68 | $this->jwt = $jwt; |
||
| 69 | $this->url = $url; |
||
| 70 | $this->hook = $hook; |
||
| 71 | $this->http = $http; |
||
| 72 | $this->session = $session; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Does main authorization process |
||
| 77 | * @param array $provider |
||
| 78 | * @param array $params |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public function authorize(array $provider, array $params) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns an Oauth provider |
||
| 102 | * @param string $id |
||
| 103 | * @return array |
||
| 104 | * @throws OutOfRangeException |
||
| 105 | */ |
||
| 106 | public function getProvider($id) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Returns an array of Oauth providers |
||
| 119 | * @param array $options |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getProviders(array $options = array()) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns an array of URL query to redirect a user to an authorization server |
||
| 137 | * @param array $provider |
||
| 138 | * @param array $params |
||
| 139 | * @return array |
||
| 140 | * @throws OutOfRangeException |
||
| 141 | */ |
||
| 142 | public function getQueryAuth(array $provider, array $params = array()) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns an array of URL query to request an access token |
||
| 177 | * @param array $provider |
||
| 178 | * @param array $params |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function getQueryToken(array $provider, array $params = array()) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns the full URL to an authorization server |
||
| 203 | * @param array $provider |
||
| 204 | * @param array $params |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getAuthUrl(array $provider, array $params = array()) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Build a state code for the given provider |
||
| 223 | * @param string $provider_id |
||
| 224 | * @return string |
||
| 225 | * @throws UnexpectedValueException |
||
| 226 | */ |
||
| 227 | public function encodeState($provider_id) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Decode the state code |
||
| 246 | * @param string $state |
||
| 247 | * @return array |
||
| 248 | * @throws UnexpectedValueException |
||
| 249 | * @throws OutOfRangeException |
||
| 250 | */ |
||
| 251 | public function decodeState($state) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Save the state code in the session |
||
| 270 | * @param string $state |
||
| 271 | * @param string $provider_id |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function setState($state, $provider_id) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Returns a saved state data for the provider from the session |
||
| 281 | * @param string $provider_id |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getState($provider_id) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Remove a state form the session |
||
| 291 | * @param null|null $provider_id |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | public function unsetState($provider_id = null) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Save the token data in the session |
||
| 307 | * @param array $token |
||
| 308 | * @param string $provider_id |
||
| 309 | */ |
||
| 310 | public function setToken(array $token, $provider_id) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Whether a token for the given provider is valid |
||
| 321 | * @param string $provider_id |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | public function isValidToken($provider_id) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Whether the state for the provider is valid |
||
| 333 | * @param string $state |
||
| 334 | * @param string $provider_id |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function isValidState($state, $provider_id) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns a saved token data for the provider from the session |
||
| 344 | * @param string $provider_id |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | public function getToken($provider_id) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Performs an HTTP request to get an access token |
||
| 354 | * @param array $provider |
||
| 355 | * @param array $query |
||
| 356 | * @return array |
||
| 357 | * @throws OutOfRangeException |
||
| 358 | */ |
||
| 359 | public function requestToken(array $provider, array $query) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Returns an array of requested token data |
||
| 386 | * @param array $provider |
||
| 387 | * @param array $params |
||
| 388 | * @return array |
||
| 389 | * @throws OutOfRangeException |
||
| 390 | */ |
||
| 391 | public function exchangeToken(array $provider, array $params = array()) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Returns an array of requested token for "server-to-server" authorization |
||
| 413 | * @param array $provider |
||
| 414 | * @param array $jwt |
||
| 415 | * @return mixed |
||
| 416 | * @throws OutOfRangeException |
||
| 417 | */ |
||
| 418 | public function exchangeTokenServer($provider, $jwt) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Encode JWT token data |
||
| 448 | * @param array $data |
||
| 449 | * @param array $provider |
||
| 450 | * @return string |
||
| 451 | * @throws OutOfRangeException |
||
| 452 | */ |
||
| 453 | public function encodeJwt(array $data, array $provider) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Call a provider handler |
||
| 465 | * @param string $handler_name |
||
| 466 | * @param array $provider |
||
| 467 | * @param array $params |
||
| 468 | * @return mixed |
||
| 469 | */ |
||
| 470 | public function callHandler($handler_name, array $provider, array $params) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Prepare an array of Oauth providers |
||
| 478 | * @param array $providers |
||
| 479 | * @param array $options |
||
| 480 | * @return array |
||
| 481 | */ |
||
| 482 | protected function prepareProviders(array $providers, array $options) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Decode JSON string |
||
| 511 | * @param string $json |
||
| 512 | * @return array |
||
| 513 | * @throws UnexpectedValueException |
||
| 514 | */ |
||
| 515 | protected function decodeJson($json) |
||
| 525 | |||
| 526 | } |
||
| 527 |