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