Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CountryPrice_BuyableExtension 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 CountryPrice_BuyableExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class CountryPrice_BuyableExtension extends DataExtension |
||
|
|
|||
| 9 | { |
||
| 10 | private static $db = array( |
||
| 11 | "AllCountries" => "Boolean" |
||
| 12 | ); |
||
| 13 | |||
| 14 | private static $many_many = array( |
||
| 15 | "IncludedCountries" => "EcommerceCountry", |
||
| 16 | "ExcludedCountries" => "EcommerceCountry" |
||
| 17 | ); |
||
| 18 | |||
| 19 | private static $allow_usage_of_distributor_backup_country_pricing = false; |
||
| 20 | |||
| 21 | public function updateCMSFields(FieldList $fields) |
||
| 22 | { |
||
| 23 | $excludedCountries = EcommerceCountry::get() |
||
| 24 | ->filter(array("DoNotAllowSales" => 1, "AlwaysTheSameAsID" => 0)); |
||
| 25 | if ($excludedCountries->count()) { |
||
| 26 | $excludedCountries = $excludedCountries->map('ID', 'Name')->toArray(); |
||
| 27 | } |
||
| 28 | $includedCountries = EcommerceCountry::get() |
||
| 29 | ->filter(array("DoNotAllowSales" => 0, "AlwaysTheSameAsID" => 0)); |
||
| 30 | if ($includedCountries->count()) { |
||
| 31 | $includedCountries = $includedCountries->map('ID', 'Name')->toArray(); |
||
| 32 | } |
||
| 33 | if ($this->owner->AllCountries) { |
||
| 34 | $tabs = new TabSet('Countries', |
||
| 35 | new Tab( |
||
| 36 | 'Include', |
||
| 37 | new CheckboxField("AllCountries", "All Countries") |
||
| 38 | ) |
||
| 39 | ); |
||
| 40 | } else { |
||
| 41 | $tabs = new TabSet('Countries', |
||
| 42 | $includeTab = new Tab( |
||
| 43 | 'Include', |
||
| 44 | new CheckboxField("AllCountries", "All Countries") |
||
| 45 | ), |
||
| 46 | $excludeTab = new Tab( |
||
| 47 | 'Exclude' |
||
| 48 | ) |
||
| 49 | ); |
||
| 50 | if (count($excludedCountries)) { |
||
| 51 | $includeTab->push( |
||
| 52 | new LiteralField( |
||
| 53 | "ExplanationInclude", |
||
| 54 | "<p>Products are not available in the countries listed below. You can include sales of <i>".$this->owner->Title."</i> to new countries by ticking the box(es) next to any country.</p>" |
||
| 55 | ) |
||
| 56 | ); |
||
| 57 | $includeTab->push( |
||
| 58 | new CheckboxSetField('IncludedCountries', '', $excludedCountries) |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | if (count($includedCountries)) { |
||
| 62 | $excludeTab->push( |
||
| 63 | new LiteralField("ExplanationExclude", "<p>Products are available in all countries listed below. You can exclude sales of <i>".$this->owner->Title."</i> from these countries by ticking the box next to any of them.</p>") |
||
| 64 | ); |
||
| 65 | $excludeTab->push( |
||
| 66 | new CheckboxSetField('ExcludedCountries', '', $includedCountries) |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | |||
| 72 | if ($this->owner->ID) { |
||
| 73 | //start cms_object hack |
||
| 74 | CountryPrice::set_cms_object($this->owner); |
||
| 75 | //end cms_object hack |
||
| 76 | $source = $this->owner->AllCountryPricesForBuyable(); |
||
| 77 | $table = new GridField( |
||
| 78 | 'CountryPrices', |
||
| 79 | 'Country Prices', |
||
| 80 | $source, |
||
| 81 | GridFieldConfig_RecordEditor::create() |
||
| 82 | ); |
||
| 83 | $tab = 'Root.Countries.Pricing'; |
||
| 84 | $fields->addFieldsToTab( |
||
| 85 | $tab, |
||
| 86 | array( |
||
| 87 | NumericField::create('Price', 'Main Price', '', 12), |
||
| 88 | HeaderField::create('OtherCountryPricing', "Prices for other countries"), |
||
| 89 | $table |
||
| 90 | ) |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | |||
| 94 | $fields->addFieldToTab('Root.Countries', $tabs); |
||
| 95 | } |
||
| 96 | |||
| 97 | private $debug = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * This is called from /ecommerce/code/Product |
||
| 101 | * returning NULL is like returning TRUE, i.e. ignore this. |
||
| 102 | * @param Member (optional) $member |
||
| 103 | * @param bool (optional) $checkPrice |
||
| 104 | * @return false | null |
||
| 105 | */ |
||
| 106 | public function canPurchaseByCountry(Member $member = null, $checkPrice = true, $countryCode = '') |
||
| 107 | { |
||
| 108 | $countryObject = CountryPrice_EcommerceCountry::get_real_country($countryCode); |
||
| 109 | if ($countryObject) { |
||
| 110 | if ($this->debug) {debug::log('found country object: '.$countryObject->Code);} |
||
| 111 | $countryCode = $countryObject->Code; |
||
| 112 | } |
||
| 113 | if ($countryCode == '') { |
||
| 114 | if ($this->debug) {debug::log('There is no country Code! ');} |
||
| 115 | |||
| 116 | //we can not decide |
||
| 117 | return null; |
||
| 118 | } else { |
||
| 119 | $canSell = false; |
||
| 120 | $excluded = $this->owner->getManyManyComponents('ExcludedCountries', "\"Code\" = '$countryCode'")->Count(); |
||
| 121 | if ($excluded) { |
||
| 122 | if ($this->debug) {debug::log('excluded country');} |
||
| 123 | |||
| 124 | //no! |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | elseif ($this->owner->AllCountries) { |
||
| 128 | //is there a valid price ??? |
||
| 129 | if ($this->debug) {debug::log('All countries applies - updated ... new price = '.floatval($this->owner->updateCalculatedPrice()));} |
||
| 130 | $canSell = true; |
||
| 131 | } elseif ($countryCode == EcommerceConfig::get('EcommerceCountry', 'default_country_code')) { |
||
| 132 | if ($this->debug) {debug::log('we are in the default country! exiting now ... ');} |
||
| 133 | $canSell = true; |
||
| 134 | } elseif($this->IncludedCountries()->count()) { |
||
| 135 | $included = $this->owner->getManyManyComponents('IncludedCountries', "\"Code\" = '$countryCode'")->Count(); |
||
| 136 | if ($included) { |
||
| 137 | if ($this->debug) {debug::log('In included countries');} |
||
| 138 | //null basically means - ignore ... |
||
| 139 | $canSell = true; |
||
| 140 | } else { |
||
| 141 | |||
| 142 | return false; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | if ($this->debug) {debug::log('the product is for '.($canSell ? '' : 'NOT ').'sale in principal... ');} |
||
| 146 | if ( |
||
| 147 | $canSell && |
||
| 148 | $this->owner instanceof Product && |
||
| 149 | $this->owner->hasMethod('hasVariations') && |
||
| 150 | $this->owner->hasVariations()->count() |
||
| 151 | ) { |
||
| 152 | if ($this->debug) {debug::log('check variations ... ');} |
||
| 153 | //check variations ... |
||
| 154 | return $this->owner->Variations()->First()->canPurchaseByCountry($member, $checkPrice); |
||
| 155 | } |
||
| 156 | |||
| 157 | //is there a valid price ??? |
||
| 158 | $countryPrice = $this->owner->getCalculatedPrice(); |
||
| 159 | if ($this->debug) {debug::log('nothing applies, but we have a country price... '.$countryPrice);} |
||
| 160 | |||
| 161 | return floatval($countryPrice) > 0 ? null : false; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * |
||
| 167 | * @return DataList |
||
| 168 | */ |
||
| 169 | public function AllCountryPricesForBuyable() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * returns all the prices for a particular country and/or currency |
||
| 178 | * for the object |
||
| 179 | * @param string (optional) $country |
||
| 180 | * @param string (optional) $currency |
||
| 181 | * @return DataList |
||
| 182 | */ |
||
| 183 | public function CountryPricesForCountryAndCurrency($countryCode = null, $currency = null) |
||
| 196 | |||
| 197 | private static $_buyable_price = array(); |
||
| 198 | |||
| 199 | /*** |
||
| 200 | * |
||
| 201 | * updates the calculated price to the local price... |
||
| 202 | * if there is no price then we return 0 |
||
| 203 | * if the default price can be used then we use NULL (i.e. ignore it!) |
||
| 204 | * @param float $price (optional) |
||
| 205 | * @return Float | null (ignore this value and use original value) |
||
| 206 | */ |
||
| 207 | public function updateBeforeCalculatedPrice($price = null) |
||
| 208 | { |
||
| 209 | $countryCode = ''; |
||
| 210 | $countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
||
| 211 | if ($countryObject) { |
||
| 212 | $countryCode = $countryObject->Code; |
||
| 213 | } |
||
| 214 | if ($countryCode == '' || $countryCode == EcommerceConfig::get('EcommerceCountry', 'default_country_code')) { |
||
| 215 | if ($this->debug) { |
||
| 216 | debug::log('No country code or default country code: '.$countryCode); |
||
| 217 | } |
||
| 218 | |||
| 219 | return null; |
||
| 220 | } |
||
| 221 | $key = $this->owner->ClassName."___".$this->owner->ID.'____'.$countryCode; |
||
| 222 | if (! isset(self::$_buyable_price[$key])) { |
||
| 223 | //basics |
||
| 224 | $currency = null; |
||
| 225 | $currencyCode = null; |
||
| 226 | |||
| 227 | if ($countryCode) { |
||
| 228 | $order = ShoppingCart::current_order(); |
||
| 229 | CountryPrice_OrderDOD::localise_order(); |
||
| 230 | $currency = $order->CurrencyUsed(); |
||
| 231 | if ($currency) { |
||
| 232 | $currencyCode = strtoupper($currency->Code); |
||
| 233 | //1. exact price for country |
||
| 234 | if ($currencyCode) { |
||
| 235 | $prices = $this->owner->CountryPricesForCountryAndCurrency( |
||
| 236 | $countryCode, |
||
| 237 | $currencyCode |
||
| 238 | ); |
||
| 239 | View Code Duplication | if ($prices && $prices->count() == 1) { |
|
| 240 | self::$_buyable_price[$key] = $prices->First()->Price; |
||
| 241 | return self::$_buyable_price[$key]; |
||
| 242 | } elseif ($prices) { |
||
| 243 | if ($this->debug) { |
||
| 244 | debug::log('MAIN COUNTRY: There is an error number of prices: '.$prices->count()); |
||
| 245 | } |
||
| 246 | } else { |
||
| 247 | if ($this->debug) { |
||
| 248 | debug::log('MAIN COUNTRY: There is no country price: '); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } else { |
||
| 252 | if ($this->debug) { |
||
| 253 | debug::log('MAIN COUNTRY: There is no currency code '.$currencyCode.''); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } else { |
||
| 257 | if ($this->debug) { |
||
| 258 | debug::log('MAIN COUNTRY: there is no currency'); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | if (Config::inst()->get('CountryPrice_BuyableExtension', 'allow_usage_of_distributor_backup_country_pricing')) { |
||
| 262 | //there is a specific country price ... |
||
| 263 | //check for distributor primary country price |
||
| 264 | // if it is the same currency, then use that one ... |
||
| 265 | $distributorCountry = CountryPrice_EcommerceCountry::get_distributor_primary_country($countryCode); |
||
| 266 | if ($distributorCurrency = $distributorCountry->EcommerceCurrency()) { |
||
| 267 | if ($distributorCurrency->ID == $currency->ID) { |
||
| 268 | $distributorCurrencyCode = strtoupper($distributorCurrency->Code); |
||
| 269 | $distributorCountryCode = $distributorCountry->Code; |
||
| 270 | if ($distributorCurrencyCode && $distributorCountryCode) { |
||
| 271 | $prices = $this->owner->CountryPricesForCountryAndCurrency( |
||
| 272 | $distributorCountryCode, |
||
| 273 | $distributorCurrencyCode |
||
| 274 | ); |
||
| 275 | View Code Duplication | if ($prices && $prices->count() == 1) { |
|
| 276 | self::$_buyable_price[$key] = $prices->First()->Price; |
||
| 277 | |||
| 278 | return self::$_buyable_price[$key]; |
||
| 279 | } elseif ($prices) { |
||
| 280 | if ($this->debug) { |
||
| 281 | debug::log('BACKUP COUNTRY: There is an error number of prices: '.$prices->count()); |
||
| 282 | } |
||
| 283 | } else { |
||
| 284 | if ($this->debug) { |
||
| 285 | debug::log('BACKUP COUNTRY: There is no country price: '); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } else { |
||
| 289 | if ($this->debug) { |
||
| 290 | debug::log('BACKUP COUNTRY: We are missing the distributor currency code ('.$distributorCurrencyCode.') or the distributor country code ('.$distributorCountryCode.')'); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } else { |
||
| 294 | if ($this->debug) { |
||
| 295 | debug::log('BACKUP COUNTRY: The distributor currency ID ('.$distributorCurrency->ID.') is not the same as the order currency ID ('.$currency->ID.').'); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } else { |
||
| 300 | if ($this->debug) { |
||
| 301 | debug::log('We do not allow backup country pricing'); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } else { |
||
| 305 | if ($this->debug) { |
||
| 306 | debug::log('There is not Country Code '); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | //order must have a country and a currency |
||
| 310 | if (! $currencyCode || ! $countryCode) { |
||
| 311 | if ($this->debug) { |
||
| 312 | debug::log('No currency ('.$currencyCode.') or no country code ('.$countryCode.') for order: '); |
||
| 313 | } |
||
| 314 | self::$_buyable_price[$key] = 0; |
||
| 315 | return self::$_buyable_price[$key]; |
||
| 316 | } |
||
| 317 | //catch error 2: no country price BUT currency is not default currency ... |
||
| 318 | if (EcommercePayment::site_currency() != $currencyCode) { |
||
| 319 | if ($this->debug) { |
||
| 320 | debug::log('site currency ('.EcommercePayment::site_currency().') is not the same order currency ('.$currencyCode.')'); |
||
| 321 | } |
||
| 322 | self::$_buyable_price[$key] = 0; |
||
| 323 | return self::$_buyable_price[$key]; |
||
| 324 | } |
||
| 325 | if ($this->debug) { |
||
| 326 | debug::log('SETTING '.$key.' to ZERO - NOT FOR SALE'); |
||
| 327 | } |
||
| 328 | self::$_buyable_price[$key] = 0; |
||
| 329 | |||
| 330 | return self::$_buyable_price[$key]; |
||
| 331 | } |
||
| 332 | |||
| 333 | return self::$_buyable_price[$key]; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * delete the related prices |
||
| 338 | */ |
||
| 339 | public function onBeforeDelete() |
||
| 348 | |||
| 349 | // VARIATION CODE ONLY |
||
| 350 | |||
| 351 | // We us isNew to presave if we should add some country price for the newy created variation based on the "possibly" pre-existing ones of the product |
||
| 352 | protected $isNew = false; |
||
| 353 | |||
| 354 | public function onBeforeWrite() |
||
| 358 | |||
| 359 | |||
| 360 | public function onAfterWrite() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * as long as we do not give distributors access to the Products |
||
| 401 | * this is fairly safe. |
||
| 402 | * @param member (optiona) $member |
||
| 403 | * @return null / bool |
||
| 404 | */ |
||
| 405 | public function canEdit($member = null) |
||
| 418 | } |
||
| 419 |
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.