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 |
||
| 13 | class OAuth |
||
| 14 | { |
||
| 15 | const AUTHORIZATION_URL = 'https://slack.com/oauth/authorize'; |
||
| 16 | const SESSION_STATE_KEY = 'state'; |
||
| 17 | |||
| 18 | private $clientId; |
||
| 19 | private $clientSecret; |
||
| 20 | private $scopes; |
||
| 21 | private $redirectUri; |
||
| 22 | private $state; |
||
| 23 | private $teamId; |
||
| 24 | private $apiClient; |
||
| 25 | private $sessionUtility; |
||
| 26 | private $requestUtility; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string configuration_url will be the URL that you can point your user to if they'd like to edit |
||
| 30 | * or remove this integration in Slack |
||
| 31 | */ |
||
| 32 | private $configurationUrl; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string The team_name field will be the name of the team that installed your app |
||
| 36 | */ |
||
| 37 | private $teamName; |
||
| 38 | private $accessToken; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string the channel will be the channel name that they have chosen to post to |
||
| 42 | */ |
||
| 43 | private $channel; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string you will need to use bot_user_id and bot_access_token whenever you are acting on behalf of |
||
| 47 | * that bot user for that team context. |
||
| 48 | * Use the top-level access_token value for other integration points. |
||
| 49 | */ |
||
| 50 | private $botUserId; |
||
| 51 | private $botAccessToken; |
||
| 52 | |||
| 53 | private $config; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * OAuth constructor. |
||
| 57 | * |
||
| 58 | * @param $clientId |
||
| 59 | * @param $clientSecret |
||
| 60 | * @param array $scopes |
||
| 61 | */ |
||
| 62 | 25 | public function __construct($clientId = '', $clientSecret = '', array $scopes = []) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @throws \Exception |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | 6 | public function getClientId(): string |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $clientId |
||
| 85 | */ |
||
| 86 | 25 | public function setClientId(string $clientId) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @throws \Exception |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | 2 | public function getScopes(): array |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param array $scopes |
||
| 107 | */ |
||
| 108 | 25 | public function setScopes(array $scopes) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | 1 | public function getRedirectUri(): string |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $redirectUri |
||
| 123 | */ |
||
| 124 | 1 | public function setRedirectUri(string $redirectUri) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @throws \Exception |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | 2 | public function getState(): string |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $state |
||
| 145 | */ |
||
| 146 | 2 | public function setState(string $state) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param $state |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | 5 | public function verifyState($state): bool |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | 1 | public function getTeamId(): string |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param string $teamId |
||
| 176 | */ |
||
| 177 | 2 | public function setTeamId(string $teamId) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $height |
||
| 184 | * @param string $weight |
||
| 185 | * @param string $cssClass |
||
| 186 | * |
||
| 187 | * @throws \Exception |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | 1 | public function generateAddButton($height = '40', $weight = '139', $cssClass = ''): string |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @param $code |
||
| 216 | * @param bool $verifyState State is checked against the value in the session |
||
| 217 | * @param null $state |
||
| 218 | * |
||
| 219 | * @throws \Exception |
||
| 220 | * |
||
| 221 | * @return mixed |
||
| 222 | */ |
||
| 223 | 10 | public function getAccessToken($code, $verifyState = true, $state = null) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param $response |
||
| 242 | * |
||
| 243 | * @throws \Exception |
||
| 244 | */ |
||
| 245 | 2 | private function handleRequestAccessTokenResponse($response) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param $code |
||
| 266 | * |
||
| 267 | * @throws \Exception |
||
| 268 | * |
||
| 269 | * @return mixed |
||
| 270 | */ |
||
| 271 | 4 | private function requestAccessToken($code) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param $accessToken |
||
| 290 | */ |
||
| 291 | 4 | public function setAccessToken(string $accessToken) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @throws \Exception |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | 4 | public function getClientSecret(): string |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $clientSecret |
||
| 312 | */ |
||
| 313 | 25 | public function setClientSecret(string $clientSecret) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | 1 | public function getBotUserId(): string |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $botUserId |
||
| 328 | */ |
||
| 329 | 2 | public function setBotUserId(string $botUserId) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | 1 | public function getBotAccessToken(): string |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $botAccessToken |
||
| 344 | */ |
||
| 345 | 2 | public function setBotAccessToken(string $botAccessToken) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | 1 | public function getChannel(): string |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @param string $channel |
||
| 360 | */ |
||
| 361 | 2 | public function setChannel(string $channel) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | 1 | public function getTeamName(): string |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $teamName |
||
| 376 | */ |
||
| 377 | 1 | public function setTeamName(string $teamName) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | 1 | public function getConfigurationUrl(): string |
|
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $configurationUrl |
||
| 392 | */ |
||
| 393 | 1 | public function setConfigurationUrl(string $configurationUrl) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @return ApiClient |
||
| 400 | */ |
||
| 401 | 4 | public function getApiClient(): ApiClient |
|
| 409 | |||
| 410 | /** |
||
| 411 | * @param ApiClient $apiClient |
||
| 412 | */ |
||
| 413 | 4 | public function setApiClient(ApiClient $apiClient) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * @param SessionUtility $sessionUtility |
||
| 420 | */ |
||
| 421 | 4 | public function setSessionUtility(SessionUtility $sessionUtility) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @return SessionUtility|null |
||
| 428 | */ |
||
| 429 | 4 | public function getSessionUtility(): SessionUtility |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @return Config |
||
| 440 | */ |
||
| 441 | 5 | public function getConfig(): Config |
|
| 449 | |||
| 450 | /** |
||
| 451 | * @param Config $config |
||
| 452 | */ |
||
| 453 | 5 | public function setConfig(Config $config) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * @param null $code |
||
| 460 | * @param null $state |
||
| 461 | * |
||
| 462 | * @throws \Exception |
||
| 463 | * |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | 4 | public function doOauth($code = null, $state = null) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * @param $accessToken |
||
| 488 | * |
||
| 489 | * @throws \Exception |
||
| 490 | */ |
||
| 491 | 1 | private function processAccessToken($accessToken) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * @return RequestUtility |
||
| 502 | */ |
||
| 503 | 5 | public function getRequestUtility(): RequestUtility |
|
| 511 | |||
| 512 | /** |
||
| 513 | * @param RequestUtility $requestUtility |
||
| 514 | */ |
||
| 515 | 5 | public function setRequestUtility(RequestUtility $requestUtility) |
|
| 519 | } |
||
| 520 |