| Conditions | 23 |
| Paths | 762 |
| Total Lines | 79 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 51 | public static function localise_order($countryCode = null, $force = false, $runAgain = false) |
||
| 52 | { |
||
| 53 | if($runAgain) { |
||
| 54 | self::$_number_of_times_we_have_run_localise_order = 0; |
||
| 55 | } |
||
| 56 | if (self::$_number_of_times_we_have_run_localise_order > 2) { |
||
| 57 | return; |
||
| 58 | } |
||
| 59 | self::$_number_of_times_we_have_run_localise_order++; |
||
| 60 | $order = ShoppingCart::current_order(); |
||
| 61 | if ($order && $order->exists()) { |
||
| 62 | if ($order->IsSubmitted()) { |
||
| 63 | return true; |
||
| 64 | } |
||
| 65 | if (! $countryCode) { |
||
| 66 | $countryCode = $order->getCountry(); |
||
| 67 | } |
||
| 68 | $currencyObject = CountryPrice_EcommerceCurrency::get_currency_for_country($countryCode); |
||
| 69 | if (Config::inst()->get('CountryPrice_OrderDOD', 'only_allow_within_country_sales')) { |
||
| 70 | $distributor = $order->getDistributor($countryCode); |
||
| 71 | $countryOptions = $distributor->Countries(); |
||
| 72 | if ($countryOptions && $countryOptions->count()) { |
||
| 73 | EcommerceCountry::set_for_current_order_only_show_countries($countryOptions->column('Code')); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | //check if the billing and shipping address have a country so that they will not be overridden by previous Orders |
||
| 77 | //we do this to make sure that the previous address can not change the region and thereby destroy the order in the cart |
||
| 78 | if ($billingAddress = $order->CreateOrReturnExistingAddress('BillingAddress')) { |
||
| 79 | if (! $billingAddress->Country || $force) { |
||
| 80 | $billingAddress->Country = $countryCode; |
||
| 81 | $billingAddress->write(); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | if ($shippingAddress = $order->CreateOrReturnExistingAddress('ShippingAddress')) { |
||
| 85 | if (! $shippingAddress->ShippingCountry || $force) { |
||
| 86 | $shippingAddress->ShippingCountry = $countryCode; |
||
| 87 | $shippingAddress->write(); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | //if a country code and currency has been set then all is good |
||
| 92 | //from there we keep it this way |
||
| 93 | if ( |
||
| 94 | $order->OriginatingCountryCode == $countryCode && |
||
| 95 | $order->CurrencyUsedID == $currencyObject->ID |
||
| 96 | ) { |
||
| 97 | return true; |
||
| 98 | } |
||
| 99 | $order->resetLocale = true; |
||
| 100 | $order->write(); |
||
| 101 | $order = Order::get()->byID($order->ID); |
||
| 102 | $orderHasBeenChanged = false; |
||
| 103 | |||
| 104 | //check currency ... |
||
| 105 | if ($order->CurrencyUsedID != $currencyObject->ID) { |
||
| 106 | $order->SetCurrency($currencyObject); |
||
| 107 | $orderHasBeenChanged = true; |
||
| 108 | } |
||
| 109 | if ($orderHasBeenChanged) { |
||
| 110 | ShoppingCart::reset_order_reference(); |
||
| 111 | $order->write(); |
||
| 112 | $items = $order->OrderItems(); |
||
| 113 | if ($items) { |
||
| 114 | foreach ($items as $item) { |
||
| 115 | $buyable = $item->Buyable(true); |
||
| 116 | if (! $buyable->canPurchase()) { |
||
| 117 | $item->delete(); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | // Called after because some modifiers use the country field to calculate the values |
||
| 122 | $order->calculateOrderAttributes(true); |
||
| 123 | } |
||
| 124 | self::localise_order($countryCode); |
||
| 125 | } else { |
||
| 126 | Session::set('temporary_country_order_store', $countryCode); |
||
| 127 | } |
||
| 128 | |||
| 129 | } |
||
| 130 | |||
| 310 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.