Total Complexity | 82 |
Total Lines | 1001 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ModelCheckoutOrder 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 ModelCheckoutOrder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ModelCheckoutOrder extends \Divine\Engine\Core\Model |
||
|
|||
24 | { |
||
25 | public function addOrder($data) |
||
26 | { |
||
27 | $this->db->query(" |
||
28 | INSERT INTO `order` |
||
29 | SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', |
||
30 | store_name = '" . $this->db->escape($data['store_name']) . "', |
||
31 | store_url = '" . $this->db->escape($data['store_url']) . "', |
||
32 | customer_id = '" . (int)$data['customer_id'] . "', |
||
33 | customer_group_id = '" . (int)$data['customer_group_id'] . "', |
||
34 | firstname = '" . $this->db->escape($data['firstname']) . "', |
||
35 | lastname = '" . $this->db->escape($data['lastname']) . "', |
||
36 | email = '" . $this->db->escape($data['email']) . "', |
||
37 | telephone = '" . $this->db->escape($data['telephone']) . "', |
||
38 | fax = '" . $this->db->escape($data['fax']) . "', |
||
39 | custom_field = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "', |
||
40 | payment_firstname = '" . $this->db->escape($data['payment_firstname']) . "', |
||
41 | payment_lastname = '" . $this->db->escape($data['payment_lastname']) . "', |
||
42 | payment_company = '" . $this->db->escape($data['payment_company']) . "', |
||
43 | payment_address_1 = '" . $this->db->escape($data['payment_address_1']) . "', |
||
44 | payment_address_2 = '" . $this->db->escape($data['payment_address_2']) . "', |
||
45 | payment_city = '" . $this->db->escape($data['payment_city']) . "', |
||
46 | payment_postcode = '" . $this->db->escape($data['payment_postcode']) . "', |
||
47 | payment_country = '" . $this->db->escape($data['payment_country']) . "', |
||
48 | payment_country_id = '" . (int)$data['payment_country_id'] . "', |
||
49 | payment_zone = '" . $this->db->escape($data['payment_zone']) . "', |
||
50 | payment_zone_id = '" . (int)$data['payment_zone_id'] . "', |
||
51 | payment_address_format = '" . $this->db->escape($data['payment_address_format']) . "', |
||
52 | payment_custom_field = '" . $this->db->escape(isset($data['payment_custom_field']) ? json_encode($data['payment_custom_field']) : '') . "', |
||
53 | payment_method = '" . $this->db->escape($data['payment_method']) . "', |
||
54 | payment_code = '" . $this->db->escape($data['payment_code']) . "', |
||
55 | shipping_firstname = '" . $this->db->escape($data['shipping_firstname']) . "', |
||
56 | shipping_lastname = '" . $this->db->escape($data['shipping_lastname']) . "', |
||
57 | shipping_company = '" . $this->db->escape($data['shipping_company']) . "', |
||
58 | shipping_address_1 = '" . $this->db->escape($data['shipping_address_1']) . "', |
||
59 | shipping_address_2 = '" . $this->db->escape($data['shipping_address_2']) . "', |
||
60 | shipping_city = '" . $this->db->escape($data['shipping_city']) . "', |
||
61 | shipping_postcode = '" . $this->db->escape($data['shipping_postcode']) . "', |
||
62 | shipping_country = '" . $this->db->escape($data['shipping_country']) . "', |
||
63 | shipping_country_id = '" . (int)$data['shipping_country_id'] . "', |
||
64 | shipping_zone = '" . $this->db->escape($data['shipping_zone']) . "', |
||
65 | shipping_zone_id = '" . (int)$data['shipping_zone_id'] . "', |
||
66 | shipping_address_format = '" . $this->db->escape($data['shipping_address_format']) . "', |
||
67 | shipping_custom_field = '" . $this->db->escape(isset($data['shipping_custom_field']) ? json_encode($data['shipping_custom_field']) : '') . "', |
||
68 | shipping_method = '" . $this->db->escape($data['shipping_method']) . "', |
||
69 | shipping_code = '" . $this->db->escape($data['shipping_code']) . "', |
||
70 | comment = '" . $this->db->escape($data['comment']) . "', |
||
71 | total = '" . (float)$data['total'] . "', |
||
72 | commission = '" . (float)$data['commission'] . "', |
||
73 | marketing_id = '" . (int)$data['marketing_id'] . "', |
||
74 | tracking = '" . $this->db->escape($data['tracking']) . "', |
||
75 | language_id = '" . (int)$data['language_id'] . "', |
||
76 | currency_id = '" . (int)$data['currency_id'] . "', |
||
77 | currency_code = '" . $this->db->escape($data['currency_code']) . "', |
||
78 | currency_value = '" . (float)$data['currency_value'] . "', |
||
79 | ip = '" . $this->db->escape($data['ip']) . "', |
||
80 | forwarded_ip = '" . $this->db->escape($data['forwarded_ip']) . "', |
||
81 | user_agent = '" . $this->db->escape($data['user_agent']) . "', |
||
82 | accept_language = '" . $this->db->escape($data['accept_language']) . "', |
||
83 | date_added = NOW(), |
||
84 | date_modified = NOW() |
||
85 | "); |
||
86 | |||
87 | $order_id = $this->db->getLastId(); |
||
88 | |||
89 | // Products |
||
90 | if (isset($data['products'])) { |
||
91 | foreach ($data['products'] as $product) { |
||
92 | $this->db->query(" |
||
93 | INSERT INTO order_product |
||
94 | SET order_id = '" . (int)$order_id . "', |
||
95 | product_id = '" . (int)$product['product_id'] . "', |
||
96 | name = '" . $this->db->escape($product['name']) . "', |
||
97 | model = '" . $this->db->escape($product['model']) . "', |
||
98 | quantity = '" . (int)$product['quantity'] . "', |
||
99 | price = '" . (float)$product['price'] . "', |
||
100 | total = '" . (float)$product['total'] . "', |
||
101 | reward = '" . (int)$product['reward'] . "' |
||
102 | "); |
||
103 | |||
104 | $order_product_id = $this->db->getLastId(); |
||
105 | |||
106 | foreach ($product['option'] as $option) { |
||
107 | $this->db->query(" |
||
108 | INSERT INTO order_option |
||
109 | SET order_id = '" . (int)$order_id . "', |
||
110 | order_product_id = '" . (int)$order_product_id . "', |
||
111 | product_option_id = '" . (int)$option['product_option_id'] . "', |
||
112 | product_option_value_id = '" . (int)$option['product_option_value_id'] . "', |
||
113 | name = '" . $this->db->escape($option['name']) . "', |
||
114 | `value` = '" . $this->db->escape($option['value']) . "', |
||
115 | `type` = '" . $this->db->escape($option['type']) . "' |
||
116 | "); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | // Totals |
||
122 | if (isset($data['totals'])) { |
||
123 | foreach ($data['totals'] as $total) { |
||
124 | $this->db->query(" |
||
125 | INSERT INTO order_total |
||
126 | SET order_id = '" . (int)$order_id . "', |
||
127 | code = '" . $this->db->escape($total['code']) . "', |
||
128 | title = '" . $this->db->escape($total['title']) . "', |
||
129 | `value` = '" . (float)$total['value'] . "', |
||
130 | sort_order = '" . (int)$total['sort_order'] . "' |
||
131 | "); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | return $order_id; |
||
136 | } |
||
137 | |||
138 | public function editOrder($order_id, $data) |
||
139 | { |
||
140 | // Void the order first |
||
141 | $this->addOrderHistory($order_id, 0); |
||
142 | |||
143 | $this->db->query(" |
||
144 | UPDATE `order` |
||
145 | SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', |
||
146 | store_name = '" . $this->db->escape($data['store_name']) . "', |
||
147 | store_url = '" . $this->db->escape($data['store_url']) . "', |
||
148 | customer_id = '" . (int)$data['customer_id'] . "', |
||
149 | customer_group_id = '" . (int)$data['customer_group_id'] . "', |
||
150 | firstname = '" . $this->db->escape($data['firstname']) . "', |
||
151 | lastname = '" . $this->db->escape($data['lastname']) . "', |
||
152 | email = '" . $this->db->escape($data['email']) . "', |
||
153 | telephone = '" . $this->db->escape($data['telephone']) . "', |
||
154 | fax = '" . $this->db->escape($data['fax']) . "', |
||
155 | custom_field = '" . $this->db->escape(json_encode($data['custom_field'])) . "', |
||
156 | payment_firstname = '" . $this->db->escape($data['payment_firstname']) . "', |
||
157 | payment_lastname = '" . $this->db->escape($data['payment_lastname']) . "', |
||
158 | payment_company = '" . $this->db->escape($data['payment_company']) . "', |
||
159 | payment_address_1 = '" . $this->db->escape($data['payment_address_1']) . "', |
||
160 | payment_address_2 = '" . $this->db->escape($data['payment_address_2']) . "', |
||
161 | payment_city = '" . $this->db->escape($data['payment_city']) . "', |
||
162 | payment_postcode = '" . $this->db->escape($data['payment_postcode']) . "', |
||
163 | payment_country = '" . $this->db->escape($data['payment_country']) . "', |
||
164 | payment_country_id = '" . (int)$data['payment_country_id'] . "', |
||
165 | payment_zone = '" . $this->db->escape($data['payment_zone']) . "', |
||
166 | payment_zone_id = '" . (int)$data['payment_zone_id'] . "', |
||
167 | payment_address_format = '" . $this->db->escape($data['payment_address_format']) . "', |
||
168 | payment_custom_field = '" . $this->db->escape(json_encode($data['payment_custom_field'])) . "', |
||
169 | payment_method = '" . $this->db->escape($data['payment_method']) . "', |
||
170 | payment_code = '" . $this->db->escape($data['payment_code']) . "', |
||
171 | shipping_firstname = '" . $this->db->escape($data['shipping_firstname']) . "', |
||
172 | shipping_lastname = '" . $this->db->escape($data['shipping_lastname']) . "', |
||
173 | shipping_company = '" . $this->db->escape($data['shipping_company']) . "', |
||
174 | shipping_address_1 = '" . $this->db->escape($data['shipping_address_1']) . "', |
||
175 | shipping_address_2 = '" . $this->db->escape($data['shipping_address_2']) . "', |
||
176 | shipping_city = '" . $this->db->escape($data['shipping_city']) . "', |
||
177 | shipping_postcode = '" . $this->db->escape($data['shipping_postcode']) . "', |
||
178 | shipping_country = '" . $this->db->escape($data['shipping_country']) . "', |
||
179 | shipping_country_id = '" . (int)$data['shipping_country_id'] . "', |
||
180 | shipping_zone = '" . $this->db->escape($data['shipping_zone']) . "', |
||
181 | shipping_zone_id = '" . (int)$data['shipping_zone_id'] . "', |
||
182 | shipping_address_format = '" . $this->db->escape($data['shipping_address_format']) . "', |
||
183 | shipping_custom_field = '" . $this->db->escape(json_encode($data['shipping_custom_field'])) . "', |
||
184 | shipping_method = '" . $this->db->escape($data['shipping_method']) . "', |
||
185 | shipping_code = '" . $this->db->escape($data['shipping_code']) . "', |
||
186 | comment = '" . $this->db->escape($data['comment']) . "', |
||
187 | total = '" . (float)$data['total'] . "', |
||
188 | commission = '" . (float)$data['commission'] . "', |
||
189 | date_modified = NOW() |
||
190 | WHERE order_id = '" . (int)$order_id . "' |
||
191 | "); |
||
192 | |||
193 | $this->db->query(" |
||
194 | DELETE |
||
195 | FROM order_product |
||
196 | WHERE order_id = '" . (int)$order_id . "' |
||
197 | "); |
||
198 | |||
199 | $this->db->query(" |
||
200 | DELETE |
||
201 | FROM order_option |
||
202 | WHERE order_id = '" . (int)$order_id . "' |
||
203 | "); |
||
204 | |||
205 | // Products |
||
206 | if (isset($data['products'])) { |
||
207 | foreach ($data['products'] as $product) { |
||
208 | $this->db->query(" |
||
209 | INSERT INTO order_product |
||
210 | SET order_id = '" . (int)$order_id . "', |
||
211 | product_id = '" . (int)$product['product_id'] . "', |
||
212 | name = '" . $this->db->escape($product['name']) . "', |
||
213 | model = '" . $this->db->escape($product['model']) . "', |
||
214 | quantity = '" . (int)$product['quantity'] . "', |
||
215 | price = '" . (float)$product['price'] . "', |
||
216 | total = '" . (float)$product['total'] . "', |
||
217 | reward = '" . (int)$product['reward'] . "' |
||
218 | "); |
||
219 | |||
220 | $order_product_id = $this->db->getLastId(); |
||
221 | |||
222 | foreach ($product['option'] as $option) { |
||
223 | $this->db->query(" |
||
224 | INSERT INTO order_option |
||
225 | SET order_id = '" . (int)$order_id . "', |
||
226 | order_product_id = '" . (int)$order_product_id . "', |
||
227 | product_option_id = '" . (int)$option['product_option_id'] . "', |
||
228 | product_option_value_id = '" . (int)$option['product_option_value_id'] . "', |
||
229 | name = '" . $this->db->escape($option['name']) . "', |
||
230 | `value` = '" . $this->db->escape($option['value']) . "', |
||
231 | `type` = '" . $this->db->escape($option['type']) . "' |
||
232 | "); |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | // Totals |
||
238 | $this->db->query(" |
||
239 | DELETE |
||
240 | FROM order_total |
||
241 | WHERE order_id = '" . (int)$order_id . "' |
||
242 | "); |
||
243 | |||
244 | if (isset($data['totals'])) { |
||
245 | foreach ($data['totals'] as $total) { |
||
246 | $this->db->query(" |
||
247 | INSERT INTO order_total |
||
248 | SET order_id = '" . (int)$order_id . "', |
||
249 | code = '" . $this->db->escape($total['code']) . "', |
||
250 | title = '" . $this->db->escape($total['title']) . "', |
||
251 | `value` = '" . (float)$total['value'] . "', |
||
252 | sort_order = '" . (int)$total['sort_order'] . "' |
||
253 | "); |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | public function deleteOrder($order_id) |
||
291 | "); |
||
292 | } |
||
293 | |||
294 | public function getOrder($order_id) |
||
295 | { |
||
296 | $order_query = $this->db->query(" |
||
297 | SELECT *, |
||
298 | ( |
||
299 | SELECT os.name |
||
300 | FROM `order_status` os |
||
301 | WHERE os.order_status_id = o.order_status_id |
||
302 | AND os.language_id = o.language_id) AS order_status |
||
303 | FROM `order` o |
||
304 | WHERE o.order_id = '" . (int)$order_id . "' |
||
305 | "); |
||
306 | |||
307 | if ($order_query->num_rows) { |
||
308 | $country_query = $this->db->query(" |
||
309 | SELECT * |
||
310 | FROM `country` |
||
311 | WHERE country_id = '" . (int)$order_query->row['payment_country_id'] . "' |
||
312 | "); |
||
313 | |||
314 | if ($country_query->num_rows) { |
||
315 | $payment_iso_code_2 = $country_query->row['iso_code_2']; |
||
316 | $payment_iso_code_3 = $country_query->row['iso_code_3']; |
||
317 | } else { |
||
318 | $payment_iso_code_2 = ''; |
||
319 | $payment_iso_code_3 = ''; |
||
320 | } |
||
321 | |||
322 | $zone_query = $this->db->query(" |
||
323 | SELECT * |
||
324 | FROM `zone` |
||
325 | WHERE zone_id = '" . (int)$order_query->row['payment_zone_id'] . "' |
||
326 | "); |
||
327 | |||
328 | if ($zone_query->num_rows) { |
||
329 | $payment_zone_code = $zone_query->row['code']; |
||
330 | } else { |
||
331 | $payment_zone_code = ''; |
||
332 | } |
||
333 | |||
334 | $country_query = $this->db->query(" |
||
335 | SELECT * |
||
336 | FROM `country` |
||
337 | WHERE country_id = '" . (int)$order_query->row['shipping_country_id'] . "' |
||
338 | "); |
||
339 | |||
340 | if ($country_query->num_rows) { |
||
341 | $shipping_iso_code_2 = $country_query->row['iso_code_2']; |
||
342 | $shipping_iso_code_3 = $country_query->row['iso_code_3']; |
||
343 | } else { |
||
344 | $shipping_iso_code_2 = ''; |
||
345 | $shipping_iso_code_3 = ''; |
||
346 | } |
||
347 | |||
348 | $zone_query = $this->db->query(" |
||
349 | SELECT * FROM `zone` |
||
350 | WHERE zone_id = '" . (int)$order_query->row['shipping_zone_id'] . "' |
||
351 | "); |
||
352 | |||
353 | if ($zone_query->num_rows) { |
||
354 | $shipping_zone_code = $zone_query->row['code']; |
||
355 | } else { |
||
356 | $shipping_zone_code = ''; |
||
357 | } |
||
358 | |||
359 | $this->load->model('localisation/language'); |
||
360 | |||
361 | $language_info = $this->model_localisation_language->getLanguage($order_query->row['language_id']); |
||
362 | |||
363 | if ($language_info) { |
||
364 | $language_code = $language_info['code']; |
||
365 | } else { |
||
366 | $language_code = $this->config->get('config_language'); |
||
367 | } |
||
368 | |||
369 | return array( |
||
370 | 'order_id' => $order_query->row['order_id'], |
||
371 | 'invoice_no' => $order_query->row['invoice_no'], |
||
372 | 'invoice_prefix' => $order_query->row['invoice_prefix'], |
||
373 | 'store_name' => $order_query->row['store_name'], |
||
374 | 'store_url' => $order_query->row['store_url'], |
||
375 | 'customer_id' => $order_query->row['customer_id'], |
||
376 | 'firstname' => $order_query->row['firstname'], |
||
377 | 'lastname' => $order_query->row['lastname'], |
||
378 | 'email' => $order_query->row['email'], |
||
379 | 'telephone' => $order_query->row['telephone'], |
||
380 | 'fax' => $order_query->row['fax'], |
||
381 | 'custom_field' => json_decode($order_query->row['custom_field'], true), |
||
382 | 'payment_firstname' => $order_query->row['payment_firstname'], |
||
383 | 'payment_lastname' => $order_query->row['payment_lastname'], |
||
384 | 'payment_company' => $order_query->row['payment_company'], |
||
385 | 'payment_address_1' => $order_query->row['payment_address_1'], |
||
386 | 'payment_address_2' => $order_query->row['payment_address_2'], |
||
387 | 'payment_postcode' => $order_query->row['payment_postcode'], |
||
388 | 'payment_city' => $order_query->row['payment_city'], |
||
389 | 'payment_zone_id' => $order_query->row['payment_zone_id'], |
||
390 | 'payment_zone' => $order_query->row['payment_zone'], |
||
391 | 'payment_zone_code' => $payment_zone_code, |
||
392 | 'payment_country_id' => $order_query->row['payment_country_id'], |
||
393 | 'payment_country' => $order_query->row['payment_country'], |
||
394 | 'payment_iso_code_2' => $payment_iso_code_2, |
||
395 | 'payment_iso_code_3' => $payment_iso_code_3, |
||
396 | 'payment_address_format' => $order_query->row['payment_address_format'], |
||
397 | 'payment_custom_field' => json_decode($order_query->row['payment_custom_field'], true), |
||
398 | 'payment_method' => $order_query->row['payment_method'], |
||
399 | 'payment_code' => $order_query->row['payment_code'], |
||
400 | 'shipping_firstname' => $order_query->row['shipping_firstname'], |
||
401 | 'shipping_lastname' => $order_query->row['shipping_lastname'], |
||
402 | 'shipping_company' => $order_query->row['shipping_company'], |
||
403 | 'shipping_address_1' => $order_query->row['shipping_address_1'], |
||
404 | 'shipping_address_2' => $order_query->row['shipping_address_2'], |
||
405 | 'shipping_postcode' => $order_query->row['shipping_postcode'], |
||
406 | 'shipping_city' => $order_query->row['shipping_city'], |
||
407 | 'shipping_zone_id' => $order_query->row['shipping_zone_id'], |
||
408 | 'shipping_zone' => $order_query->row['shipping_zone'], |
||
409 | 'shipping_zone_code' => $shipping_zone_code, |
||
410 | 'shipping_country_id' => $order_query->row['shipping_country_id'], |
||
411 | 'shipping_country' => $order_query->row['shipping_country'], |
||
412 | 'shipping_iso_code_2' => $shipping_iso_code_2, |
||
413 | 'shipping_iso_code_3' => $shipping_iso_code_3, |
||
414 | 'shipping_address_format' => $order_query->row['shipping_address_format'], |
||
415 | 'shipping_custom_field' => json_decode($order_query->row['shipping_custom_field'], true), |
||
416 | 'shipping_method' => $order_query->row['shipping_method'], |
||
417 | 'shipping_code' => $order_query->row['shipping_code'], |
||
418 | 'comment' => $order_query->row['comment'], |
||
419 | 'total' => $order_query->row['total'], |
||
420 | 'order_status_id' => $order_query->row['order_status_id'], |
||
421 | 'order_status' => $order_query->row['order_status'], |
||
422 | 'commission' => $order_query->row['commission'], |
||
423 | 'language_id' => $order_query->row['language_id'], |
||
424 | 'language_code' => $language_code, |
||
425 | 'currency_id' => $order_query->row['currency_id'], |
||
426 | 'currency_code' => $order_query->row['currency_code'], |
||
427 | 'currency_value' => $order_query->row['currency_value'], |
||
428 | 'ip' => $order_query->row['ip'], |
||
429 | 'forwarded_ip' => $order_query->row['forwarded_ip'], |
||
430 | 'user_agent' => $order_query->row['user_agent'], |
||
431 | 'accept_language' => $order_query->row['accept_language'], |
||
432 | 'date_added' => $order_query->row['date_added'], |
||
433 | 'date_modified' => $order_query->row['date_modified'] |
||
434 | ); |
||
435 | } else { |
||
436 | return false; |
||
437 | } |
||
438 | } |
||
439 | |||
440 | public function addOrderHistory($order_id, $order_status_id, $comment = '', $notify = false, $override = false) |
||
1024 | } |
||
1025 | } |
||
1026 | } |
||
1028 |
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.