Total Complexity | 73 |
Total Lines | 471 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ControllerLocalisationCountry 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.
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 ControllerLocalisationCountry, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ControllerLocalisationCountry extends \Divine\Engine\Core\Controller |
||
|
|||
24 | { |
||
25 | private $error = array(); |
||
26 | |||
27 | public function index() |
||
36 | } |
||
37 | |||
38 | public function add() |
||
39 | { |
||
40 | $this->load->language('localisation/country'); |
||
41 | |||
42 | $this->document->setTitle($this->language->get('heading_title')); |
||
43 | |||
44 | $this->load->model('localisation/country'); |
||
45 | |||
46 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
47 | $this->model_localisation_country->addCountry($this->request->post); |
||
48 | |||
49 | $this->session->data['success'] = $this->language->get('text_success'); |
||
50 | |||
51 | $url = ''; |
||
52 | |||
53 | if (isset($this->request->get['sort'])) { |
||
54 | $url .= '&sort=' . $this->request->get['sort']; |
||
55 | } |
||
56 | |||
57 | if (isset($this->request->get['order'])) { |
||
58 | $url .= '&order=' . $this->request->get['order']; |
||
59 | } |
||
60 | |||
61 | if (isset($this->request->get['page'])) { |
||
62 | $url .= '&page=' . $this->request->get['page']; |
||
63 | } |
||
64 | |||
65 | $this->response->redirect($this->url->link('localisation/country', 'token=' . $this->session->data['token'] . $url, true)); |
||
66 | } |
||
67 | |||
68 | $this->getForm(); |
||
69 | } |
||
70 | |||
71 | public function edit() |
||
72 | { |
||
73 | $this->load->language('localisation/country'); |
||
74 | |||
75 | $this->document->setTitle($this->language->get('heading_title')); |
||
76 | |||
77 | $this->load->model('localisation/country'); |
||
78 | |||
79 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
80 | $this->model_localisation_country->editCountry($this->request->get['country_id'], $this->request->post); |
||
81 | |||
82 | $this->session->data['success'] = $this->language->get('text_success'); |
||
83 | |||
84 | $url = ''; |
||
85 | |||
86 | if (isset($this->request->get['sort'])) { |
||
87 | $url .= '&sort=' . $this->request->get['sort']; |
||
88 | } |
||
89 | |||
90 | if (isset($this->request->get['order'])) { |
||
91 | $url .= '&order=' . $this->request->get['order']; |
||
92 | } |
||
93 | |||
94 | if (isset($this->request->get['page'])) { |
||
95 | $url .= '&page=' . $this->request->get['page']; |
||
96 | } |
||
97 | |||
98 | $this->response->redirect($this->url->link('localisation/country', 'token=' . $this->session->data['token'] . $url, true)); |
||
99 | } |
||
100 | |||
101 | $this->getForm(); |
||
102 | } |
||
103 | |||
104 | public function delete() |
||
105 | { |
||
106 | $this->load->language('localisation/country'); |
||
107 | |||
108 | $this->document->setTitle($this->language->get('heading_title')); |
||
109 | |||
110 | $this->load->model('localisation/country'); |
||
111 | |||
112 | if (isset($this->request->post['selected']) && $this->validateDelete()) { |
||
113 | foreach ($this->request->post['selected'] as $country_id) { |
||
114 | $this->model_localisation_country->deleteCountry($country_id); |
||
115 | } |
||
116 | |||
117 | $this->session->data['success'] = $this->language->get('text_success'); |
||
118 | |||
119 | $url = ''; |
||
120 | |||
121 | if (isset($this->request->get['sort'])) { |
||
122 | $url .= '&sort=' . $this->request->get['sort']; |
||
123 | } |
||
124 | |||
125 | if (isset($this->request->get['order'])) { |
||
126 | $url .= '&order=' . $this->request->get['order']; |
||
127 | } |
||
128 | |||
129 | if (isset($this->request->get['page'])) { |
||
130 | $url .= '&page=' . $this->request->get['page']; |
||
131 | } |
||
132 | |||
133 | $this->response->redirect($this->url->link('localisation/country', 'token=' . $this->session->data['token'] . $url, true)); |
||
134 | } |
||
135 | |||
136 | $this->getList(); |
||
137 | } |
||
138 | |||
139 | protected function getList() |
||
140 | { |
||
141 | if (isset($this->request->get['sort'])) { |
||
142 | $sort = $this->request->get['sort']; |
||
143 | } else { |
||
144 | $sort = 'name'; |
||
145 | } |
||
146 | |||
147 | if (isset($this->request->get['order'])) { |
||
148 | $order = $this->request->get['order']; |
||
149 | } else { |
||
150 | $order = 'ASC'; |
||
151 | } |
||
152 | |||
153 | if (isset($this->request->get['page'])) { |
||
154 | $page = $this->request->get['page']; |
||
155 | } else { |
||
156 | $page = 1; |
||
157 | } |
||
158 | |||
159 | $url = ''; |
||
160 | |||
161 | if (isset($this->request->get['sort'])) { |
||
162 | $url .= '&sort=' . $this->request->get['sort']; |
||
163 | } |
||
164 | |||
165 | if (isset($this->request->get['order'])) { |
||
166 | $url .= '&order=' . $this->request->get['order']; |
||
167 | } |
||
168 | |||
169 | if (isset($this->request->get['page'])) { |
||
170 | $url .= '&page=' . $this->request->get['page']; |
||
171 | } |
||
172 | |||
173 | $data['breadcrumbs'] = array(); |
||
174 | |||
175 | $data['breadcrumbs'][] = array( |
||
176 | 'text' => $this->language->get('text_home'), |
||
177 | 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true) |
||
178 | ); |
||
179 | |||
180 | $data['breadcrumbs'][] = array( |
||
181 | 'text' => $this->language->get('heading_title'), |
||
182 | 'href' => $this->url->link('localisation/country', 'token=' . $this->session->data['token'] . $url, true) |
||
183 | ); |
||
184 | |||
185 | $data['add'] = $this->url->link('localisation/country/add', 'token=' . $this->session->data['token'] . $url, true); |
||
186 | $data['delete'] = $this->url->link('localisation/country/delete', 'token=' . $this->session->data['token'] . $url, true); |
||
187 | |||
188 | $data['countries'] = array(); |
||
189 | |||
190 | $filter_data = array( |
||
191 | 'sort' => $sort, |
||
192 | 'order' => $order, |
||
193 | 'start' => ($page - 1) * $this->config->get('config_limit_admin'), |
||
194 | 'limit' => $this->config->get('config_limit_admin') |
||
195 | ); |
||
196 | |||
197 | $country_total = $this->model_localisation_country->getTotalCountries(); |
||
198 | |||
199 | $results = $this->model_localisation_country->getCountries($filter_data); |
||
200 | |||
201 | foreach ($results as $result) { |
||
202 | $data['countries'][] = array( |
||
203 | 'country_id' => $result['country_id'], |
||
204 | 'name' => $result['name'] . (($result['country_id'] == $this->config->get('config_country_id')) ? $this->language->get('text_default') : null), |
||
205 | 'iso_code_2' => $result['iso_code_2'], |
||
206 | 'iso_code_3' => $result['iso_code_3'], |
||
207 | 'edit' => $this->url->link('localisation/country/edit', 'token=' . $this->session->data['token'] . '&country_id=' . $result['country_id'] . $url, true) |
||
208 | ); |
||
209 | } |
||
210 | |||
211 | $data['heading_title'] = $this->language->get('heading_title'); |
||
212 | |||
213 | $data['text_list'] = $this->language->get('text_list'); |
||
214 | $data['text_no_results'] = $this->language->get('text_no_results'); |
||
215 | $data['text_confirm'] = $this->language->get('text_confirm'); |
||
216 | |||
217 | $data['column_name'] = $this->language->get('column_name'); |
||
218 | $data['column_iso_code_2'] = $this->language->get('column_iso_code_2'); |
||
219 | $data['column_iso_code_3'] = $this->language->get('column_iso_code_3'); |
||
220 | $data['column_action'] = $this->language->get('column_action'); |
||
221 | |||
222 | $data['button_add'] = $this->language->get('button_add'); |
||
223 | $data['button_edit'] = $this->language->get('button_edit'); |
||
224 | $data['button_delete'] = $this->language->get('button_delete'); |
||
225 | |||
226 | if (isset($this->error['warning'])) { |
||
227 | $data['error_warning'] = $this->error['warning']; |
||
228 | } else { |
||
229 | $data['error_warning'] = ''; |
||
230 | } |
||
231 | |||
232 | if (isset($this->session->data['success'])) { |
||
233 | $data['success'] = $this->session->data['success']; |
||
234 | |||
235 | unset($this->session->data['success']); |
||
236 | } else { |
||
237 | $data['success'] = ''; |
||
238 | } |
||
239 | |||
240 | if (isset($this->request->post['selected'])) { |
||
241 | $data['selected'] = (array)$this->request->post['selected']; |
||
242 | } else { |
||
243 | $data['selected'] = array(); |
||
244 | } |
||
245 | |||
246 | $url = ''; |
||
247 | |||
248 | if ($order == 'ASC') { |
||
249 | $url .= '&order=DESC'; |
||
250 | } else { |
||
251 | $url .= '&order=ASC'; |
||
252 | } |
||
253 | |||
254 | if (isset($this->request->get['page'])) { |
||
255 | $url .= '&page=' . $this->request->get['page']; |
||
256 | } |
||
257 | |||
258 | $data['sort_name'] = $this->url->link('localisation/country', 'token=' . $this->session->data['token'] . '&sort=name' . $url, true); |
||
259 | $data['sort_iso_code_2'] = $this->url->link('localisation/country', 'token=' . $this->session->data['token'] . '&sort=iso_code_2' . $url, true); |
||
260 | $data['sort_iso_code_3'] = $this->url->link('localisation/country', 'token=' . $this->session->data['token'] . '&sort=iso_code_3' . $url, true); |
||
261 | |||
262 | $url = ''; |
||
263 | |||
264 | if (isset($this->request->get['sort'])) { |
||
265 | $url .= '&sort=' . $this->request->get['sort']; |
||
266 | } |
||
267 | |||
268 | if (isset($this->request->get['order'])) { |
||
269 | $url .= '&order=' . $this->request->get['order']; |
||
270 | } |
||
271 | |||
272 | $pagination = new \Divine\Engine\Library\Pagination(); |
||
273 | $pagination->total = $country_total; |
||
274 | $pagination->page = $page; |
||
275 | $pagination->limit = $this->config->get('config_limit_admin'); |
||
276 | $pagination->url = $this->url->link('localisation/country', 'token=' . $this->session->data['token'] . $url . '&page={page}', true); |
||
277 | |||
278 | $data['pagination'] = $pagination->render(); |
||
279 | |||
280 | $data['results'] = sprintf($this->language->get('text_pagination'), ($country_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($country_total - $this->config->get('config_limit_admin'))) ? $country_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $country_total, ceil($country_total / $this->config->get('config_limit_admin'))); |
||
281 | |||
282 | $data['sort'] = $sort; |
||
283 | $data['order'] = $order; |
||
284 | |||
285 | $data['header'] = $this->load->controller('common/header'); |
||
286 | $data['column'] = $this->load->controller('common/column_left'); |
||
287 | $data['footer'] = $this->load->controller('common/footer'); |
||
288 | |||
289 | $this->response->setOutput($this->load->view('localisation/country_list', $data)); |
||
290 | } |
||
291 | |||
292 | protected function getForm() |
||
293 | { |
||
294 | $data['heading_title'] = $this->language->get('heading_title'); |
||
295 | |||
296 | $data['text_form'] = !isset($this->request->get['country_id']) ? $this->language->get('text_add') : $this->language->get('text_edit'); |
||
297 | $data['text_enabled'] = $this->language->get('text_enabled'); |
||
298 | $data['text_disabled'] = $this->language->get('text_disabled'); |
||
299 | $data['text_yes'] = $this->language->get('text_yes'); |
||
300 | $data['text_no'] = $this->language->get('text_no'); |
||
301 | |||
302 | $data['entry_name'] = $this->language->get('entry_name'); |
||
303 | $data['entry_iso_code_2'] = $this->language->get('entry_iso_code_2'); |
||
304 | $data['entry_iso_code_3'] = $this->language->get('entry_iso_code_3'); |
||
305 | $data['entry_address_format'] = $this->language->get('entry_address_format'); |
||
306 | $data['entry_postcode_required'] = $this->language->get('entry_postcode_required'); |
||
307 | $data['entry_status'] = $this->language->get('entry_status'); |
||
308 | |||
309 | $data['help_address_format'] = $this->language->get('help_address_format'); |
||
310 | |||
311 | $data['button_save'] = $this->language->get('button_save'); |
||
312 | $data['button_cancel'] = $this->language->get('button_cancel'); |
||
313 | |||
314 | if (isset($this->error['warning'])) { |
||
315 | $data['error_warning'] = $this->error['warning']; |
||
316 | } else { |
||
317 | $data['error_warning'] = ''; |
||
318 | } |
||
319 | |||
320 | if (isset($this->error['name'])) { |
||
321 | $data['error_name'] = $this->error['name']; |
||
322 | } else { |
||
323 | $data['error_name'] = ''; |
||
324 | } |
||
325 | |||
326 | $url = ''; |
||
327 | |||
328 | if (isset($this->request->get['sort'])) { |
||
329 | $url .= '&sort=' . $this->request->get['sort']; |
||
330 | } |
||
331 | |||
332 | if (isset($this->request->get['order'])) { |
||
333 | $url .= '&order=' . $this->request->get['order']; |
||
334 | } |
||
335 | |||
336 | if (isset($this->request->get['page'])) { |
||
337 | $url .= '&page=' . $this->request->get['page']; |
||
338 | } |
||
339 | |||
340 | $data['breadcrumbs'] = array(); |
||
341 | |||
342 | $data['breadcrumbs'][] = array( |
||
343 | 'text' => $this->language->get('text_home'), |
||
344 | 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true) |
||
345 | ); |
||
346 | |||
347 | $data['breadcrumbs'][] = array( |
||
348 | 'text' => $this->language->get('heading_title'), |
||
349 | 'href' => $this->url->link('localisation/country', 'token=' . $this->session->data['token'] . $url, true) |
||
350 | ); |
||
351 | |||
352 | if (!isset($this->request->get['country_id'])) { |
||
353 | $data['action'] = $this->url->link('localisation/country/add', 'token=' . $this->session->data['token'] . $url, true); |
||
354 | } else { |
||
355 | $data['action'] = $this->url->link('localisation/country/edit', 'token=' . $this->session->data['token'] . '&country_id=' . $this->request->get['country_id'] . $url, true); |
||
356 | } |
||
357 | |||
358 | $data['cancel'] = $this->url->link('localisation/country', 'token=' . $this->session->data['token'] . $url, true); |
||
359 | |||
360 | if (isset($this->request->get['country_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { |
||
361 | $country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']); |
||
362 | } |
||
363 | |||
364 | if (isset($this->request->post['name'])) { |
||
365 | $data['name'] = $this->request->post['name']; |
||
366 | } elseif (!empty($country_info)) { |
||
367 | $data['name'] = $country_info['name']; |
||
368 | } else { |
||
369 | $data['name'] = ''; |
||
370 | } |
||
371 | |||
372 | if (isset($this->request->post['iso_code_2'])) { |
||
373 | $data['iso_code_2'] = $this->request->post['iso_code_2']; |
||
374 | } elseif (!empty($country_info)) { |
||
375 | $data['iso_code_2'] = $country_info['iso_code_2']; |
||
376 | } else { |
||
377 | $data['iso_code_2'] = ''; |
||
378 | } |
||
379 | |||
380 | if (isset($this->request->post['iso_code_3'])) { |
||
381 | $data['iso_code_3'] = $this->request->post['iso_code_3']; |
||
382 | } elseif (!empty($country_info)) { |
||
383 | $data['iso_code_3'] = $country_info['iso_code_3']; |
||
384 | } else { |
||
385 | $data['iso_code_3'] = ''; |
||
386 | } |
||
387 | |||
388 | if (isset($this->request->post['address_format'])) { |
||
389 | $data['address_format'] = $this->request->post['address_format']; |
||
390 | } elseif (!empty($country_info)) { |
||
391 | $data['address_format'] = $country_info['address_format']; |
||
392 | } else { |
||
393 | $data['address_format'] = ''; |
||
394 | } |
||
395 | |||
396 | if (isset($this->request->post['postcode_required'])) { |
||
397 | $data['postcode_required'] = $this->request->post['postcode_required']; |
||
398 | } elseif (!empty($country_info)) { |
||
399 | $data['postcode_required'] = $country_info['postcode_required']; |
||
400 | } else { |
||
401 | $data['postcode_required'] = 0; |
||
402 | } |
||
403 | |||
404 | if (isset($this->request->post['status'])) { |
||
405 | $data['status'] = $this->request->post['status']; |
||
406 | } elseif (!empty($country_info)) { |
||
407 | $data['status'] = $country_info['status']; |
||
408 | } else { |
||
409 | $data['status'] = '1'; |
||
410 | } |
||
411 | |||
412 | $data['header'] = $this->load->controller('common/header'); |
||
413 | $data['column'] = $this->load->controller('common/column_left'); |
||
414 | $data['footer'] = $this->load->controller('common/footer'); |
||
415 | |||
416 | $this->response->setOutput($this->load->view('localisation/country_form', $data)); |
||
417 | } |
||
418 | |||
419 | protected function validateForm() |
||
420 | { |
||
421 | if (!$this->user->hasPermission('modify', 'localisation/country')) { |
||
422 | $this->error['warning'] = $this->language->get('error_permission'); |
||
423 | } |
||
424 | |||
425 | if ((\voku\helper\UTF8::strlen($this->request->post['name']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['name']) > 128)) { |
||
426 | $this->error['name'] = $this->language->get('error_name'); |
||
427 | } |
||
428 | |||
429 | return !$this->error; |
||
430 | } |
||
431 | |||
432 | protected function validateDelete() |
||
467 | } |
||
468 | |||
469 | public function country() |
||
494 | } |
||
495 | } |
||
496 |
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.