Total Complexity | 64 |
Total Lines | 475 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Order 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 Order, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Order extends DataExtension |
||
17 | { |
||
18 | /** |
||
19 | * Record when an order is sent to Unleashed |
||
20 | * @config |
||
21 | */ |
||
22 | private static array $db = [ |
||
23 | 'OrderSentToUnleashed' => 'Datetime' |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * Apply Guid if absent |
||
28 | */ |
||
29 | public function onBeforeWrite(): void |
||
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Return the Address Name |
||
39 | */ |
||
40 | public function getAddressName(array|Address $address): string |
||
41 | { |
||
42 | if (is_array($address)) { |
||
43 | $address_name = $address['StreetAddress']; |
||
44 | if ($address['StreetAddress2']) { |
||
45 | $address_name .= ' ' . $address['StreetAddress2']; |
||
46 | } |
||
47 | $address_name .= ' ' . $address['City']; |
||
48 | } else { |
||
49 | $address_name = $address->Address; |
||
50 | if ($address->AddressLine2) { |
||
51 | $address_name .= ' ' . $address->AddressLine2; |
||
52 | } |
||
53 | $address_name .= ' ' . $address->City; |
||
54 | } |
||
55 | return $address_name; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Match the order's shipping address to items returned from Unleashed |
||
60 | */ |
||
61 | public function matchCustomerAddress(array $items, array|Address $shipping_address): bool |
||
62 | { |
||
63 | // Obtain the delivery address |
||
64 | $address = $items[0]['Addresses'][0]; |
||
65 | if ($address['AddressType'] != "Physical" && isset($items[0]['Addresses'][1])) { |
||
66 | $address = $items[0]['Addresses'][1]; |
||
67 | } |
||
68 | return strtoupper($this->getAddressName($shipping_address)) === strtoupper($this->getAddressName($address)); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * add the address components to the body array |
||
73 | * $type is either Postal or Physical |
||
74 | */ |
||
75 | public function setBodyAddress(array $body, DataObject $order, string $type): array |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Add the currency code to the body array |
||
130 | */ |
||
131 | public function setBodyCurrencyCode(array $body, DataObject $order): array |
||
132 | { |
||
133 | $body['Currency']['CurrencyCode'] = $order->Currency(); |
||
134 | return $body; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Add the Customer Code/Name (use Company field of BillingAddress to allow for B2B eCommerce sites) |
||
139 | */ |
||
140 | public function setBodyCustomerCodeAndName(array $body, DataObject $order): array |
||
141 | { |
||
142 | $billing_address = $order->BillingAddress(); |
||
143 | if ($billing_address->Company) { |
||
144 | // use Organisation name |
||
145 | $body['CustomerCode'] = $billing_address->Company; |
||
146 | $body['CustomerName'] = $billing_address->Company; |
||
147 | } else { |
||
148 | // use Contact full name instead |
||
149 | $body['CustomerCode'] = $order->getName(); |
||
150 | $body['CustomerName'] = $order->getName(); |
||
151 | } |
||
152 | return $body; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Set Delivery Method and Delivery Name |
||
157 | * Allow for the SilverShop Shipping module |
||
158 | */ |
||
159 | public function setBodyDeliveryMethodAndDeliveryName(array $body, DataObject $order, string $shipping_modifier_class_name): array |
||
160 | { |
||
161 | $shipping_modifier = $order->getModifier($shipping_modifier_class_name); |
||
162 | if (!empty($shipping_modifier)) { |
||
163 | $body['DeliveryMethod'] = $shipping_modifier::config()->product_code; |
||
164 | $body['DeliveryName'] = $shipping_modifier::config()->product_code; |
||
165 | } |
||
166 | return $body; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Set Sales Order Lines |
||
171 | */ |
||
172 | public function setBodySalesOrderLines(array $body, DataObject $order, string $tax_modifier_class_name, int $rounding_precision): array |
||
173 | { |
||
174 | $line_number = 0; |
||
175 | |||
176 | // Sales Order Lines |
||
177 | foreach ($order->Items()->getIterator() as $item) { |
||
178 | // Definitions |
||
179 | $product = $item->Product(); |
||
180 | $line_number += 1; |
||
181 | $sales_order_line = [ |
||
182 | 'DiscountRate' => 0, |
||
183 | 'Guid' => $item->Guid, |
||
184 | 'LineNumber' => $line_number, |
||
185 | 'LineType' => null, |
||
186 | 'LineTotal' => round(floatval($item->Total()), $rounding_precision), |
||
187 | 'OrderQuantity' => $item->Quantity, |
||
188 | 'Product' => [ |
||
189 | 'Guid' => $product->Guid |
||
190 | ], |
||
191 | 'UnitPrice' => round(floatval($product->getPrice()), $rounding_precision) |
||
192 | ]; |
||
193 | if ($tax_modifier_class_name !== '' && $tax_modifier_class_name !== '0') { |
||
194 | $tax_calculator = new $tax_modifier_class_name; |
||
195 | $sales_order_line['LineTax'] = round( |
||
196 | $tax_calculator->value($item->Total()), |
||
197 | $rounding_precision |
||
198 | ); |
||
199 | $sales_order_line['LineTaxCode'] = $body['Tax']['TaxCode']; |
||
200 | } |
||
201 | $body['SalesOrderLines'][] = $sales_order_line; |
||
202 | } |
||
203 | |||
204 | // Add Modifiers that have a product_code |
||
205 | foreach ($order->Modifiers()->sort('Sort')->getIterator() as $modifier) { |
||
206 | $line_total = round(floatval($modifier->Amount), $rounding_precision); |
||
207 | |||
208 | if ($modifier::config()->product_code && |
||
209 | $modifier->Type !== 'Ignored' && |
||
210 | !empty($line_total) |
||
211 | ) { |
||
212 | $line_number += 1; |
||
213 | $sales_order_line = [ |
||
214 | 'DiscountRate' => 0, |
||
215 | 'Guid' => $modifier->Guid, |
||
216 | 'LineNumber' => $line_number, |
||
217 | 'LineTotal' => $line_total, |
||
218 | 'LineType' => null, |
||
219 | 'OrderQuantity' => 1, |
||
220 | 'Product' => [ |
||
221 | 'ProductCode' => $modifier::config()->product_code, |
||
222 | ], |
||
223 | 'UnitPrice' => round(floatval($modifier->Amount), $rounding_precision) |
||
224 | ]; |
||
225 | if ($tax_modifier_class_name !== '' && $tax_modifier_class_name !== '0') { |
||
226 | $tax_calculator = new $tax_modifier_class_name; |
||
227 | $sales_order_line['LineTax'] = round( |
||
228 | $tax_calculator->value($modifier->Amount), |
||
229 | $rounding_precision |
||
230 | ); |
||
231 | $sales_order_line['LineTaxCode'] = $body['Tax']['TaxCode']; |
||
232 | } |
||
233 | $body['SalesOrderLines'][] = $sales_order_line; |
||
234 | } |
||
235 | } |
||
236 | return $body; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Set the Tax Codes |
||
241 | */ |
||
242 | public function setBodyTaxCode(array $body, DataObject $order, string $tax_modifier_class_name): array |
||
252 | } |
||
253 | |||
254 | |||
255 | /** |
||
256 | * Calculate the SubTotal and TaxTotal |
||
257 | */ |
||
258 | public function setBodySubTotalAndTax(array $body, DataObject $order, string $tax_modifier_class_name, int $rounding_precision): array |
||
259 | { |
||
260 | if ($tax_modifier_class_name !== '' && $tax_modifier_class_name !== '0') { |
||
261 | $tax_modifier = $order->getModifier($tax_modifier_class_name); |
||
262 | |||
263 | // Calculate the Tax and Sub Total, which excludes Tax |
||
264 | if (!empty($tax_modifier)) { |
||
265 | $sub_total = 0; |
||
266 | $tax_total = 0; |
||
267 | foreach ($body['SalesOrderLines'] as $item) { |
||
268 | $sub_total = bcadd( |
||
269 | (string) $sub_total, |
||
270 | (string) $item['LineTotal'], |
||
271 | $rounding_precision |
||
272 | ); |
||
273 | $tax_total = bcadd( |
||
274 | (string) $tax_total, |
||
275 | (string) $item['LineTax'], |
||
276 | $rounding_precision |
||
277 | ); |
||
278 | } |
||
279 | $body['TaxTotal'] = $tax_total; |
||
280 | $body['SubTotal'] = $sub_total; |
||
281 | |||
282 | $rounding = round(floatval($order->Total() - $tax_total - $sub_total), $rounding_precision); |
||
283 | // if there is some rounding, adjust the Tax on the first sales order line |
||
284 | // and adjust the Tax Total by the same amount |
||
285 | if (!empty($rounding)) { |
||
286 | $body['SalesOrderLines'][0]['LineTax'] = round($body['SalesOrderLines'][0]['LineTax'] + $rounding, $rounding_precision); |
||
287 | $body['TaxTotal'] = round($body['TaxTotal'] + $rounding, $rounding_precision); |
||
288 | } |
||
289 | } |
||
290 | } else { |
||
291 | $body['SubTotal'] = round(floatval($order->Total()), $rounding_precision); |
||
292 | } |
||
293 | return $body; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Send a sales order to Unleashed upon paid status |
||
298 | * May need to create the Customer first |
||
299 | */ |
||
300 | public function onAfterWrite(): void |
||
491 | } |
||
492 | } |
||
493 | } |
||
494 | } |
||
495 | } |
||
496 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths