Total Complexity | 65 |
Total Lines | 488 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like OrderExtension 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 OrderExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class OrderExtension extends Extension |
||
22 | { |
||
23 | /** |
||
24 | * Record when an order is sent to Unleashed |
||
25 | * @config |
||
26 | */ |
||
27 | private static array $db = [ |
||
28 | 'OrderSentToUnleashed' => 'Datetime' |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * Apply Guid if absent |
||
33 | */ |
||
34 | public function onBeforeWrite(): void |
||
38 | } |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Return the Address Name |
||
43 | */ |
||
44 | public function getAddressName(array|Address $address): string |
||
45 | { |
||
46 | if (is_array($address)) { |
||
47 | $address_name = $address['StreetAddress']; |
||
48 | if ($address['StreetAddress2']) { |
||
49 | $address_name .= ' ' . $address['StreetAddress2']; |
||
50 | } |
||
51 | |||
52 | $address_name .= ' ' . $address['City']; |
||
53 | } else { |
||
54 | $address_name = $address->Address; |
||
55 | if ($address->AddressLine2) { |
||
56 | $address_name .= ' ' . $address->AddressLine2; |
||
57 | } |
||
58 | |||
59 | $address_name .= ' ' . $address->City; |
||
60 | } |
||
61 | |||
62 | return $address_name; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Match the order's shipping address to items returned from Unleashed |
||
67 | */ |
||
68 | public function matchCustomerAddress(array $items, array|Address $shipping_address): bool |
||
69 | { |
||
70 | // Obtain the delivery address |
||
71 | $address = $items[0]['Addresses'][0]; |
||
72 | if ($address['AddressType'] != "Physical" && isset($items[0]['Addresses'][1])) { |
||
73 | $address = $items[0]['Addresses'][1]; |
||
74 | } |
||
75 | |||
76 | return strtoupper($this->getAddressName($shipping_address)) === strtoupper($this->getAddressName($address)); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * add the address components to the body array |
||
81 | * $type is either Postal or Physical |
||
82 | */ |
||
83 | public function setBodyAddress(array $body, Order $order, string $type): array |
||
84 | { |
||
85 | $countries = (array) ShopConfigExtension::config()->get('iso_3166_country_codes'); |
||
86 | |||
87 | if ($type === 'Postal') { |
||
88 | $address = $order->BillingAddress(); |
||
89 | $body['Addresses'][] = [ |
||
90 | 'AddressName' => $this->getAddressName($address), |
||
91 | 'AddressType' => $type, |
||
92 | 'City' => $address->City, |
||
93 | 'Country' => $countries[$address->Country], |
||
94 | 'PostalCode' => $address->PostalCode, |
||
95 | 'Region' => $address->State, |
||
96 | 'StreetAddress' => $address->Address, |
||
97 | 'StreetAddress2' => $address->AddressLine2 |
||
98 | ]; |
||
99 | } |
||
100 | |||
101 | if ($type === 'Physical') { |
||
102 | $address = $order->ShippingAddress(); |
||
103 | $body['DeliveryCity'] = $address->City; |
||
104 | $body['DeliveryCountry'] = $countries[$address->Country]; |
||
105 | $body['DeliveryPostCode'] = $address->PostalCode; |
||
106 | $body['DeliveryRegion'] = $address->State; |
||
107 | $body['DeliveryStreetAddress'] = $address->Address; |
||
108 | $body['DeliveryStreetAddress2'] = $address->AddressLine2; |
||
109 | |||
110 | $body['Addresses'][] = [ |
||
111 | 'AddressName' => $this->getAddressName($address), |
||
112 | 'AddressType' => 'Physical', |
||
113 | 'City' => $address->City, |
||
114 | 'Country' => $countries[$address->Country], |
||
115 | 'PostalCode' => $address->PostalCode, |
||
116 | 'Region' => $address->State, |
||
117 | 'StreetAddress' => $address->Address, |
||
118 | 'StreetAddress2' => $address->AddressLine2 |
||
119 | ]; |
||
120 | |||
121 | $body['Addresses'][] = [ |
||
122 | 'AddressName' => $this->getAddressName($address), |
||
123 | 'AddressType' => 'Shipping', |
||
124 | 'City' => $address->City, |
||
125 | 'Country' => $countries[$address->Country], |
||
126 | 'PostalCode' => $address->PostalCode, |
||
127 | 'Region' => $address->State, |
||
128 | 'StreetAddress' => $address->Address, |
||
129 | 'StreetAddress2' => $address->AddressLine2 |
||
130 | ]; |
||
131 | } |
||
132 | |||
133 | return $body; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Add the currency code to the body array |
||
138 | */ |
||
139 | public function setBodyCurrencyCode(array $body, Order $order): array |
||
140 | { |
||
141 | $body['Currency']['CurrencyCode'] = $order->Currency(); |
||
142 | return $body; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Add the Customer Code/Name (use Company field of BillingAddress to allow for B2B eCommerce sites) |
||
147 | */ |
||
148 | public function setBodyCustomerCodeAndName(array $body, Order $order): array |
||
149 | { |
||
150 | $address = $order->BillingAddress(); |
||
151 | if ($address->Company) { |
||
152 | // use Organisation name |
||
153 | $body['CustomerCode'] = $address->Company; |
||
154 | $body['CustomerName'] = $address->Company; |
||
155 | } else { |
||
156 | // use Contact full name instead |
||
157 | $body['CustomerCode'] = $order->getName(); |
||
158 | $body['CustomerName'] = $order->getName(); |
||
159 | } |
||
160 | |||
161 | return $body; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Set Delivery Method and Delivery Name |
||
166 | * Allow for the SilverShop Shipping module |
||
167 | */ |
||
168 | public function setBodyDeliveryMethodAndDeliveryName(array $body, Order $order, string $shipping_modifier_class_name): array |
||
169 | { |
||
170 | $shipping_modifier = $order->getModifier($shipping_modifier_class_name); |
||
171 | if (!empty($shipping_modifier)) { |
||
172 | $body['DeliveryMethod'] = $shipping_modifier::config()->product_code; |
||
173 | $body['DeliveryName'] = $shipping_modifier::config()->product_code; |
||
174 | } |
||
175 | |||
176 | return $body; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Set Sales Order Lines |
||
181 | */ |
||
182 | public function setBodySalesOrderLines(array $body, Order $order, string $tax_modifier_class_name, int $rounding_precision): array |
||
183 | { |
||
184 | $line_number = 0; |
||
185 | |||
186 | // Sales Order Lines |
||
187 | foreach ($order->Items()->getIterator() as $item) { |
||
188 | // Definitions |
||
189 | $product = $item->Product(); |
||
190 | $line_number += 1; |
||
191 | $sales_order_line = [ |
||
192 | 'DiscountRate' => 0, |
||
193 | 'Guid' => $item->Guid, |
||
194 | 'LineNumber' => $line_number, |
||
195 | 'LineType' => null, |
||
196 | 'LineTotal' => round(floatval($item->Total()), $rounding_precision), |
||
197 | 'OrderQuantity' => $item->Quantity, |
||
198 | 'Product' => [ |
||
199 | 'Guid' => $product->Guid |
||
200 | ], |
||
201 | 'UnitPrice' => round(floatval($product->getPrice()), $rounding_precision) |
||
202 | ]; |
||
203 | if ($tax_modifier_class_name !== '' && $tax_modifier_class_name !== '0') { |
||
204 | $tax_calculator = new $tax_modifier_class_name; |
||
205 | $sales_order_line['LineTax'] = round( |
||
206 | $tax_calculator->value($item->Total()), |
||
207 | $rounding_precision |
||
208 | ); |
||
209 | $sales_order_line['LineTaxCode'] = $body['Tax']['TaxCode']; |
||
210 | } |
||
211 | |||
212 | $body['SalesOrderLines'][] = $sales_order_line; |
||
213 | } |
||
214 | |||
215 | // Add Modifiers that have a product_code |
||
216 | foreach ($order->Modifiers()->sort('Sort')->getIterator() as $modifier) { |
||
217 | $line_total = round(floatval($modifier->Amount), $rounding_precision); |
||
218 | |||
219 | if ($modifier::config()->get('product_code') && |
||
220 | $modifier->Type !== 'Ignored' && |
||
221 | !empty($line_total) |
||
222 | ) { |
||
223 | $line_number += 1; |
||
224 | $sales_order_line = [ |
||
225 | 'DiscountRate' => 0, |
||
226 | 'Guid' => $modifier->Guid, |
||
227 | 'LineNumber' => $line_number, |
||
228 | 'LineTotal' => $line_total, |
||
229 | 'LineType' => null, |
||
230 | 'OrderQuantity' => 1, |
||
231 | 'Product' => [ |
||
232 | 'ProductCode' => $modifier::config()->get('product_code'), |
||
233 | ], |
||
234 | 'UnitPrice' => round(floatval($modifier->Amount), $rounding_precision) |
||
235 | ]; |
||
236 | if ($tax_modifier_class_name !== '' && $tax_modifier_class_name !== '0') { |
||
237 | $tax_calculator = new $tax_modifier_class_name; |
||
238 | $sales_order_line['LineTax'] = round( |
||
239 | $tax_calculator->value($modifier->Amount), |
||
240 | $rounding_precision |
||
241 | ); |
||
242 | $sales_order_line['LineTaxCode'] = $body['Tax']['TaxCode']; |
||
243 | } |
||
244 | |||
245 | $body['SalesOrderLines'][] = $sales_order_line; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | return $body; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Set the Tax Codes |
||
254 | */ |
||
255 | public function setBodyTaxCode(array $body, Order $order, string $tax_modifier_class_name): array |
||
266 | } |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Calculate the SubTotal and TaxTotal |
||
271 | */ |
||
272 | public function setBodySubTotalAndTax(array $body, Order $order, string $tax_modifier_class_name, int $rounding_precision): array |
||
273 | { |
||
274 | if ($tax_modifier_class_name !== '' && $tax_modifier_class_name !== '0') { |
||
275 | $tax_modifier = $order->getModifier($tax_modifier_class_name); |
||
276 | |||
277 | // Calculate the Tax and Sub Total, which excludes Tax |
||
278 | if (!empty($tax_modifier)) { |
||
279 | $sub_total = 0; |
||
280 | $tax_total = 0; |
||
281 | foreach ($body['SalesOrderLines'] as $item) { |
||
282 | $sub_total = bcadd( |
||
283 | (string) $sub_total, |
||
284 | (string) $item['LineTotal'], |
||
285 | $rounding_precision |
||
286 | ); |
||
287 | $tax_total = bcadd( |
||
288 | (string) $tax_total, |
||
289 | (string) $item['LineTax'], |
||
290 | $rounding_precision |
||
291 | ); |
||
292 | } |
||
293 | |||
294 | $body['TaxTotal'] = $tax_total; |
||
295 | $body['SubTotal'] = $sub_total; |
||
296 | |||
297 | $rounding = round(floatval($order->Total() - $tax_total - $sub_total), $rounding_precision); |
||
298 | // if there is some rounding, adjust the Tax on the first sales order line |
||
299 | // and adjust the Tax Total by the same amount |
||
300 | if (!empty($rounding)) { |
||
301 | $body['SalesOrderLines'][0]['LineTax'] = round($body['SalesOrderLines'][0]['LineTax'] + $rounding, $rounding_precision); |
||
302 | $body['TaxTotal'] = round($body['TaxTotal'] + $rounding, $rounding_precision); |
||
303 | } |
||
304 | } |
||
305 | } else { |
||
306 | $body['SubTotal'] = round(floatval($order->Total()), $rounding_precision); |
||
307 | } |
||
308 | |||
309 | return $body; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Send a sales order to Unleashed upon paid status |
||
314 | * May need to create the Customer first |
||
315 | */ |
||
316 | public function onAfterWrite(): void |
||
509 | } |
||
510 | } |
||
511 | } |
||
512 | } |
||
513 | } |
||
514 |
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