Total Complexity | 61 |
Total Lines | 438 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ControllerCheckoutCart 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 ControllerCheckoutCart, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ControllerCheckoutCart extends \Divine\Engine\Core\Controller |
||
|
|||
24 | { |
||
25 | public function index() |
||
26 | { |
||
27 | $this->load->language('checkout/cart'); |
||
28 | |||
29 | $this->document->setTitle($this->language->get('heading_title')); |
||
30 | |||
31 | $data['breadcrumbs'] = array(); |
||
32 | |||
33 | $data['breadcrumbs'][] = array( |
||
34 | 'href' => $this->url->link('common/home'), |
||
35 | 'text' => $this->language->get('text_home') |
||
36 | ); |
||
37 | |||
38 | $data['breadcrumbs'][] = array( |
||
39 | 'href' => $this->url->link('checkout/cart'), |
||
40 | 'text' => $this->language->get('heading_title') |
||
41 | ); |
||
42 | |||
43 | if ($this->cart->hasProducts()) { |
||
44 | $data['heading_title'] = $this->language->get('heading_title'); |
||
45 | |||
46 | $data['text_next'] = $this->language->get('text_next'); |
||
47 | $data['text_next_choice'] = $this->language->get('text_next_choice'); |
||
48 | |||
49 | $data['column_image'] = $this->language->get('column_image'); |
||
50 | $data['column_name'] = $this->language->get('column_name'); |
||
51 | $data['column_model'] = $this->language->get('column_model'); |
||
52 | $data['column_quantity'] = $this->language->get('column_quantity'); |
||
53 | $data['column_price'] = $this->language->get('column_price'); |
||
54 | $data['column_total'] = $this->language->get('column_total'); |
||
55 | |||
56 | $data['button_update'] = $this->language->get('button_update'); |
||
57 | $data['button_remove'] = $this->language->get('button_remove'); |
||
58 | $data['button_shopping'] = $this->language->get('button_shopping'); |
||
59 | $data['button_checkout'] = $this->language->get('button_checkout'); |
||
60 | |||
61 | if (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) { |
||
62 | $data['error_warning'] = $this->language->get('error_stock'); |
||
63 | } elseif (isset($this->session->data['error'])) { |
||
64 | $data['error_warning'] = $this->session->data['error']; |
||
65 | |||
66 | unset($this->session->data['error']); |
||
67 | } else { |
||
68 | $data['error_warning'] = ''; |
||
69 | } |
||
70 | |||
71 | if ($this->config->get('config_customer_price') && !$this->customer->isLogged()) { |
||
72 | $data['attention'] = sprintf($this->language->get('text_login'), $this->url->link('account/login'), $this->url->link('account/register')); |
||
73 | } else { |
||
74 | $data['attention'] = ''; |
||
75 | } |
||
76 | |||
77 | if (isset($this->session->data['success'])) { |
||
78 | $data['success'] = $this->session->data['success']; |
||
79 | |||
80 | unset($this->session->data['success']); |
||
81 | } else { |
||
82 | $data['success'] = ''; |
||
83 | } |
||
84 | |||
85 | $data['action'] = $this->url->link('checkout/cart/edit', '', true); |
||
86 | |||
87 | |||
88 | $this->load->model('tool/upload'); |
||
89 | |||
90 | $data['products'] = array(); |
||
91 | |||
92 | $products = $this->cart->getProducts(); |
||
93 | |||
94 | foreach ($products as $product) { |
||
95 | $product_total = 0; |
||
96 | |||
97 | foreach ($products as $product_2) { |
||
98 | if ($product_2['product_id'] == $product['product_id']) { |
||
99 | $product_total += $product_2['quantity']; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | if ($product['minimum'] > $product_total) { |
||
104 | $data['error_warning'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']); |
||
105 | } |
||
106 | |||
107 | if ($product['image']) { |
||
108 | $image = '/public_html/assets/images/' . $product['image']; |
||
109 | } else { |
||
110 | $image = '/public_html/assets/images/no_image.png'; |
||
111 | } |
||
112 | |||
113 | $option_data = array(); |
||
114 | |||
115 | foreach ($product['option'] as $option) { |
||
116 | if ($option['type'] != 'file') { |
||
117 | $value = $option['value']; |
||
118 | } else { |
||
119 | $upload_info = $this->model_tool_upload->getUploadByCode($option['value']); |
||
120 | |||
121 | if ($upload_info) { |
||
122 | $value = $upload_info['name']; |
||
123 | } else { |
||
124 | $value = ''; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | $option_data[] = array( |
||
129 | 'name' => $option['name'], |
||
130 | 'value' => (\voku\helper\UTF8::strlen($value) > 20 ? \voku\helper\UTF8::substr($value, 0, 20) . '..' : $value) |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | // Display prices |
||
135 | if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { |
||
136 | $unit_price = $product['price']; |
||
137 | |||
138 | $price = $this->currency->format($unit_price, $this->session->data['currency']); |
||
139 | $total = $this->currency->format($unit_price * $product['quantity'], $this->session->data['currency']); |
||
140 | } else { |
||
141 | $price = false; |
||
142 | $total = false; |
||
143 | } |
||
144 | |||
145 | |||
146 | $data['products'][] = array( |
||
147 | 'cart_id' => $product['cart_id'], |
||
148 | 'thumb' => $image, |
||
149 | 'name' => $product['name'], |
||
150 | 'model' => $product['model'], |
||
151 | 'option' => $option_data, |
||
152 | 'quantity' => $product['quantity'], |
||
153 | 'stock' => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')), |
||
154 | 'reward' => ($product['reward'] ? sprintf($this->language->get('text_points'), $product['reward']) : ''), |
||
155 | 'price' => $price, |
||
156 | 'total' => $total, |
||
157 | 'href' => $this->url->link('product/product', 'product_id=' . $product['product_id']) |
||
158 | ); |
||
159 | } |
||
160 | |||
161 | // Totals |
||
162 | $this->load->model('extension/extension'); |
||
163 | |||
164 | $totals = array(); |
||
165 | $total = 0; |
||
166 | |||
167 | // Because __call can not keep var references so we put them into an array. |
||
168 | $total_data = array( |
||
169 | 'totals' => &$totals, |
||
170 | 'total' => &$total |
||
171 | ); |
||
172 | |||
173 | // Display prices |
||
174 | if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { |
||
175 | $sort_order = array(); |
||
176 | |||
177 | $results = $this->model_extension_extension->getExtensions('total'); |
||
178 | |||
179 | foreach ($results as $key => $value) { |
||
180 | $sort_order[$key] = $this->config->get($value['code'] . '_sort_order'); |
||
181 | } |
||
182 | |||
183 | array_multisort($sort_order, SORT_ASC, $results); |
||
184 | |||
185 | foreach ($results as $result) { |
||
186 | if ($this->config->get($result['code'] . '_status')) { |
||
187 | $this->load->model('extension/total/' . $result['code']); |
||
188 | |||
189 | // We have to put the totals in an array so that they pass by reference. |
||
190 | $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | $sort_order = array(); |
||
195 | |||
196 | foreach ($totals as $key => $value) { |
||
197 | $sort_order[$key] = $value['sort_order']; |
||
198 | } |
||
199 | |||
200 | array_multisort($sort_order, SORT_ASC, $totals); |
||
201 | } |
||
202 | |||
203 | $data['totals'] = array(); |
||
204 | |||
205 | foreach ($totals as $total) { |
||
206 | $data['totals'][] = array( |
||
207 | 'title' => $total['title'], |
||
208 | 'text' => $this->currency->format($total['value'], $this->session->data['currency']) |
||
209 | ); |
||
210 | } |
||
211 | |||
212 | $data['continue'] = $this->url->link('common/home'); |
||
213 | |||
214 | // $data['checkout'] = $this->url->link('checkout/checkout', '', true); |
||
215 | $data['checkout'] = $this->url->link('checkout/onepagecheckout', '', true); |
||
216 | |||
217 | $this->load->model('extension/extension'); |
||
218 | |||
219 | $data['modules'] = array(); |
||
220 | |||
221 | $files = glob(SR_APPLICATION . '/controller/extension/total/*.php'); |
||
222 | |||
223 | if ($files) { |
||
224 | foreach ($files as $file) { |
||
225 | $result = $this->load->controller('extension/total/' . basename($file, '.php')); |
||
226 | |||
227 | if ($result) { |
||
228 | $data['modules'][] = $result; |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | |||
233 | $data['column'] = $this->load->controller('common/column'); |
||
234 | |||
235 | $data['content_top'] = $this->load->controller('common/content_top'); |
||
236 | $data['content_bottom'] = $this->load->controller('common/content_bottom'); |
||
237 | $data['footer'] = $this->load->controller('common/footer'); |
||
238 | $data['header'] = $this->load->controller('common/header'); |
||
239 | |||
240 | $this->response->setOutput($this->load->view('checkout/cart', $data)); |
||
241 | } else { |
||
242 | $data['heading_title'] = $this->language->get('heading_title'); |
||
243 | |||
244 | $data['text_error'] = $this->language->get('text_empty'); |
||
245 | $data['text_go_back'] = $this->language->get('text_go_back'); |
||
246 | $data['text_get_back'] = $this->language->get('text_get_back'); |
||
247 | |||
248 | $data['button_continue'] = $this->language->get('button_continue'); |
||
249 | |||
250 | $data['continue'] = $this->url->link('common/home'); |
||
251 | |||
252 | unset($this->session->data['success']); |
||
253 | |||
254 | $data['column'] = $this->load->controller('common/column'); |
||
255 | |||
256 | $data['content_top'] = $this->load->controller('common/content_top'); |
||
257 | $data['content_bottom'] = $this->load->controller('common/content_bottom'); |
||
258 | $data['footer'] = $this->load->controller('common/footer'); |
||
259 | $data['header'] = $this->load->controller('common/header'); |
||
260 | |||
261 | $this->response->setOutput($this->load->view('error/not_found', $data)); |
||
262 | } |
||
263 | } |
||
264 | |||
265 | public function add() |
||
266 | { |
||
267 | $this->load->language('checkout/cart'); |
||
268 | |||
269 | $json = array(); |
||
270 | |||
271 | if (isset($this->request->post['product_id'])) { |
||
272 | $product_id = (int)$this->request->post['product_id']; |
||
273 | } else { |
||
274 | $product_id = 0; |
||
275 | } |
||
276 | |||
277 | $this->load->model('catalog/product'); |
||
278 | |||
279 | $product_info = $this->model_catalog_product->getProduct($product_id); |
||
280 | |||
281 | if ($product_info) { |
||
282 | if (isset($this->request->post['quantity']) && ((int)$this->request->post['quantity'] >= $product_info['minimum'])) { |
||
283 | $quantity = (int)$this->request->post['quantity']; |
||
284 | } else { |
||
285 | $quantity = $product_info['minimum'] ? $product_info['minimum'] : 1; |
||
286 | } |
||
287 | |||
288 | if (isset($this->request->post['option'])) { |
||
289 | $option = array_filter($this->request->post['option']); |
||
290 | } else { |
||
291 | $option = array(); |
||
292 | } |
||
293 | |||
294 | $product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']); |
||
295 | |||
296 | foreach ($product_options as $product_option) { |
||
297 | if ($product_option['required'] && empty($option[$product_option['product_option_id']])) { |
||
298 | $json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | if (!$json) { |
||
303 | $this->cart->add($this->request->post['product_id'], $quantity, $option); |
||
304 | |||
305 | $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart')); |
||
306 | |||
307 | // Unset all shipping and payment methods |
||
308 | unset($this->session->data['shipping_method']); |
||
309 | unset($this->session->data['shipping_methods']); |
||
310 | unset($this->session->data['payment_method']); |
||
311 | unset($this->session->data['payment_methods']); |
||
312 | |||
313 | // Totals |
||
314 | $this->load->model('extension/extension'); |
||
315 | |||
316 | $totals = array(); |
||
317 | $total = 0; |
||
318 | |||
319 | // Because __call can not keep var references so we put them into an array. |
||
320 | $total_data = array( |
||
321 | 'totals' => &$totals, |
||
322 | 'total' => &$total |
||
323 | ); |
||
324 | |||
325 | // Display prices |
||
326 | if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { |
||
327 | $sort_order = array(); |
||
328 | |||
329 | $results = $this->model_extension_extension->getExtensions('total'); |
||
330 | |||
331 | foreach ($results as $key => $value) { |
||
332 | $sort_order[$key] = $this->config->get($value['code'] . '_sort_order'); |
||
333 | } |
||
334 | |||
335 | array_multisort($sort_order, SORT_ASC, $results); |
||
336 | |||
337 | foreach ($results as $result) { |
||
338 | if ($this->config->get($result['code'] . '_status')) { |
||
339 | $this->load->model('extension/total/' . $result['code']); |
||
340 | |||
341 | // We have to put the totals in an array so that they pass by reference. |
||
342 | $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); |
||
343 | } |
||
344 | } |
||
345 | |||
346 | $sort_order = array(); |
||
347 | |||
348 | foreach ($totals as $key => $value) { |
||
349 | $sort_order[$key] = $value['sort_order']; |
||
350 | } |
||
351 | |||
352 | array_multisort($sort_order, SORT_ASC, $totals); |
||
353 | } |
||
354 | |||
355 | $json['text_items'] = $this->language->get('text_items'); |
||
356 | $json['products_count'] = $this->cart->countProducts(); |
||
357 | $json['products_price'] = $this->currency->format($total, $this->session->data['currency']); |
||
358 | } else { |
||
359 | $json['redirect'] = str_replace('&', '&', $this->url->link('product/product', 'product_id=' . $this->request->post['product_id'])); |
||
360 | } |
||
361 | } |
||
362 | |||
363 | $this->response->addHeader('Content-Type: application/json'); |
||
364 | $this->response->setOutput(json_encode($json)); |
||
365 | } |
||
366 | |||
367 | public function edit() |
||
368 | { |
||
369 | $this->load->language('checkout/cart'); |
||
370 | |||
371 | $json = array(); |
||
372 | |||
373 | // Update |
||
374 | if (!empty($this->request->post['quantity'])) { |
||
375 | foreach ($this->request->post['quantity'] as $key => $value) { |
||
376 | $this->cart->update($key, $value); |
||
377 | } |
||
378 | |||
379 | $this->session->data['success'] = $this->language->get('text_remove'); |
||
380 | |||
381 | unset($this->session->data['shipping_method']); |
||
382 | unset($this->session->data['shipping_methods']); |
||
383 | unset($this->session->data['payment_method']); |
||
384 | unset($this->session->data['payment_methods']); |
||
385 | unset($this->session->data['reward']); |
||
386 | |||
387 | $this->response->redirect($this->url->link('checkout/cart')); |
||
388 | } |
||
389 | |||
390 | $this->response->addHeader('Content-Type: application/json'); |
||
391 | $this->response->setOutput(json_encode($json)); |
||
392 | } |
||
393 | |||
394 | public function remove() |
||
461 | } |
||
462 | } |
||
463 |
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.