Total Complexity | 102 |
Total Lines | 606 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ControllerCatalogManufacturer 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 ControllerCatalogManufacturer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ControllerCatalogManufacturer extends \Divine\Engine\Core\Controller |
||
|
|||
24 | { |
||
25 | private $error = array(); |
||
26 | |||
27 | public function index() |
||
28 | { |
||
29 | $this->load->language('catalog/manufacturer'); |
||
30 | |||
31 | $this->document->setTitle($this->language->get('heading_title')); |
||
32 | |||
33 | $this->load->model('catalog/manufacturer'); |
||
34 | |||
35 | $this->getList(); |
||
36 | } |
||
37 | |||
38 | public function add() |
||
39 | { |
||
40 | $this->load->language('catalog/manufacturer'); |
||
41 | |||
42 | $this->document->setTitle($this->language->get('heading_title')); |
||
43 | |||
44 | $this->load->model('catalog/manufacturer'); |
||
45 | |||
46 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
47 | $this->model_catalog_manufacturer->addManufacturer($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('catalog/manufacturer', 'token=' . $this->session->data['token'] . $url, true)); |
||
66 | } |
||
67 | |||
68 | $this->getForm(); |
||
69 | } |
||
70 | |||
71 | public function edit() |
||
72 | { |
||
73 | $this->load->language('catalog/manufacturer'); |
||
74 | |||
75 | $this->document->setTitle($this->language->get('heading_title')); |
||
76 | |||
77 | $this->load->model('catalog/manufacturer'); |
||
78 | |||
79 | if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { |
||
80 | $this->model_catalog_manufacturer->editManufacturer($this->request->get['manufacturer_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('catalog/manufacturer', 'token=' . $this->session->data['token'] . $url, true)); |
||
99 | } |
||
100 | |||
101 | $this->getForm(); |
||
102 | } |
||
103 | |||
104 | public function delete() |
||
105 | { |
||
106 | $this->load->language('catalog/manufacturer'); |
||
107 | |||
108 | $this->document->setTitle($this->language->get('heading_title')); |
||
109 | |||
110 | $this->load->model('catalog/manufacturer'); |
||
111 | |||
112 | if (isset($this->request->post['selected']) && $this->validateDelete()) { |
||
113 | foreach ($this->request->post['selected'] as $manufacturer_id) { |
||
114 | $this->model_catalog_manufacturer->deleteManufacturer($manufacturer_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('catalog/manufacturer', '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('catalog/manufacturer', 'token=' . $this->session->data['token'] . $url, true) |
||
183 | ); |
||
184 | |||
185 | $data['add'] = $this->url->link('catalog/manufacturer/add', 'token=' . $this->session->data['token'] . $url, true); |
||
186 | $data['delete'] = $this->url->link('catalog/manufacturer/delete', 'token=' . $this->session->data['token'] . $url, true); |
||
187 | |||
188 | $data['manufacturers'] = 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 | $manufacturer_total = $this->model_catalog_manufacturer->getTotalManufacturers(); |
||
198 | |||
199 | $results = $this->model_catalog_manufacturer->getManufacturers($filter_data); |
||
200 | |||
201 | foreach ($results as $result) { |
||
202 | $data['manufacturers'][] = array( |
||
203 | 'manufacturer_id' => $result['manufacturer_id'], |
||
204 | 'name' => $result['name'], |
||
205 | 'sort_order' => $result['sort_order'], |
||
206 | 'noindex' => $result['noindex'], |
||
207 | 'href_shop' => '/index.php?route=product/manufacturer/info&manufacturer_id=' . ($result['manufacturer_id']), |
||
208 | 'edit' => $this->url->link('catalog/manufacturer/edit', 'token=' . $this->session->data['token'] . '&manufacturer_id=' . $result['manufacturer_id'] . $url, true) |
||
209 | ); |
||
210 | } |
||
211 | |||
212 | $data['heading_title'] = $this->language->get('heading_title'); |
||
213 | |||
214 | $data['text_list'] = $this->language->get('text_list'); |
||
215 | $data['text_no_results'] = $this->language->get('text_no_results'); |
||
216 | $data['text_confirm'] = $this->language->get('text_confirm'); |
||
217 | |||
218 | $data['column_name'] = $this->language->get('column_name'); |
||
219 | $data['column_sort_order'] = $this->language->get('column_sort_order'); |
||
220 | $data['column_noindex'] = $this->language->get('column_noindex'); |
||
221 | $data['column_action'] = $this->language->get('column_action'); |
||
222 | |||
223 | $data['button_add'] = $this->language->get('button_add'); |
||
224 | $data['button_edit'] = $this->language->get('button_edit'); |
||
225 | $data['button_shop'] = $this->language->get('button_shop'); |
||
226 | $data['button_delete'] = $this->language->get('button_delete'); |
||
227 | |||
228 | if (isset($this->error['warning'])) { |
||
229 | $data['error_warning'] = $this->error['warning']; |
||
230 | } else { |
||
231 | $data['error_warning'] = ''; |
||
232 | } |
||
233 | |||
234 | if (isset($this->session->data['success'])) { |
||
235 | $data['success'] = $this->session->data['success']; |
||
236 | |||
237 | unset($this->session->data['success']); |
||
238 | } else { |
||
239 | $data['success'] = ''; |
||
240 | } |
||
241 | |||
242 | if (isset($this->request->post['selected'])) { |
||
243 | $data['selected'] = (array) $this->request->post['selected']; |
||
244 | } else { |
||
245 | $data['selected'] = array(); |
||
246 | } |
||
247 | |||
248 | $url = ''; |
||
249 | |||
250 | if ($order == 'ASC') { |
||
251 | $url .= '&order=DESC'; |
||
252 | } else { |
||
253 | $url .= '&order=ASC'; |
||
254 | } |
||
255 | |||
256 | if (isset($this->request->get['page'])) { |
||
257 | $url .= '&page=' . $this->request->get['page']; |
||
258 | } |
||
259 | |||
260 | $data['sort_name'] = $this->url->link('catalog/manufacturer', 'token=' . $this->session->data['token'] . '&sort=name' . $url, true); |
||
261 | $data['sort_sort_order'] = $this->url->link('catalog/manufacturer', 'token=' . $this->session->data['token'] . '&sort=sort_order' . $url, true); |
||
262 | $data['sort_noindex'] = $this->url->link('catalog/manufacturer', 'token=' . $this->session->data['token'] . '&sort=noindex' . $url, true); |
||
263 | |||
264 | $url = ''; |
||
265 | |||
266 | if (isset($this->request->get['sort'])) { |
||
267 | $url .= '&sort=' . $this->request->get['sort']; |
||
268 | } |
||
269 | |||
270 | if (isset($this->request->get['order'])) { |
||
271 | $url .= '&order=' . $this->request->get['order']; |
||
272 | } |
||
273 | |||
274 | $pagination = new \Divine\Engine\Library\Pagination(); |
||
275 | $pagination->total = $manufacturer_total; |
||
276 | $pagination->page = $page; |
||
277 | $pagination->limit = $this->config->get('config_limit_admin'); |
||
278 | $pagination->url = $this->url->link('catalog/manufacturer', 'token=' . $this->session->data['token'] . $url . '&page={page}', true); |
||
279 | |||
280 | $data['pagination'] = $pagination->render(); |
||
281 | |||
282 | $data['results'] = sprintf($this->language->get('text_pagination'), ($manufacturer_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($manufacturer_total - $this->config->get('config_limit_admin'))) ? $manufacturer_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $manufacturer_total, ceil($manufacturer_total / $this->config->get('config_limit_admin'))); |
||
283 | |||
284 | $data['sort'] = $sort; |
||
285 | $data['order'] = $order; |
||
286 | |||
287 | $data['header'] = $this->load->controller('common/header'); |
||
288 | $data['column'] = $this->load->controller('common/column_left'); |
||
289 | $data['footer'] = $this->load->controller('common/footer'); |
||
290 | |||
291 | $this->response->setOutput($this->load->view('catalog/manufacturer_list', $data)); |
||
292 | } |
||
293 | |||
294 | protected function getForm() |
||
533 | } |
||
534 | |||
535 | protected function validateForm() |
||
536 | { |
||
537 | if (!$this->user->hasPermission('modify', 'catalog/manufacturer')) { |
||
538 | $this->error['warning'] = $this->language->get('error_permission'); |
||
539 | } |
||
540 | |||
541 | if ((\voku\helper\UTF8::strlen($this->request->post['name']) < 2) || (\voku\helper\UTF8::strlen($this->request->post['name']) > 64)) { |
||
542 | $this->error['name'] = $this->language->get('error_name'); |
||
543 | } |
||
544 | |||
545 | foreach ($this->request->post['manufacturer_description'] as $language_id => $value) { |
||
546 | if ((\voku\helper\UTF8::strlen($value['meta_title']) < 0) || (\voku\helper\UTF8::strlen($value['meta_title']) > 255)) { |
||
547 | $this->error['meta_title'][$language_id] = $this->language->get('error_meta_title'); |
||
548 | $this->error['warning'] = $this->language->get('error_warning'); |
||
549 | } |
||
550 | |||
551 | if ((\voku\helper\UTF8::strlen($value['meta_h1']) < 0) || (\voku\helper\UTF8::strlen($value['meta_h1']) > 255)) { |
||
552 | $this->error['meta_h1'][$language_id] = $this->language->get('error_meta_h1'); |
||
553 | $this->error['warning'] = $this->language->get('error_warning'); |
||
554 | } |
||
555 | } |
||
556 | if ($this->error && !isset($this->error['warning'])) { |
||
557 | $this->error['warning'] = $this->language->get('error_warning'); |
||
558 | } |
||
559 | |||
560 | if (\voku\helper\UTF8::strlen($this->request->post['keyword']) > 0) { |
||
561 | $this->load->model('catalog/url_alias'); |
||
562 | |||
563 | $url_alias_info = $this->model_catalog_url_alias->getUrlAlias($this->request->post['keyword']); |
||
564 | |||
565 | if ($url_alias_info && isset($this->request->get['manufacturer_id']) && $url_alias_info['query'] != 'manufacturer_id=' . $this->request->get['manufacturer_id']) { |
||
566 | $this->error['keyword'] = sprintf($this->language->get('error_keyword')); |
||
567 | } |
||
568 | |||
569 | if ($url_alias_info && !isset($this->request->get['manufacturer_id'])) { |
||
570 | $this->error['keyword'] = sprintf($this->language->get('error_keyword')); |
||
571 | } |
||
572 | } |
||
573 | |||
574 | return !$this->error; |
||
575 | } |
||
576 | |||
577 | protected function validateDelete() |
||
578 | { |
||
579 | if (!$this->user->hasPermission('modify', 'catalog/manufacturer')) { |
||
580 | $this->error['warning'] = $this->language->get('error_permission'); |
||
581 | } |
||
582 | |||
583 | $this->load->model('catalog/product'); |
||
584 | |||
585 | foreach ($this->request->post['selected'] as $manufacturer_id) { |
||
586 | $product_total = $this->model_catalog_product->getTotalProductsByManufacturerId($manufacturer_id); |
||
587 | |||
588 | if ($product_total) { |
||
589 | $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total); |
||
590 | } |
||
591 | } |
||
592 | |||
593 | return !$this->error; |
||
594 | } |
||
595 | |||
596 | public function autocomplete() |
||
629 | } |
||
630 | } |
||
631 |
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.