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 | * This function only catches the request to pass it straight on. |
||
56 | * Opauth uses the last segment of the URL to identify the auth method. |
||
57 | * In _routes.yml we enforce a $Strategy request parameter to enforce this. |
||
58 | * Equivalent to "index.php" in the Opauth package. |
||
59 | * @todo: Validate the strategy works before delegating to Opauth. |
||
60 | */ |
||
61 | public function index(SS_HTTPRequest $request) { |
||
79 | |||
80 | /** |
||
81 | * This is executed when the Oauth provider redirects back to us |
||
82 | * Opauth handles everything sent back in this request. |
||
83 | */ |
||
84 | protected function oauthCallback(SS_HTTPRequest $request) { |
||
93 | |||
94 | /** |
||
95 | * Equivalent to "callback.php" in the Opauth package. |
||
96 | * If there is a problem with the response, we throw an HTTP error. |
||
97 | * When done validating, we return back to the Authenticator continue auth. |
||
98 | * @throws SS_HTTPResponse_Exception if any validation errors |
||
99 | */ |
||
100 | public function finished(SS_HTTPRequest $request) { |
||
165 | |||
166 | /** |
||
167 | * @param Member |
||
168 | * @param OpauthIdentity |
||
169 | * @param int $mode One or more AUTH_FLAGs. |
||
170 | */ |
||
171 | protected function loginAndRedirect(Member $member, OpauthIdentity $identity, $mode) { |
||
213 | |||
214 | public function profilecompletion(SS_HTTPRequest $request = null) { |
||
226 | |||
227 | public function RegisterForm(SS_HTTPRequest $request = null, Member $member = null, $result = null) { |
||
243 | |||
244 | public function doCompleteRegister($data, $form, $request) { |
||
274 | |||
275 | /** |
||
276 | * Returns the response from the Oauth callback. |
||
277 | * @throws InvalidArugmentException |
||
278 | * @return array The response |
||
279 | */ |
||
280 | protected function getOpauthResponse() { |
||
293 | |||
294 | /** |
||
295 | * Validates the Oauth response for Opauth. |
||
296 | * @throws InvalidArgumentException |
||
297 | */ |
||
298 | protected function validateOpauthResponse($opauth, $response) { |
||
329 | |||
330 | /** |
||
331 | * Shorthand for quickly finding missing components and complaining about it |
||
332 | * @throws InvalidArgumentException |
||
333 | */ |
||
334 | protected function requireResponseComponents(array $components, $response) { |
||
341 | |||
342 | /** |
||
343 | * @return array Opauth response from session |
||
344 | */ |
||
345 | protected function getResponseFromSession() { |
||
348 | |||
349 | /** |
||
350 | * @param OpauthValidationException $e |
||
351 | */ |
||
352 | protected function handleOpauthException(OpauthValidationException $e) { |
||
382 | |||
383 | /** |
||
384 | * Looks at $method (GET, POST, PUT etc) for the response. |
||
385 | * @return array Opauth response |
||
386 | */ |
||
387 | protected function getResponseFromRequest($method) { |
||
390 | |||
391 | public function Link($action = null) { |
||
397 | |||
398 | /** |
||
399 | * 'path' param for use in Opauth's config |
||
400 | * MUST have trailling slash for Opauth needs |
||
401 | * @return string |
||
402 | */ |
||
403 | public static function get_path() { |
||
409 | |||
410 | /** |
||
411 | * 'callback_url' param for use in Opauth's config |
||
412 | * MUST have trailling slash for Opauth needs |
||
413 | * @return string |
||
414 | */ |
||
415 | public static function get_callback_path() { |
||
421 | |||
422 | ////**** Template variables ****//// |
||
423 | function Title() { |
||
429 | |||
430 | public function Form() { |
||
433 | ////**** END Template variables ****//// |
||
434 | |||
435 | } |
||
436 |
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.