Complex classes like RequestHandler 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 RequestHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class RequestHandler extends ViewableData |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * Optional url_segment for this request handler |
||
| 53 | * |
||
| 54 | * @config |
||
| 55 | * @var string|null |
||
| 56 | */ |
||
| 57 | private static $url_segment = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var HTTPRequest $request The request object that the controller was called with. |
||
| 61 | * Set in {@link handleRequest()}. Useful to generate the {} |
||
| 62 | */ |
||
| 63 | protected $request = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The DataModel for this request |
||
| 67 | */ |
||
| 68 | protected $model = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * This variable records whether RequestHandler::__construct() |
||
| 72 | * was called or not. Useful for checking if subclasses have |
||
| 73 | * called parent::__construct() |
||
| 74 | * |
||
| 75 | * @var boolean |
||
| 76 | */ |
||
| 77 | protected $brokenOnConstruct = true; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The default URL handling rules. This specifies that the next component of the URL corresponds to a method to |
||
| 81 | * be called on this RequestHandlingData object. |
||
| 82 | * |
||
| 83 | * The keys of this array are parse rules. See {@link HTTPRequest::match()} for a description of the rules |
||
| 84 | * available. |
||
| 85 | * |
||
| 86 | * The values of the array are the method to be called if the rule matches. If this value starts with a '$', then |
||
| 87 | * the named parameter of the parsed URL wil be used to determine the method name. |
||
| 88 | * @config |
||
| 89 | */ |
||
| 90 | private static $url_handlers = array( |
||
| 91 | '$Action' => '$Action', |
||
| 92 | ); |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Define a list of action handling methods that are allowed to be called directly by URLs. |
||
| 97 | * The variable should be an array of action names. This sample shows the different values that it can contain: |
||
| 98 | * |
||
| 99 | * <code> |
||
| 100 | * array( |
||
| 101 | * // someaction can be accessed by anyone, any time |
||
| 102 | * 'someaction', |
||
| 103 | * // So can otheraction |
||
| 104 | * 'otheraction' => true, |
||
| 105 | * // restrictedaction can only be people with ADMIN privilege |
||
| 106 | * 'restrictedaction' => 'ADMIN', |
||
| 107 | * // complexaction can only be accessed if $this->canComplexAction() returns true |
||
| 108 | * 'complexaction' '->canComplexAction' |
||
| 109 | * ); |
||
| 110 | * </code> |
||
| 111 | * |
||
| 112 | * Form getters count as URL actions as well, and should be included in allowed_actions. |
||
| 113 | * Form actions on the other handed (first argument to {@link FormAction()} should NOT be included, |
||
| 114 | * these are handled separately through {@link Form->httpSubmission}. You can control access on form actions |
||
| 115 | * either by conditionally removing {@link FormAction} in the form construction, |
||
| 116 | * or by defining $allowed_actions in your {@link Form} class. |
||
| 117 | * @config |
||
| 118 | */ |
||
| 119 | private static $allowed_actions = null; |
||
| 120 | |||
| 121 | public function __construct() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set the DataModel for this request. |
||
| 135 | * |
||
| 136 | * @param DataModel $model |
||
| 137 | */ |
||
| 138 | public function setDataModel($model) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Handles URL requests. |
||
| 145 | * |
||
| 146 | * - ViewableData::handleRequest() iterates through each rule in {@link self::$url_handlers}. |
||
| 147 | * - If the rule matches, the named method will be called. |
||
| 148 | * - If there is still more URL to be processed, then handleRequest() |
||
| 149 | * is called on the object that that method returns. |
||
| 150 | * |
||
| 151 | * Once all of the URL has been processed, the final result is returned. |
||
| 152 | * However, if the final result is an array, this |
||
| 153 | * array is interpreted as being additional template data to customise the |
||
| 154 | * 2nd to last result with, rather than an object |
||
| 155 | * in its own right. This is most frequently used when a Controller's |
||
| 156 | * action will return an array of data with which to |
||
| 157 | * customise the controller. |
||
| 158 | * |
||
| 159 | * @param HTTPRequest $request The object that is reponsible for distributing URL parsing |
||
| 160 | * @param DataModel $model |
||
| 161 | * @return HTTPResponse|RequestHandler|string|array |
||
| 162 | */ |
||
| 163 | public function handleRequest(HTTPRequest $request, DataModel $model) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param HTTPRequest $request |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | protected function findAction($request) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Given a request, and an action name, call that action name on this RequestHandler |
||
| 298 | * |
||
| 299 | * Must not raise HTTPResponse_Exceptions - instead it should return |
||
| 300 | * |
||
| 301 | * @param $request |
||
| 302 | * @param $action |
||
| 303 | * @return HTTPResponse |
||
| 304 | */ |
||
| 305 | protected function handleAction($request, $action) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get a array of allowed actions defined on this controller, |
||
| 330 | * any parent classes or extensions. |
||
| 331 | * |
||
| 332 | * Caution: Since 3.1, allowed_actions definitions only apply |
||
| 333 | * to methods on the controller they're defined on, |
||
| 334 | * so it is recommended to use the $class argument |
||
| 335 | * when invoking this method. |
||
| 336 | * |
||
| 337 | * @param string $limitToClass |
||
| 338 | * @return array|null |
||
| 339 | */ |
||
| 340 | public function allowedActions($limitToClass = null) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Checks if this request handler has a specific action, |
||
| 371 | * even if the current user cannot access it. |
||
| 372 | * Includes class ancestry and extensions in the checks. |
||
| 373 | * |
||
| 374 | * @param string $action |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | public function hasAction($action) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Return the class that defines the given action, so that we know where to check allowed_actions. |
||
| 422 | * |
||
| 423 | * @param string $actionOrigCasing |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | protected function definingClassForAction($actionOrigCasing) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Check that the given action is allowed to be called from a URL. |
||
| 445 | * It will interrogate {@link self::$allowed_actions} to determine this. |
||
| 446 | * |
||
| 447 | * @param string $action |
||
| 448 | * @return bool |
||
| 449 | * @throws Exception |
||
| 450 | */ |
||
| 451 | public function checkAccessAction($action) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Throws a HTTP error response encased in a {@link HTTPResponse_Exception}, which is later caught in |
||
| 504 | * {@link RequestHandler::handleAction()} and returned to the user. |
||
| 505 | * |
||
| 506 | * @param int $errorCode |
||
| 507 | * @param string $errorMessage Plaintext error message |
||
| 508 | * @uses HTTPResponse_Exception |
||
| 509 | * @throws HTTPResponse_Exception |
||
| 510 | */ |
||
| 511 | public function httpError($errorCode, $errorMessage = null) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Returns the HTTPRequest object that this controller is using. |
||
| 527 | * Returns a placeholder {@link NullHTTPRequest} object unless |
||
| 528 | * {@link handleAction()} or {@link handleRequest()} have been called, |
||
| 529 | * which adds a reference to an actual {@link HTTPRequest} object. |
||
| 530 | * |
||
| 531 | * @return HTTPRequest |
||
| 532 | */ |
||
| 533 | public function getRequest() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Typically the request is set through {@link handleAction()} |
||
| 540 | * or {@link handleRequest()}, but in some based we want to set it manually. |
||
| 541 | * |
||
| 542 | * @param HTTPRequest $request |
||
| 543 | * @return $this |
||
| 544 | */ |
||
| 545 | public function setRequest($request) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Returns a link to this controller. Overload with your own Link rules if they exist. |
||
| 553 | * |
||
| 554 | * @param string $action Optional action |
||
| 555 | * @return string |
||
| 556 | */ |
||
| 557 | public function Link($action = null) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Redirect to the given URL. |
||
| 576 | * |
||
| 577 | * @param string $url |
||
| 578 | * @param int $code |
||
| 579 | * @return HTTPResponse |
||
| 580 | */ |
||
| 581 | public function redirect($url, $code = 302) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Safely get the value of the BackURL param, if provided via querystring / posted var |
||
| 590 | * |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | public function getBackURL() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Returns the referer, if it is safely validated as an internal URL |
||
| 615 | * and can be redirected to. |
||
| 616 | * |
||
| 617 | * @internal called from {@see Form::getValidationErrorResponse} |
||
| 618 | * @return string|null |
||
| 619 | */ |
||
| 620 | public function getReturnReferer() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Get referer |
||
| 631 | * |
||
| 632 | * @return string |
||
| 633 | */ |
||
| 634 | public function getReferer() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Redirect back. Uses either the HTTP-Referer or a manually set request-variable called "BackURL". |
||
| 645 | * This variable is needed in scenarios where HTTP-Referer is not sent (e.g when calling a page by |
||
| 646 | * location.href in IE). If none of the two variables is available, it will redirect to the base |
||
| 647 | * URL (see {@link Director::baseURL()}). |
||
| 648 | * |
||
| 649 | * @uses redirect() |
||
| 650 | * |
||
| 651 | * @return HTTPResponse |
||
| 652 | */ |
||
| 653 | public function redirectBack() |
||
| 668 | } |
||
| 669 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: