| Total Complexity | 42 |
| Total Lines | 444 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PricingExtension 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 PricingExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class PricingExtension extends DataExtension |
||
| 22 | { |
||
| 23 | use Configurable; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Default behaviour for price with tax (if current instance not set) |
||
| 27 | * |
||
| 28 | * @var boolean |
||
| 29 | */ |
||
| 30 | private static $default_price_with_tax = false; |
||
|
|
|||
| 31 | |||
| 32 | /** |
||
| 33 | * Default behaviour for adding the tax string to the rendered currency. |
||
| 34 | * |
||
| 35 | * @var boolean |
||
| 36 | */ |
||
| 37 | private static $default_tax_string = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Default decimal precision used for rounding |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | private static $default_precision = 2; |
||
| 45 | |||
| 46 | |||
| 47 | private static $db = [ |
||
| 48 | 'BasePrice' => 'Decimal' |
||
| 49 | ]; |
||
| 50 | |||
| 51 | private static $has_one = [ |
||
| 52 | 'TaxCategory' => TaxCategory::class, |
||
| 53 | 'TaxRate' => TaxRate::class |
||
| 54 | ]; |
||
| 55 | |||
| 56 | private static $casting = [ |
||
| 57 | 'CurrencySymbol' => 'Varchar(1)', |
||
| 58 | 'Currency' => 'Varchar(3)', |
||
| 59 | "NoTaxPrice" => "Decimal", |
||
| 60 | "TaxAmount" => "Decimal", |
||
| 61 | "TaxPercentage" => "Decimal", |
||
| 62 | "PriceAndTax" => "Decimal", |
||
| 63 | 'TaxString' => 'Varchar', |
||
| 64 | 'RoundedNoTaxPrice' => 'Decimal', |
||
| 65 | 'RoundedTaxAmount' => 'Decimal', |
||
| 66 | 'RoundedPriceAndTax'=> 'Decimal', |
||
| 67 | 'ShowPriceWithTax' => 'Boolean', |
||
| 68 | 'ShowTaxString' => 'Boolean', |
||
| 69 | 'FormattedPrice' => 'Varchar', |
||
| 70 | 'NicePrice' => 'HTMLText' |
||
| 71 | ]; |
||
| 72 | |||
| 73 | private static $field_labels = [ |
||
| 74 | 'BasePrice' => 'Price' |
||
| 75 | ]; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Filter the results returned by an extension |
||
| 79 | * |
||
| 80 | * @param mixed $results Possible results |
||
| 81 | * |
||
| 82 | * @return mixed |
||
| 83 | */ |
||
| 84 | public function filterExtensionResults($results) |
||
| 85 | { |
||
| 86 | if (!empty($results) && is_array($results)) { |
||
| 87 | $results = array_filter( |
||
| 88 | $results, |
||
| 89 | function ($v) { |
||
| 90 | return !is_null($v); |
||
| 91 | } |
||
| 92 | ); |
||
| 93 | if (is_array($results) && count($results) > 0) { |
||
| 94 | return $results[0]; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get should this field automatically show the price including TAX? |
||
| 103 | * |
||
| 104 | * @return boolean |
||
| 105 | */ |
||
| 106 | public function getShowPriceWithTax() |
||
| 107 | { |
||
| 108 | return $this->config()->get('default_price_with_tax'); |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Get if this field should add a "Tax String" (EG Includes VAT) to the rendered |
||
| 114 | * currency? |
||
| 115 | * |
||
| 116 | * @return boolean|null |
||
| 117 | */ |
||
| 118 | public function getShowTaxString() |
||
| 119 | { |
||
| 120 | return $this->config()->get('default_tax_string'); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get current decimal precision for rounding |
||
| 125 | * |
||
| 126 | * @return int |
||
| 127 | */ |
||
| 128 | public function getPrecision() |
||
| 129 | { |
||
| 130 | return $this->config()->get('default_precision'); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Return the currently available locale |
||
| 135 | * |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function getLocale() |
||
| 139 | { |
||
| 140 | return i18n::get_locale(); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get currency formatter |
||
| 145 | * |
||
| 146 | * @return NumberFormatter |
||
| 147 | */ |
||
| 148 | public function getFormatter() |
||
| 149 | { |
||
| 150 | return NumberFormatter::create( |
||
| 151 | $this->getOwner()->getLocale(), |
||
| 152 | NumberFormatter::CURRENCY |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get a currency symbol from the current site local |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getCurrencySymbol() |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get ISO 4217 currency code from curent locale |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getCurrency() |
||
| 175 | { |
||
| 176 | return $this |
||
| 177 | ->getOwner() |
||
| 178 | ->getFormatter() |
||
| 179 | ->getTextAttribute(NumberFormatter::CURRENCY_CODE); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Shortcut to get the price of this product without tax |
||
| 184 | * |
||
| 185 | * @return float |
||
| 186 | */ |
||
| 187 | public function getNoTaxPrice() |
||
| 188 | { |
||
| 189 | $price = $this->getOwner()->BasePrice; |
||
| 190 | $result = $this->getOwner()->filterExtensionResults( |
||
| 191 | $this->getOwner()->extend("updateNoTaxPrice", $price) |
||
| 192 | ); |
||
| 193 | |||
| 194 | if (!empty($result)) { |
||
| 195 | return $result; |
||
| 196 | } |
||
| 197 | |||
| 198 | return $price; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the current amount rounded to the desired precision |
||
| 203 | * |
||
| 204 | * @return float |
||
| 205 | */ |
||
| 206 | public function getRoundedNoTaxPrice() |
||
| 207 | { |
||
| 208 | return number_format( |
||
| 209 | $this->getOwner()->BasePrice, |
||
| 210 | $this->getOwner()->getPrecision() |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Find a tax rate based on the selected ID, or revert to using the valid tax |
||
| 216 | * from the current category |
||
| 217 | * |
||
| 218 | * @return \SilverCommerce\TaxAdmin\Model\TaxRate |
||
| 219 | */ |
||
| 220 | public function getTaxRate() |
||
| 221 | { |
||
| 222 | $tax = TaxRate::get()->byID($this->getOwner()->TaxRateID); |
||
| 223 | |||
| 224 | // If no tax explicity set, try to get from category |
||
| 225 | if (empty($tax)) { |
||
| 226 | $category = TaxCategory::get()->byID($this->getOwner()->TaxCategoryID); |
||
| 227 | |||
| 228 | $tax = (!empty($category)) ? $category->ValidTax() : null ; |
||
| 229 | } |
||
| 230 | |||
| 231 | if (empty($tax)) { |
||
| 232 | $tax = TaxRate::create(); |
||
| 233 | $tax->ID = -1; |
||
| 234 | } |
||
| 235 | |||
| 236 | return $tax; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get the percentage tax rate assotiated with this field |
||
| 241 | * |
||
| 242 | * @return float |
||
| 243 | */ |
||
| 244 | public function getTaxPercentage() |
||
| 245 | { |
||
| 246 | $percent = $this->getOwner()->getTaxRate()->Rate; |
||
| 247 | |||
| 248 | $result = $this->getOwner()->filterExtensionResults( |
||
| 249 | $this->getOwner()->extend("updateTaxPercentage", $percent) |
||
| 250 | ); |
||
| 251 | |||
| 252 | if (!empty($result)) { |
||
| 253 | return $result; |
||
| 254 | } |
||
| 255 | |||
| 256 | return $percent; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get a final tax amount for this object. You can extend this |
||
| 261 | * method using "UpdateTax" allowing third party modules to alter |
||
| 262 | * tax amounts dynamically. |
||
| 263 | * |
||
| 264 | * @return float |
||
| 265 | */ |
||
| 266 | public function getTaxAmount() |
||
| 267 | { |
||
| 268 | if (!$this->getOwner()->exists()) { |
||
| 269 | return 0; |
||
| 270 | } |
||
| 271 | |||
| 272 | // Round using default rounding defined on MathsHelper |
||
| 273 | $tax = MathsHelper::round( |
||
| 274 | ($this->getOwner()->BasePrice / 100) * $this->getOwner()->TaxPercentage, |
||
| 275 | 4 |
||
| 276 | ); |
||
| 277 | |||
| 278 | $result = $this->getOwner()->filterExtensionResults( |
||
| 279 | $this->getOwner()->extend("updateTaxAmount", $tax) |
||
| 280 | ); |
||
| 281 | |||
| 282 | if (!empty($result)) { |
||
| 283 | return $result; |
||
| 284 | } |
||
| 285 | |||
| 286 | return $tax; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the tax amount rounded to the desired precision |
||
| 291 | * |
||
| 292 | * @return float |
||
| 293 | */ |
||
| 294 | public function getRoundedTaxAmount() |
||
| 295 | { |
||
| 296 | return number_format( |
||
| 297 | $this->getOwner()->TaxAmount, |
||
| 298 | $this->getPrecision() |
||
| 299 | ); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get the Tax Rate object applied to this product |
||
| 304 | * |
||
| 305 | * @return float |
||
| 306 | */ |
||
| 307 | public function getPriceAndTax() |
||
| 308 | { |
||
| 309 | $notax = $this->getOwner()->NoTaxPrice; |
||
| 310 | $tax = $this->getOwner()->TaxAmount; |
||
| 311 | $price = $notax + $tax; |
||
| 312 | |||
| 313 | $result = $this->getOwner()->filterExtensionResults( |
||
| 314 | $this->getOwner()->extend("updatePriceAndTax", $price) |
||
| 315 | ); |
||
| 316 | |||
| 317 | if (!empty($result)) { |
||
| 318 | return $result; |
||
| 319 | } |
||
| 320 | |||
| 321 | return $price; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the price and tax amount rounded to the desired precision |
||
| 326 | * |
||
| 327 | * @return float |
||
| 328 | */ |
||
| 329 | public function getRoundedPriceAndTax() |
||
| 330 | { |
||
| 331 | $amount = $this->getOwner()->RoundedNoTaxPrice; |
||
| 332 | $tax = $this->getOwner()->RoundedTaxAmount; |
||
| 333 | $price = $amount + $tax; |
||
| 334 | |||
| 335 | $result = $this->getOwner()->filterExtensionResults( |
||
| 336 | $this->getOwner()->extend("updateRoundedPriceAndTax", $price) |
||
| 337 | ); |
||
| 338 | |||
| 339 | if (!empty($result)) { |
||
| 340 | return $result; |
||
| 341 | } |
||
| 342 | |||
| 343 | return $price; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Generate a string to go with the the product price. We can |
||
| 348 | * overwrite the wording of this by using Silverstripes language |
||
| 349 | * files |
||
| 350 | * |
||
| 351 | * @param bool|null $include_tax Should this include tax or not? |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getTaxString($include_tax = null) |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Return a formatted price (based on locale) |
||
| 391 | * |
||
| 392 | * @param bool $include_tax Should the formatted price include tax? |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public function getFormattedPrice($include_tax = false) |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get nicely formatted currency (based on current locale) |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getNicePrice() |
||
| 421 | { |
||
| 422 | return $this->getOwner()->renderWith(__CLASS__ . "_NicePrice"); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Create a field group to hold tax info. |
||
| 427 | * |
||
| 428 | * @return FieldList |
||
| 429 | */ |
||
| 430 | public function updateCMSFields(FieldList $fields) |
||
| 465 | ); |
||
| 466 | } |
||
| 467 | } |
||
| 468 |