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 | * @return string |
||
| 71 | */ |
||
| 72 | 6 | public function getClientId() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $clientId |
||
| 83 | */ |
||
| 84 | 25 | public function setClientId($clientId) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | 2 | public function getScopes() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param array $scopes |
||
| 103 | */ |
||
| 104 | 25 | public function setScopes(array $scopes) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | 1 | public function getRedirectUri() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $redirectUri |
||
| 119 | */ |
||
| 120 | 1 | public function setRedirectUri($redirectUri) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | 2 | public function getState() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $state |
||
| 139 | */ |
||
| 140 | 2 | public function setState($state) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param $state |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | 5 | public function verifyState($state) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @return int |
||
| 162 | */ |
||
| 163 | 1 | public function getTeamId() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param int $teamId |
||
| 170 | */ |
||
| 171 | 2 | public function setTeamId($teamId) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $height |
||
| 178 | * @param string $weight |
||
| 179 | * @param string $cssClass |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | 1 | public function generateAddButton($height = '40', $weight = '139', $cssClass = '') |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param $code |
||
| 208 | * @param bool $verifyState State is checked against the value in the session |
||
| 209 | * @param null $state |
||
| 210 | * |
||
| 211 | * @throws \Exception |
||
| 212 | * |
||
| 213 | * @return mixed |
||
| 214 | */ |
||
| 215 | 10 | public function getAccessToken($code, $verifyState = true, $state = null) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param $response |
||
| 234 | * |
||
| 235 | * @throws \Exception |
||
| 236 | */ |
||
| 237 | 2 | private function handleRequestAccessTokenResponse($response) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param $code |
||
| 258 | * |
||
| 259 | * @throws \Exception |
||
| 260 | * |
||
| 261 | * @return mixed |
||
| 262 | */ |
||
| 263 | 4 | private function requestAccessToken($code) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param $accessToken |
||
| 282 | */ |
||
| 283 | 4 | public function setAccessToken($accessToken) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | 4 | public function getClientSecret() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $clientSecret |
||
| 302 | */ |
||
| 303 | 25 | public function setClientSecret($clientSecret) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | 1 | public function getBotUserId() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $botUserId |
||
| 318 | */ |
||
| 319 | 2 | public function setBotUserId($botUserId) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | 1 | public function getBotAccessToken() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $botAccessToken |
||
| 334 | */ |
||
| 335 | 2 | public function setBotAccessToken($botAccessToken) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | 1 | public function getChannel() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $channel |
||
| 350 | */ |
||
| 351 | 2 | public function setChannel($channel) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | 1 | public function getTeamName() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $teamName |
||
| 366 | */ |
||
| 367 | 1 | public function setTeamName($teamName) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | 1 | public function getConfigurationUrl() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @param string $configurationUrl |
||
| 382 | */ |
||
| 383 | 1 | public function setConfigurationUrl($configurationUrl) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @return ApiClient |
||
| 390 | */ |
||
| 391 | 4 | public function getApiClient() |
|
| 399 | |||
| 400 | /** |
||
| 401 | * @param ApiClient $apiClient |
||
| 402 | */ |
||
| 403 | 4 | public function setApiClient(ApiClient $apiClient) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @param SessionUtility $sessionUtility |
||
| 410 | */ |
||
| 411 | 4 | public function setSessionUtility(SessionUtility $sessionUtility) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @return SessionUtility|null |
||
| 418 | */ |
||
| 419 | 4 | public function getSessionUtility() |
|
| 427 | |||
| 428 | /** |
||
| 429 | * @return Config |
||
| 430 | */ |
||
| 431 | 5 | public function getConfig() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @param Config $config |
||
| 442 | */ |
||
| 443 | 5 | public function setConfig(Config $config) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @param null $code |
||
| 450 | * @param null $state |
||
| 451 | * |
||
| 452 | * @throws \Exception |
||
| 453 | * |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | 4 | public function doOauth($code = null, $state = null) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @param $accessToken |
||
| 481 | * |
||
| 482 | * @throws \Exception |
||
| 483 | */ |
||
| 484 | 1 | private function processAccessToken($accessToken) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * @return RequestUtility |
||
| 495 | */ |
||
| 496 | 5 | public function getRequestUtility() |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @param RequestUtility $requestUtility |
||
| 507 | */ |
||
| 508 | 5 | public function setRequestUtility(RequestUtility $requestUtility) |
|
| 512 | } |
||
| 513 |