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 | public function __construct($clientId = '', $clientSecret = '', array $scopes = []) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | public function getClientId() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $clientId |
||
| 83 | */ |
||
| 84 | public function setClientId($clientId) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function getScopes() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param array $scopes |
||
| 103 | */ |
||
| 104 | public function setScopes(array $scopes) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function getRedirectUri() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $redirectUri |
||
| 119 | */ |
||
| 120 | public function setRedirectUri($redirectUri) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function getState() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $state |
||
| 139 | */ |
||
| 140 | public function setState($state) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param $state |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function verifyState($state) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return int |
||
| 158 | */ |
||
| 159 | public function getTeamId() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param int $teamId |
||
| 166 | */ |
||
| 167 | public function setTeamId($teamId) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $height |
||
| 174 | * @param string $weight |
||
| 175 | * @param string $cssClass |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function generateAddButton($height = '40', $weight = '139', $cssClass = '') |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param $code |
||
| 204 | * @param bool $verifyState State is checked against the value in the session |
||
| 205 | * @param null $state |
||
| 206 | * |
||
| 207 | * @throws \Exception |
||
| 208 | * |
||
| 209 | * @return mixed |
||
| 210 | */ |
||
| 211 | public function getAccessToken($code, $verifyState = true, $state = null) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param $response |
||
| 238 | * |
||
| 239 | * @throws \Exception |
||
| 240 | */ |
||
| 241 | private function handleRequestAccessTokenResponse($response) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param $code |
||
| 262 | * |
||
| 263 | * @throws \Exception |
||
| 264 | * |
||
| 265 | * @return mixed |
||
| 266 | */ |
||
| 267 | private function requestAccessToken($code) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param $accessToken |
||
| 286 | */ |
||
| 287 | public function setAccessToken($accessToken) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getClientSecret() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $clientSecret |
||
| 306 | */ |
||
| 307 | public function setClientSecret($clientSecret) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getBotUserId() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $botUserId |
||
| 322 | */ |
||
| 323 | public function setBotUserId($botUserId) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function getBotAccessToken() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $botAccessToken |
||
| 338 | */ |
||
| 339 | public function setBotAccessToken($botAccessToken) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function getChannel() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $channel |
||
| 354 | */ |
||
| 355 | public function setChannel($channel) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function getTeamName() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param string $teamName |
||
| 370 | */ |
||
| 371 | public function setTeamName($teamName) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | public function getConfigurationUrl() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $configurationUrl |
||
| 386 | */ |
||
| 387 | public function setConfigurationUrl($configurationUrl) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return ApiClient |
||
| 394 | */ |
||
| 395 | public function getApiClient() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param ApiClient $apiClient |
||
| 406 | */ |
||
| 407 | public function setApiClient(ApiClient $apiClient) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param SessionUtility $sessionUtility |
||
| 414 | */ |
||
| 415 | public function setSessionUtility(SessionUtility $sessionUtility) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return SessionUtility|null |
||
| 422 | */ |
||
| 423 | public function getSessionUtility() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @return Config |
||
| 434 | */ |
||
| 435 | public function getConfig() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param Config $config |
||
| 446 | */ |
||
| 447 | public function setConfig(Config $config) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param null $code |
||
| 454 | * @param null $state |
||
| 455 | * |
||
| 456 | * @throws \Exception |
||
| 457 | * |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public function doOauth($code = null, $state = null) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return RequestUtility |
||
| 491 | */ |
||
| 492 | public function getRequestUtility() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param RequestUtility $requestUtility |
||
| 503 | */ |
||
| 504 | public function setRequestUtility(RequestUtility $requestUtility) |
||
| 508 | } |
||
| 509 |