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