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) { |
||
70 | |||
71 | /** |
||
72 | * This function only catches the request to pass it straight on. |
||
73 | * Opauth uses the last segment of the URL to identify the auth method. |
||
74 | * In _routes.yml we enforce a $Strategy request parameter to enforce this. |
||
75 | * Equivalent to "index.php" in the Opauth package. |
||
76 | * @todo: Validate the strategy works before delegating to Opauth. |
||
77 | */ |
||
78 | public function index(SS_HTTPRequest $request) { |
||
96 | |||
97 | /** |
||
98 | * This is executed when the Oauth provider redirects back to us |
||
99 | * Opauth handles everything sent back in this request. |
||
100 | */ |
||
101 | protected function oauthCallback(SS_HTTPRequest $request) { |
||
110 | |||
111 | /** |
||
112 | * Equivalent to "callback.php" in the Opauth package. |
||
113 | * If there is a problem with the response, we throw an HTTP error. |
||
114 | * When done validating, we return back to the Authenticator continue auth. |
||
115 | * @throws SS_HTTPResponse_Exception if any validation errors |
||
116 | */ |
||
117 | public function finished(SS_HTTPRequest $request) { |
||
182 | |||
183 | /** |
||
184 | * @param Member |
||
185 | * @param OpauthIdentity |
||
186 | * @param int $mode One or more AUTH_FLAGs. |
||
187 | */ |
||
188 | protected function loginAndRedirect(Member $member, OpauthIdentity $identity, $mode) { |
||
230 | |||
231 | public function profilecompletion(SS_HTTPRequest $request = null) { |
||
246 | |||
247 | public function RegisterForm(SS_HTTPRequest $request = null, Member $member = null, $result = null) { |
||
263 | |||
264 | public function doCompleteRegister($data, $form, $request) { |
||
294 | |||
295 | /** |
||
296 | * Returns the response from the Oauth callback. |
||
297 | * @throws InvalidArugmentException |
||
298 | * @return array The response |
||
299 | */ |
||
300 | protected function getOpauthResponse() { |
||
313 | |||
314 | /** |
||
315 | * Validates the Oauth response for Opauth. |
||
316 | * @throws InvalidArgumentException |
||
317 | */ |
||
318 | protected function validateOpauthResponse($opauth, $response) { |
||
349 | |||
350 | /** |
||
351 | * Shorthand for quickly finding missing components and complaining about it |
||
352 | * @throws InvalidArgumentException |
||
353 | */ |
||
354 | protected function requireResponseComponents(array $components, $response) { |
||
361 | |||
362 | /** |
||
363 | * @return array Opauth response from session |
||
364 | */ |
||
365 | protected function getResponseFromSession() { |
||
368 | |||
369 | /** |
||
370 | * @param OpauthValidationException $e |
||
371 | */ |
||
372 | protected function handleOpauthException(OpauthValidationException $e) { |
||
402 | |||
403 | /** |
||
404 | * Looks at $method (GET, POST, PUT etc) for the response. |
||
405 | * @return array Opauth response |
||
406 | */ |
||
407 | protected function getResponseFromRequest($method) { |
||
410 | |||
411 | public function Link($action = null) { |
||
417 | |||
418 | /** |
||
419 | * 'path' param for use in Opauth's config |
||
420 | * MUST have trailling slash for Opauth needs |
||
421 | * @return string |
||
422 | */ |
||
423 | public static function get_path() { |
||
429 | |||
430 | /** |
||
431 | * 'callback_url' param for use in Opauth's config |
||
432 | * MUST have trailling slash for Opauth needs |
||
433 | * @return string |
||
434 | */ |
||
435 | public static function get_callback_path() { |
||
441 | |||
442 | ////**** Template variables ****//// |
||
443 | function Title() { |
||
449 | |||
450 | public function Form() { |
||
453 | ////**** END Template variables ****//// |
||
454 | |||
455 | } |
||
456 |
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.