Total Complexity | 58 |
Total Lines | 512 |
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 |
||
14 | class Order extends DataExtension |
||
15 | { |
||
16 | /** |
||
17 | * Record when an order is sent to Unleashed |
||
18 | */ |
||
19 | private static $db = [ |
||
|
|||
20 | 'OrderSentToUnleashed' => 'Datetime' |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * Apply Guid if absent |
||
25 | */ |
||
26 | public function onBeforeWrite() |
||
27 | { |
||
28 | parent::onBeforeWrite(); |
||
29 | if (!$this->owner->getField("Guid")) { |
||
30 | $this->owner->Guid = (string) Utils::createGuid(); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Return the Address Name |
||
36 | * @return string |
||
37 | */ |
||
38 | public function getAddressName($address) |
||
39 | { |
||
40 | if (is_array($address)) { |
||
41 | $address_name = $address['StreetAddress']; |
||
42 | if ($address['StreetAddress2']) { |
||
43 | $address_name .= ' ' . $address['StreetAddress2']; |
||
44 | } |
||
45 | $address_name .= ' ' . $address['City']; |
||
46 | } else { |
||
47 | $address_name = $address->Address; |
||
48 | if ($address->AddressLine2) { |
||
49 | $address_name .= ' ' . $address->AddressLine2; |
||
50 | } |
||
51 | $address_name .= ' ' . $address->City; |
||
52 | } |
||
53 | return $address_name; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Match the order's shipping address to items returned from Unleashed |
||
58 | * @return boolean |
||
59 | */ |
||
60 | public function matchCustomerAddress($items, $shipping_address) |
||
61 | { |
||
62 | // Obtain the delivery address |
||
63 | $address = $items[0]['Addresses'][0]; |
||
64 | if ($address['AddressType'] != "Physical") { |
||
65 | if (isset($items[0]['Addresses'][1])) { |
||
66 | $address = $items[0]['Addresses'][1]; |
||
67 | } |
||
68 | } |
||
69 | return strtoupper($this->getAddressName($shipping_address)) == strtoupper($this->getAddressName($address)); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * add the address components to the body array |
||
74 | * @param array $body |
||
75 | * @param object $order |
||
76 | * @param string $type (either Postal or Physical) |
||
77 | * @return array $body |
||
78 | */ |
||
79 | public function setBodyAddress($body, $order, $type) |
||
80 | { |
||
81 | $countries = ShopConfigExtension::config()->iso_3166_country_codes; |
||
82 | |||
83 | if ($type == 'Postal') { |
||
84 | $address = $order->BillingAddress(); |
||
85 | array_push( |
||
86 | $body['Addresses'], |
||
87 | [ |
||
88 | 'AddressName' => $this->getAddressName($address), |
||
89 | 'AddressType' => $type, |
||
90 | 'City' => $address->City, |
||
91 | 'Country' => $countries[$address->Country], |
||
92 | 'PostalCode' => $address->PostalCode, |
||
93 | 'Region' => $address->State, |
||
94 | 'StreetAddress' => $address->Address, |
||
95 | 'StreetAddress2' => $address->AddressLine2 |
||
96 | ] |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | if ($type == 'Physical') { |
||
101 | $address = $order->ShippingAddress(); |
||
102 | $body['DeliveryCity'] = $address->City; |
||
103 | $body['DeliveryCountry'] = $countries[$address->Country]; |
||
104 | $body['DeliveryPostCode'] = $address->PostalCode; |
||
105 | $body['DeliveryRegion'] = $address->State; |
||
106 | $body['DeliveryStreetAddress'] = $address->Address; |
||
107 | $body['DeliveryStreetAddress2'] = $address->AddressLine2; |
||
108 | |||
109 | array_push( |
||
110 | $body['Addresses'], |
||
111 | [ |
||
112 | 'AddressName' => $this->getAddressName($address), |
||
113 | 'AddressType' => 'Physical', |
||
114 | 'City' => $address->City, |
||
115 | 'Country' => $countries[$address->Country], |
||
116 | 'PostalCode' => $address->PostalCode, |
||
117 | 'Region' => $address->State, |
||
118 | 'StreetAddress' => $address->Address, |
||
119 | 'StreetAddress2' => $address->AddressLine2 |
||
120 | ] |
||
121 | ); |
||
122 | |||
123 | array_push( |
||
124 | $body['Addresses'], |
||
125 | [ |
||
126 | 'AddressName' => $this->getAddressName($address), |
||
127 | 'AddressType' => 'Shipping', |
||
128 | 'City' => $address->City, |
||
129 | 'Country' => $countries[$address->Country], |
||
130 | 'PostalCode' => $address->PostalCode, |
||
131 | 'Region' => $address->State, |
||
132 | 'StreetAddress' => $address->Address, |
||
133 | 'StreetAddress2' => $address->AddressLine2 |
||
134 | ] |
||
135 | ); |
||
136 | } |
||
137 | |||
138 | return $body; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * add the currency code to the body array |
||
143 | * @param array $body |
||
144 | * @param object $order |
||
145 | * @return array $body |
||
146 | */ |
||
147 | public function setBodyCurrencyCode($body, $order) |
||
148 | { |
||
149 | $body['Currency']['CurrencyCode'] = $order->Currency(); |
||
150 | return $body; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Add the Customer Code/Name (use Company field of BillingAddress to allow for B2B eCommerce sites) |
||
155 | * @param array $body |
||
156 | * @param object $order |
||
157 | * @return array $body |
||
158 | */ |
||
159 | public function setBodyCustomerCodeAndName($body, $order) |
||
160 | { |
||
161 | $billing_address = $order->BillingAddress(); |
||
162 | if ($billing_address->Company) { |
||
163 | // use Organisation name |
||
164 | $body['CustomerCode'] = $billing_address->Company; |
||
165 | $body['CustomerName'] = $billing_address->Company; |
||
166 | } else { |
||
167 | // use Contact full name instead |
||
168 | $body['CustomerCode'] = $order->getName(); |
||
169 | $body['CustomerName'] = $order->getName(); |
||
170 | } |
||
171 | return $body; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Set Delivery Method and Delivery Name |
||
176 | * Allow for the SilverShop Shipping module |
||
177 | * @param array $body |
||
178 | * @param object $order |
||
179 | * @return array $body |
||
180 | */ |
||
181 | public function setBodyDeliveryMethodAndDeliveryName($body, $order, $shipping_modifier_class_name) |
||
182 | { |
||
183 | $shipping_modifier = $order->getModifier($shipping_modifier_class_name); |
||
184 | if (!empty($shipping_modifier)) { |
||
185 | $body['DeliveryMethod'] = $shipping_modifier::config()->product_code; |
||
186 | $body['DeliveryName'] = $shipping_modifier::config()->product_code; |
||
187 | } |
||
188 | return $body; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Set Sales Order Lines |
||
193 | * @param array $body |
||
194 | * @param object $order |
||
195 | * @param string $tax_modifier_class_name |
||
196 | * @param int $rounding_precision |
||
197 | * @return array $body |
||
198 | */ |
||
199 | public function setBodySalesOrderLines($body, $order, $tax_modifier_class_name, $rounding_precision) |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Set the Tax Codes |
||
268 | * @param array $body |
||
269 | * @param object $order |
||
270 | * @param string $tax_modifier_class_name |
||
271 | * @return array $body |
||
272 | */ |
||
273 | public function setBodyTaxCode($body, $order, $tax_modifier_class_name) |
||
274 | { |
||
275 | if ($tax_modifier_class_name) { |
||
276 | $tax_modifier = $order->getModifier($tax_modifier_class_name); |
||
277 | if (!empty($tax_modifier)) { |
||
278 | $body['Taxable'] = true; |
||
279 | $body['Tax']['TaxCode'] = $tax_modifier::config()->tax_code; |
||
280 | } |
||
281 | } |
||
282 | return $body; |
||
283 | } |
||
284 | |||
285 | |||
286 | /** |
||
287 | * Calculate the SubTotal and TaxTotal |
||
288 | * @param array $body |
||
289 | * @param object $order |
||
290 | * @param string $tax_modifier_class_name |
||
291 | * @param int $rounding_precision |
||
292 | * @return array $body |
||
293 | */ |
||
294 | public function setBodySubTotalAndTax($body, $order, $tax_modifier_class_name, $rounding_precision) |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Send a sales order to Unleashed upon paid status |
||
333 | * May need to create the Customer first |
||
334 | */ |
||
335 | public function onAfterWrite() |
||
526 | } |
||
527 | } |
||
528 | } |
||
529 | } |
||
531 |