Complex classes like OpauthController 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 OpauthController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class OpauthController extends ContentController { |
||
|
|
|||
| 12 | |||
| 13 | private static |
||
| 14 | $allowed_actions = array( |
||
| 15 | 'index', |
||
| 16 | 'finished', |
||
| 17 | 'profilecompletion', |
||
| 18 | 'RegisterForm', |
||
| 19 | ), |
||
| 20 | $url_handlers = array( |
||
| 21 | 'finished' => 'finished', |
||
| 22 | ); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Bitwise indicators to extensions what sort of action is happening |
||
| 26 | */ |
||
| 27 | const |
||
| 28 | /** |
||
| 29 | * LOGIN = already a user with an OAuth ID |
||
| 30 | */ |
||
| 31 | AUTH_FLAG_LOGIN = 2, |
||
| 32 | /** |
||
| 33 | * LINK = already a user, linking a new OAuth ID |
||
| 34 | */ |
||
| 35 | AUTH_FLAG_LINK = 4, |
||
| 36 | /** |
||
| 37 | * REGISTER = new user, linking OAuth ID |
||
| 38 | */ |
||
| 39 | AUTH_FLAG_REGISTER = 8; |
||
| 40 | |||
| 41 | protected |
||
| 42 | $registerForm; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Fake a Page_Controller by using that class as a failover |
||
| 46 | */ |
||
| 47 | public function __construct($dataRecord = null) { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Prepare the controller for handling the response to this request |
||
| 56 | * |
||
| 57 | * @param string $title Title to use |
||
| 58 | * @return Controller |
||
| 59 | */ |
||
| 60 | protected function getResponseController($title) { |
||
| 61 | if(!class_exists('SiteTree')) return $this; |
||
| 62 | |||
| 63 | // Use sitetree pages to render the opauth pages |
||
| 64 | $tmpPage = new Page(); |
||
| 65 | $tmpPage->ID = -1; |
||
| 66 | $tmpPage->Title = $title; |
||
| 67 | |||
| 68 | $controller = ModelAsController::controller_for($tmpPage); |
||
| 69 | $controller->init(); |
||
| 70 | return $controller; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * This function only catches the request to pass it straight on. |
||
| 75 | * Opauth uses the last segment of the URL to identify the auth method. |
||
| 76 | * In _routes.yml we enforce a $Strategy request parameter to enforce this. |
||
| 77 | * Equivalent to "index.php" in the Opauth package. |
||
| 78 | * @todo: Validate the strategy works before delegating to Opauth. |
||
| 79 | */ |
||
| 80 | public function index(SS_HTTPRequest $request) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * This is executed when the Oauth provider redirects back to us |
||
| 101 | * Opauth handles everything sent back in this request. |
||
| 102 | */ |
||
| 103 | protected function oauthCallback(SS_HTTPRequest $request) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Equivalent to "callback.php" in the Opauth package. |
||
| 115 | * If there is a problem with the response, we throw an HTTP error. |
||
| 116 | * When done validating, we return back to the Authenticator continue auth. |
||
| 117 | * @throws SS_HTTPResponse_Exception if any validation errors |
||
| 118 | */ |
||
| 119 | public function finished(SS_HTTPRequest $request) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param Member |
||
| 187 | * @param OpauthIdentity |
||
| 188 | * @param int $mode One or more AUTH_FLAGs. |
||
| 189 | */ |
||
| 190 | protected function loginAndRedirect(Member $member, OpauthIdentity $identity, $mode) { |
||
| 232 | |||
| 233 | public function profilecompletion(SS_HTTPRequest $request = null) { |
||
| 248 | |||
| 249 | public function RegisterForm(SS_HTTPRequest $request = null, Member $member = null, $result = null) { |
||
| 265 | |||
| 266 | public function doCompleteRegister($data, $form, $request) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Returns the response from the Oauth callback. |
||
| 299 | * @throws InvalidArugmentException |
||
| 300 | * @return array The response |
||
| 301 | */ |
||
| 302 | protected function getOpauthResponse() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Validates the Oauth response for Opauth. |
||
| 318 | * @throws InvalidArgumentException |
||
| 319 | */ |
||
| 320 | protected function validateOpauthResponse($opauth, $response) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Shorthand for quickly finding missing components and complaining about it |
||
| 354 | * @throws InvalidArgumentException |
||
| 355 | */ |
||
| 356 | protected function requireResponseComponents(array $components, $response) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return array Opauth response from session |
||
| 366 | */ |
||
| 367 | protected function getResponseFromSession() { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param OpauthValidationException $e |
||
| 373 | */ |
||
| 374 | protected function handleOpauthException(OpauthValidationException $e) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Looks at $method (GET, POST, PUT etc) for the response. |
||
| 407 | * @return array Opauth response |
||
| 408 | */ |
||
| 409 | protected function getResponseFromRequest($method) { |
||
| 412 | |||
| 413 | public function Link($action = null) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * 'path' param for use in Opauth's config |
||
| 422 | * MUST have trailling slash for Opauth needs |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public static function get_path() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * 'callback_url' param for use in Opauth's config |
||
| 434 | * MUST have trailling slash for Opauth needs |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | public static function get_callback_path() { |
||
| 443 | |||
| 444 | ////**** Template variables ****//// |
||
| 445 | function Title() { |
||
| 451 | |||
| 452 | public function Form() { |
||
| 455 | ////**** END Template variables ****//// |
||
| 456 | |||
| 457 | } |
||
| 458 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.