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()) |
||
20 | { |
||
21 | if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'GoogleShoppingXml', AccessManager::VIEW)) { |
||
22 | return $response; |
||
23 | } |
||
24 | |||
25 | $isRewritingEnabled = ConfigQuery::isRewritingEnable(); |
||
26 | $isRedirectionEnabled = ConfigQuery::isRedirectionEnable(); |
||
27 | |||
28 | return $this->render( |
||
29 | "RewriteUrl/module-configuration", |
||
30 | [ |
||
31 | "isRewritingEnabled" => $isRewritingEnabled, |
||
32 | "isRedirectionEnabled" => $isRedirectionEnabled, |
||
33 | ] |
||
34 | ); |
||
35 | } |
||
36 | |||
37 | public function getDatatableRules() |
||
38 | { |
||
39 | $request = $this->getRequest(); |
||
40 | |||
41 | $requestSearchValue = $request->get('search') ? '%' . $request->get('search')['value'] . '%' : ""; |
||
42 | $recordsTotal = RewriteurlRuleQuery::create()->count(); |
||
43 | $search = RewriteurlRuleQuery::create(); |
||
44 | if ("" !== $requestSearchValue) { |
||
45 | $search |
||
46 | ->filterByValue($requestSearchValue, Criteria::LIKE) |
||
47 | ->_or() |
||
48 | ->filterByRedirectUrl($requestSearchValue) |
||
49 | ; |
||
50 | } |
||
51 | |||
52 | $recordsFiltered = $search->count(); |
||
53 | |||
54 | $orderColumn = $request->get('order')[0]['column']; |
||
55 | $orderDirection = $request->get('order')[0]['dir']; |
||
56 | switch ($orderColumn) { |
||
57 | case '0': |
||
58 | $search->orderByRuleType($orderDirection); |
||
59 | break; |
||
60 | case '1': |
||
61 | $search->orderByValue($orderDirection); |
||
62 | break; |
||
63 | case '2': |
||
64 | $search->orderByOnly404($orderDirection); |
||
65 | break; |
||
66 | case '3': |
||
67 | $search->orderByRedirectUrl($orderDirection); |
||
68 | break; |
||
69 | case '4': |
||
70 | $search->orderByPosition($orderDirection); |
||
71 | break; |
||
72 | default: |
||
73 | $search->orderByPosition(); |
||
74 | break; |
||
75 | } |
||
76 | |||
77 | $search |
||
78 | ->offset($request->get('start')) |
||
79 | ->limit($request->get('length')) |
||
80 | ; |
||
81 | $searchArray = $search->find()->toArray(); |
||
82 | |||
83 | $resultsArray = []; |
||
84 | foreach ($searchArray as $row) { |
||
85 | $id = $row['Id']; |
||
86 | $isRegexSelected = $row['RuleType'] === 'regex' ? 'selected' : ''; |
||
87 | $isParamsSelected = $row['RuleType'] === 'params' ? 'selected' : ''; |
||
88 | $isOnly404Checked = $row['Only404'] ? 'checked' : ''; |
||
89 | $rewriteUrlRuleParams = RewriteurlRuleQuery::create()->findPk($row['Id'])->getRewriteUrlParamCollection(); |
||
90 | $resultsArray[] = [ |
||
91 | 'Id' => $row['Id'], |
||
92 | 'RuleType' => '<select class="js_rule_type form-control" data-idrule="' . $id . '" required> |
||
93 | <option value="regex" ' . $isRegexSelected . '>' . Translator::getInstance()->trans("Regex", [], RewriteUrl::MODULE_DOMAIN) . '</option> |
||
94 | <option value="params" ' . $isParamsSelected . '>' . Translator::getInstance()->trans("Get Params", [], RewriteUrl::MODULE_DOMAIN) . '</option> |
||
95 | </select>', |
||
96 | 'Value' => $this->renderRaw( |
||
97 | "RewriteUrl/tab-value-render", |
||
98 | [ |
||
99 | "REWRITE_URL_PARAMS" => $rewriteUrlRuleParams, |
||
100 | "VALUE" => $row['Value'], |
||
101 | ] |
||
102 | ), |
||
103 | 'Only404' => '<input class="js_only404 form-control" type="checkbox" style="width: 100%!important;" ' . $isOnly404Checked . '/>', |
||
104 | 'RedirectUrl' => '<div class="col-md-12 input-group"> |
||
105 | <input class="js_url_to_redirect form-control" type="text" placeholder="/path/mypage.html" value="' . $row['RedirectUrl'] . '"/> |
||
106 | </div>', |
||
107 | 'Position' => '<a href="#" class="u-position-up js_move_rule_position_up" data-idrule="' . $id . '"><i class="glyphicon glyphicon-arrow-up"></i></a> |
||
108 | <span class="js_editable_rule_position editable editable-click" data-idrule="' . $id . '">' . $row['Position'] . '</span> |
||
109 | <a href="#" class="u-position-down js_move_rule_position_down" data-idrule="' . $id . '"><i class="glyphicon glyphicon-arrow-down"></i></a>', |
||
110 | 'Actions' => '<a href="#" class="js_btn_update_rule btn btn-success" data-idrule="' . $id . '"><span class="glyphicon glyphicon-check"></span></a> |
||
111 | <a href="#" class="js_btn_remove_rule btn btn-danger" data-idrule="' . $id . '"><span class="glyphicon glyphicon-remove"></span></a> |
||
112 | ', |
||
113 | ]; |
||
114 | } |
||
115 | |||
116 | return new JsonResponse([ |
||
117 | 'draw' => $request->get('draw'), |
||
118 | 'recordsTotal' => $recordsTotal, |
||
119 | 'recordsFiltered' => $recordsFiltered, |
||
120 | 'data' => $resultsArray |
||
121 | ]); |
||
122 | } |
||
123 | |||
124 | public function setRewritingEnableAction() |
||
140 | |||
141 | // check redirection_enable status & write an entry if there is none // |
||
142 | |||
143 | public function setRedirectionEnableAction() |
||
159 | // ############################## // |
||
160 | |||
161 | public function addRuleAction() |
||
174 | |||
175 | public function updateRuleAction() |
||
195 | |||
196 | |||
197 | public function removeRuleAction() |
||
217 | |||
218 | public function moveRulePositionAction() |
||
248 | |||
249 | protected function fillRuleObjectFields(RewriteurlRule $rule, $request) |
||
312 | } |
||
313 |