Total Complexity | 77 |
Total Lines | 470 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ControllerDesignBenefit 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 ControllerDesignBenefit, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ControllerDesignBenefit extends \Divine\Engine\Core\Controller |
||
|
|||
24 | { |
||
25 | private $error = array(); |
||
26 | |||
27 | public function index() |
||
28 | { |
||
29 | $this->language->load('design/benefit'); |
||
30 | |||
31 | $this->document->setTitle($this->language->get('heading_title')); |
||
32 | |||
33 | $this->load->model('design/benefit'); |
||
34 | |||
35 | $this->getList(); |
||
36 | } |
||
37 | |||
38 | public function add() |
||
39 | { |
||
40 | $this->language->load('design/benefit'); |
||
41 | |||
42 | $this->document->setTitle($this->language->get('heading_title')); |
||
43 | |||
44 | $this->load->model('design/benefit'); |
||
45 | |||
46 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
47 | $this->model_design_benefit->addBenefit($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('design/benefit', 'token=' . $this->session->data['token'] . $url, true)); |
||
66 | } |
||
67 | |||
68 | $this->getForm(); |
||
69 | } |
||
70 | |||
71 | public function edit() |
||
72 | { |
||
73 | $this->language->load('design/benefit'); |
||
74 | |||
75 | $this->document->setTitle($this->language->get('heading_title')); |
||
76 | |||
77 | $this->load->model('design/benefit'); |
||
78 | |||
79 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
80 | $this->model_design_benefit->editBenefit($this->request->get['benefit_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('design/benefit', 'token=' . $this->session->data['token'] . $url, true)); |
||
99 | } |
||
100 | |||
101 | $this->getForm(); |
||
102 | } |
||
103 | |||
104 | public function delete() |
||
105 | { |
||
106 | $this->language->load('design/benefit'); |
||
107 | |||
108 | $this->document->setTitle($this->language->get('heading_title')); |
||
109 | |||
110 | $this->load->model('design/benefit'); |
||
111 | |||
112 | if (isset($this->request->post['selected']) && $this->validateDelete()) { |
||
113 | foreach ($this->request->post['selected'] as $benefit_id) { |
||
114 | $this->model_design_benefit->deleteBenefit($benefit_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('design/benefit', 'token=' . $this->session->data['token'] . $url, true)); |
||
134 | } |
||
135 | |||
136 | $this->getList(); |
||
137 | } |
||
138 | |||
139 | protected function getList() |
||
301 | } |
||
302 | |||
303 | protected function getForm() |
||
304 | { |
||
305 | $data['heading_title'] = $this->language->get('heading_title'); |
||
306 | |||
307 | $data['text_form'] = !isset($this->request->get['benefit_id']) ? $this->language->get('text_add') : $this->language->get('text_edit'); |
||
308 | $data['text_enabled'] = $this->language->get('text_enabled'); |
||
309 | $data['text_disabled'] = $this->language->get('text_disabled'); |
||
310 | $data['text_default'] = $this->language->get('text_default'); |
||
311 | $data['text_image_manager'] = $this->language->get('text_image_manager'); |
||
312 | $data['text_browse'] = $this->language->get('text_browse'); |
||
313 | $data['text_clear'] = $this->language->get('text_clear'); |
||
314 | $data['text_type_benefit'] = $this->language->get('text_type_benefit'); |
||
315 | $data['text_type_gift'] = $this->language->get('text_type_gift'); |
||
316 | |||
317 | $data['entry_name'] = $this->language->get('entry_name'); |
||
318 | $data['entry_image'] = $this->language->get('entry_image'); |
||
319 | $data['entry_link'] = $this->language->get('entry_link'); |
||
320 | $data['entry_status'] = $this->language->get('entry_status'); |
||
321 | $data['entry_type'] = $this->language->get('entry_type'); |
||
322 | $data['entry_description'] = $this->language->get('entry_description'); |
||
323 | |||
324 | $data['button_save'] = $this->language->get('button_save'); |
||
325 | $data['button_cancel'] = $this->language->get('button_cancel'); |
||
326 | $data['button_remove'] = $this->language->get('button_remove'); |
||
327 | |||
328 | if (isset($this->error['warning'])) { |
||
329 | $data['error_warning'] = $this->error['warning']; |
||
330 | } else { |
||
331 | $data['error_warning'] = ''; |
||
332 | } |
||
333 | |||
334 | if (isset($this->error['name'])) { |
||
335 | $data['error_name'] = $this->error['name']; |
||
336 | } else { |
||
337 | $data['error_name'] = ''; |
||
338 | } |
||
339 | |||
340 | $url = ''; |
||
341 | |||
342 | if (isset($this->request->get['sort'])) { |
||
343 | $url .= '&sort=' . $this->request->get['sort']; |
||
344 | } |
||
345 | |||
346 | if (isset($this->request->get['order'])) { |
||
347 | $url .= '&order=' . $this->request->get['order']; |
||
348 | } |
||
349 | |||
350 | if (isset($this->request->get['page'])) { |
||
351 | $url .= '&page=' . $this->request->get['page']; |
||
352 | } |
||
353 | |||
354 | $data['breadcrumbs'] = array(); |
||
355 | |||
356 | $data['breadcrumbs'][] = array( |
||
357 | 'text' => $this->language->get('text_home'), |
||
358 | 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true), |
||
359 | 'separator' => false |
||
360 | ); |
||
361 | |||
362 | $data['breadcrumbs'][] = array( |
||
363 | 'text' => $this->language->get('heading_title'), |
||
364 | 'href' => $this->url->link('design/benefit', 'token=' . $this->session->data['token'] . $url, true), |
||
365 | 'separator' => ' :: ' |
||
366 | ); |
||
367 | |||
368 | if (!isset($this->request->get['benefit_id'])) { |
||
369 | $data['action'] = $this->url->link('design/benefit/add', 'token=' . $this->session->data['token'] . $url, true); |
||
370 | } else { |
||
371 | $data['action'] = $this->url->link('design/benefit/edit', 'token=' . $this->session->data['token'] . '&benefit_id=' . $this->request->get['benefit_id'] . $url, true); |
||
372 | } |
||
373 | |||
374 | $data['cancel'] = $this->url->link('design/benefit', 'token=' . $this->session->data['token'] . $url, true); |
||
375 | |||
376 | if (isset($this->request->get['benefit_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { |
||
377 | $benefit_info = $this->model_design_benefit->getBenefit($this->request->get['benefit_id']); |
||
378 | } |
||
379 | |||
380 | $data['token'] = $this->session->data['token']; |
||
381 | |||
382 | |||
383 | $this->load->model('localisation/language'); |
||
384 | |||
385 | $data['languages'] = $this->model_localisation_language->getLanguages(); |
||
386 | |||
387 | if (isset($this->request->post['benefit_description'])) { |
||
388 | $data['benefit_description'] = $this->request->post['benefit_description']; |
||
389 | } elseif (isset($this->request->get['benefit_id'])) { |
||
390 | $data['benefit_description'] = $this->model_design_benefit->getBenefitDescriptions($this->request->get['benefit_id']); |
||
391 | } else { |
||
392 | $data['benefit_description'] = array(); |
||
393 | } |
||
394 | |||
395 | |||
396 | if (isset($this->request->post['name'])) { |
||
397 | $data['name'] = $this->request->post['name']; |
||
398 | } elseif (!empty($benefit_info)) { |
||
399 | $data['name'] = $benefit_info['name']; |
||
400 | } else { |
||
401 | $data['name'] = ''; |
||
402 | } |
||
403 | |||
404 | if (isset($this->request->post['status'])) { |
||
405 | $data['status'] = $this->request->post['status']; |
||
406 | } elseif (!empty($benefit_info)) { |
||
407 | $data['status'] = $benefit_info['status']; |
||
408 | } else { |
||
409 | $data['status'] = true; |
||
410 | } |
||
411 | |||
412 | if (isset($this->request->post['type'])) { |
||
413 | $data['type'] = $this->request->post['type']; |
||
414 | } elseif (!empty($benefit_info)) { |
||
415 | $data['type'] = $benefit_info['type']; |
||
416 | } else { |
||
417 | $data['type'] = true; |
||
418 | } |
||
419 | |||
420 | if (isset($this->request->post['link'])) { |
||
421 | $data['link'] = $this->request->post['link']; |
||
422 | } elseif (!empty($benefit_info)) { |
||
423 | $data['link'] = $benefit_info['link']; |
||
424 | } else { |
||
425 | $data['link'] = ''; |
||
426 | } |
||
427 | |||
428 | if (isset($this->request->post['image'])) { |
||
429 | $data['image'] = $this->request->post['image']; |
||
430 | } elseif (!empty($benefit_info)) { |
||
431 | $data['image'] = $benefit_info['image']; |
||
432 | } else { |
||
433 | $data['image'] = ''; |
||
434 | } |
||
435 | |||
436 | |||
437 | |||
438 | if (isset($this->request->post['image']) && file_exists($_SERVER['DOCUMENT_ROOT'] . '/public_html/assets/images/' . $this->request->post['image'])) { |
||
439 | $data['thumb'] = '/public_html/assets/images/' . $this->request->post['image']; |
||
440 | } elseif (!empty($benefit_info) && $benefit_info['image'] && file_exists($_SERVER['DOCUMENT_ROOT'] . '/public_html/assets/images/' . $benefit_info['image'])) { |
||
441 | $data['thumb'] = '/public_html/assets/images/' . $benefit_info['image']; |
||
442 | } else { |
||
443 | $data['thumb'] = '/public_html/assets/images/no_image.png'; |
||
444 | } |
||
445 | |||
446 | $data['placeholder'] = '/public_html/assets/images/no_image.png'; |
||
447 | |||
448 | $data['header'] = $this->load->controller('common/header'); |
||
449 | $data['column'] = $this->load->controller('common/column_left'); |
||
450 | $data['footer'] = $this->load->controller('common/footer'); |
||
451 | |||
452 | $this->response->setOutput($this->load->view('design/benefit_form', $data)); |
||
453 | } |
||
454 | |||
455 | protected function validateForm() |
||
469 | } |
||
470 | } |
||
471 | |||
472 | protected function validateDelete() |
||
493 | } |
||
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.