Complex classes like ModuleConfigController 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 ModuleConfigController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ModuleConfigController extends BaseAdminController |
||
| 18 | { |
||
| 19 | public function viewConfigAction($params = array()) |
||
| 34 | |||
| 35 | public function getDatatableRules() |
||
| 36 | { |
||
| 37 | $request = $this->getRequest(); |
||
| 38 | |||
| 39 | $requestSearchValue = $request->get('search') ? '%' . $request->get('search')['value'] . '%' : ""; |
||
| 40 | $recordsTotal = RewriteurlRuleQuery::create()->count(); |
||
| 41 | $search = RewriteurlRuleQuery::create(); |
||
| 42 | if ("" !== $requestSearchValue) { |
||
| 43 | $search |
||
| 44 | ->filterByValue($requestSearchValue, Criteria::LIKE) |
||
| 45 | ->_or() |
||
| 46 | ->filterByRedirectUrl($requestSearchValue) |
||
| 47 | ; |
||
| 48 | } |
||
| 49 | |||
| 50 | $recordsFiltered = $search->count(); |
||
| 51 | |||
| 52 | $orderColumn = $request->get('order')[0]['column']; |
||
| 53 | $orderDirection = $request->get('order')[0]['dir']; |
||
| 54 | switch ($orderColumn) { |
||
| 55 | case '0': |
||
| 56 | $search->orderByRuleType($orderDirection); |
||
| 57 | break; |
||
| 58 | case '1': |
||
| 59 | $search->orderByValue($orderDirection); |
||
| 60 | break; |
||
| 61 | case '2': |
||
| 62 | $search->orderByOnly404($orderDirection); |
||
| 63 | break; |
||
| 64 | case '3': |
||
| 65 | $search->orderByRedirectUrl($orderDirection); |
||
| 66 | break; |
||
| 67 | case '4': |
||
| 68 | $search->orderByPosition($orderDirection); |
||
| 69 | break; |
||
| 70 | default: |
||
| 71 | $search->orderByPosition(); |
||
| 72 | break; |
||
| 73 | } |
||
| 74 | |||
| 75 | $search |
||
| 76 | ->offset($request->get('start')) |
||
| 77 | ->limit($request->get('length')) |
||
| 78 | ; |
||
| 79 | $searchArray = $search->find()->toArray(); |
||
| 80 | |||
| 81 | $resultsArray = []; |
||
| 82 | foreach ($searchArray as $row) { |
||
| 83 | $id = $row['Id']; |
||
| 84 | $isRegexSelected = $row['RuleType'] === 'regex' ? 'selected' : ''; |
||
| 85 | $isParamsSelected = $row['RuleType'] === 'params' ? 'selected' : ''; |
||
| 86 | $isOnly404Checked = $row['Only404'] ? 'checked' : ''; |
||
| 87 | $rewriteUrlRuleParams = RewriteurlRuleQuery::create()->findPk($row['Id'])->getRewriteUrlParamCollection(); |
||
| 88 | $resultsArray[] = [ |
||
| 89 | 'Id' => $row['Id'], |
||
| 90 | 'RuleType' => '<select class="js_rule_type form-control" data-idrule="' . $id . '" required> |
||
| 91 | <option value="regex" ' . $isRegexSelected . '>' . Translator::getInstance()->trans("Regex", [], RewriteUrl::MODULE_DOMAIN) . '</option> |
||
| 92 | <option value="params" ' . $isParamsSelected . '>' . Translator::getInstance()->trans("Get Params", [], RewriteUrl::MODULE_DOMAIN) . '</option> |
||
| 93 | </select>', |
||
| 94 | 'Value' => $this->renderRaw( |
||
| 95 | "RewriteUrl/tab-value-render", |
||
| 96 | [ |
||
| 97 | "REWRITE_URL_PARAMS" => $rewriteUrlRuleParams, |
||
| 98 | "VALUE" => $row['Value'], |
||
| 99 | ] |
||
| 100 | ), |
||
| 101 | 'Only404' => '<input class="js_only404 form-control" type="checkbox" style="width: 100%!important;" ' . $isOnly404Checked . '/>', |
||
| 102 | 'RedirectUrl' => '<div class="col-md-12 input-group"> |
||
| 103 | <input class="js_url_to_redirect form-control" type="text" placeholder="/path/mypage.html" value="' . $row['RedirectUrl'] . '"/> |
||
| 104 | </div>', |
||
| 105 | 'Position' => '<a href="#" class="u-position-up js_move_rule_position_up" data-idrule="' . $id . '"><i class="glyphicon glyphicon-arrow-up"></i></a> |
||
| 106 | <span class="js_editable_rule_position editable editable-click" data-idrule="' . $id . '">' . $row['Position'] . '</span> |
||
| 107 | <a href="#" class="u-position-down js_move_rule_position_down" data-idrule="' . $id . '"><i class="glyphicon glyphicon-arrow-down"></i></a>', |
||
| 108 | 'Actions' => '<a href="#" class="js_btn_update_rule btn btn-success" data-idrule="' . $id . '"><span class="glyphicon glyphicon-check"></span></a> |
||
| 109 | <a href="#" class="js_btn_remove_rule btn btn-danger" data-idrule="' . $id . '"><span class="glyphicon glyphicon-remove"></span></a> |
||
| 110 | ', |
||
| 111 | ]; |
||
| 112 | } |
||
| 113 | |||
| 114 | return new JsonResponse([ |
||
| 115 | 'draw' => $request->get('draw'), |
||
| 116 | 'recordsTotal' => $recordsTotal, |
||
| 117 | 'recordsFiltered' => $recordsFiltered, |
||
| 118 | 'data' => $resultsArray |
||
| 119 | ]); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function setRewritingEnableAction() |
||
| 123 | { |
||
| 124 | $request = $this->getRequest()->request; |
||
| 125 | $isRewritingEnable = $request->get("rewriting_enable", null); |
||
| 126 | |||
| 127 | if ($isRewritingEnable !== null) { |
||
| 128 | ConfigQuery::write("rewriting_enable", $isRewritingEnable ? 1 : 0); |
||
| 129 | return $this->jsonResponse(json_encode(["state" => "Success"]), 200); |
||
| 130 | } else { |
||
| 131 | return $this->jsonResponse(Translator::getInstance()->trans( |
||
| 132 | "Unable to change the configuration variable.", |
||
| 133 | [], |
||
| 134 | RewriteUrl::MODULE_DOMAIN |
||
| 135 | ), 500); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | public function addRuleAction() |
||
| 140 | { |
||
| 141 | try { |
||
| 142 | $request = $this->getRequest()->request; |
||
| 143 | |||
| 144 | $rule = new RewriteurlRule(); |
||
| 145 | |||
| 146 | $this->fillRuleObjectFields($rule, $request); |
||
| 147 | } catch (\Exception $ex) { |
||
| 148 | return $this->jsonResponse($ex->getMessage(), 500); |
||
| 149 | } |
||
| 150 | return $this->jsonResponse(json_encode(["state" => "Success"]), 200); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function updateRuleAction() |
||
| 173 | |||
| 174 | |||
| 175 | public function removeRuleAction() |
||
| 195 | |||
| 196 | public function moveRulePositionAction() |
||
| 197 | { |
||
| 198 | try { |
||
| 226 | |||
| 227 | protected function fillRuleObjectFields(RewriteurlRule $rule, $request) |
||
| 290 | } |
||
| 291 |