| Total Complexity | 65 |
| Total Lines | 611 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 0 | Features | 2 |
Complex classes like LineItem 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 LineItem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class LineItem extends DataObject implements TaxableProvider |
||
| 48 | { |
||
| 49 | use Taxable; |
||
| 50 | |||
| 51 | private static $table_name = 'LineItem'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The name of the param used on a related product to |
||
| 55 | * track Stock Levels. |
||
| 56 | * |
||
| 57 | * Defaults to StockLevel |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | * @config |
||
| 61 | */ |
||
| 62 | private static $stock_param = "StockLevel"; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Standard database columns |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | * @config |
||
| 69 | */ |
||
| 70 | private static $db = [ |
||
| 71 | "Key" => "Varchar(255)", |
||
| 72 | "Title" => "Varchar", |
||
| 73 | "BasePrice" => "Decimal(9,3)", |
||
| 74 | "Price" => "Currency", |
||
| 75 | "Quantity" => "Int", |
||
| 76 | "StockID" => "Varchar(100)", |
||
| 77 | "ProductClass" => "Varchar", |
||
| 78 | "Locked" => "Boolean", |
||
| 79 | "Stocked" => "Boolean", |
||
| 80 | "Deliverable" => "Boolean" |
||
| 81 | ]; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Foreign key associations in DB |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | * @config |
||
| 88 | */ |
||
| 89 | private static $has_one = [ |
||
| 90 | "Parent" => Estimate::class, |
||
| 91 | "Tax" => TaxRate::class, |
||
| 92 | "TaxRate" => TaxRate::class |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * One to many associations |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | * @config |
||
| 100 | */ |
||
| 101 | private static $has_many = [ |
||
| 102 | "Customisations" => LineItemCustomisation::class |
||
| 103 | ]; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Specify default values of a field |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | * @config |
||
| 110 | */ |
||
| 111 | private static $defaults = [ |
||
| 112 | "Quantity" => 1, |
||
| 113 | "ProductClass" => "Product", |
||
| 114 | "Locked" => false, |
||
| 115 | "Stocked" => false, |
||
| 116 | "Deliverable" => true |
||
| 117 | ]; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Function to DB Object conversions |
||
| 121 | * |
||
| 122 | * @var array |
||
| 123 | * @config |
||
| 124 | */ |
||
| 125 | private static $casting = [ |
||
| 126 | "UnitPrice" => "Currency(9,3)", |
||
| 127 | "UnitTax" => "Currency(9,3)", |
||
| 128 | "UnitTotal" => "Currency(9,3)", |
||
| 129 | "SubTotal" => "Currency(9,3)", |
||
| 130 | "TaxRate" => "Decimal", |
||
| 131 | "TaxTotal" => "Currency(9,3)", |
||
| 132 | "Total" => "Currency(9,3)", |
||
| 133 | "CustomisationList" => "Text", |
||
| 134 | "CustomisationAndPriceList" => "Text", |
||
| 135 | ]; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Fields to display in list tables |
||
| 139 | * |
||
| 140 | * @var array |
||
| 141 | * @config |
||
| 142 | */ |
||
| 143 | private static $summary_fields = [ |
||
| 144 | "Quantity", |
||
| 145 | "Title", |
||
| 146 | "StockID", |
||
| 147 | "BasePrice", |
||
| 148 | "TaxRateID", |
||
| 149 | "CustomisationAndPriceList" |
||
| 150 | ]; |
||
| 151 | |||
| 152 | private static $field_labels = [ |
||
| 153 | "BasePrice"=> "Item Price", |
||
| 154 | "Price" => "Item Price", |
||
| 155 | "TaxID" => "Tax", |
||
| 156 | "TaxRateID"=> "Tax", |
||
| 157 | "CustomisationAndPriceList" => "Customisations" |
||
| 158 | ]; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the basic price for this object |
||
| 162 | * |
||
| 163 | * @return float |
||
| 164 | */ |
||
| 165 | public function getBasePrice() |
||
| 166 | { |
||
| 167 | return $this->dbObject('BasePrice')->getValue(); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Return the tax rate for this Object |
||
| 172 | * |
||
| 173 | * @return TaxRate |
||
| 174 | */ |
||
| 175 | public function getTaxRate() |
||
| 176 | { |
||
| 177 | return $this->TaxRate(); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get the locale from the site |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getLocale() |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get should this field automatically show the price including TAX? |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public function getShowPriceWithTax() |
||
| 196 | { |
||
| 197 | $config = SiteConfig::current_site_config(); |
||
| 198 | $show = $config->ShowPriceAndTax; |
||
| 199 | |||
| 200 | $result = $this->filterTaxableExtensionResults( |
||
| 201 | $this->extend("updateShowPriceWithTax", $show) |
||
| 202 | ); |
||
| 203 | |||
| 204 | if (!empty($result)) { |
||
| 205 | return (bool)$result; |
||
| 206 | } |
||
| 207 | |||
| 208 | return (bool)$show; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * We don't want to show a tax string on Line Items |
||
| 213 | * |
||
| 214 | * @return false |
||
| 215 | */ |
||
| 216 | public function getShowTaxString() |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Modify default field scaffolding in admin |
||
| 223 | * |
||
| 224 | * @return FieldList |
||
| 225 | */ |
||
| 226 | public function getCMSFields() |
||
| 227 | { |
||
| 228 | $this->beforeUpdateCMSFields(function ($fields) { |
||
| 229 | $config = SiteConfig::current_site_config(); |
||
| 230 | |||
| 231 | $fields->removeByName( |
||
| 232 | [ |
||
| 233 | 'Customisation', |
||
| 234 | 'Price', |
||
| 235 | 'TaxID' |
||
| 236 | ] |
||
| 237 | ); |
||
| 238 | |||
| 239 | $fields->addFieldToTab( |
||
| 240 | "Root.Main", |
||
| 241 | ReadonlyField::create("Key"), |
||
| 242 | "Title" |
||
| 243 | ); |
||
| 244 | |||
| 245 | $fields->addFieldToTab( |
||
| 246 | "Root.Main", |
||
| 247 | DropdownField::create( |
||
| 248 | "TaxRateID", |
||
| 249 | $this->fieldLabel("TaxRate"), |
||
| 250 | $config->TaxRates()->map() |
||
| 251 | ), |
||
| 252 | "Quantity" |
||
| 253 | ); |
||
| 254 | |||
| 255 | // Change unlink button to remove on customisation |
||
| 256 | $custom_field = $fields->dataFieldByName("Customisations"); |
||
| 257 | |||
| 258 | if ($custom_field) { |
||
| 259 | $config = $custom_field->getConfig(); |
||
| 260 | $config |
||
| 261 | ->removeComponentsByType(GridFieldDeleteAction::class) |
||
| 262 | ->removeComponentsByType(GridFieldDataColumns::class) |
||
| 263 | ->removeComponentsByType(GridFieldEditButton::class) |
||
| 264 | ->removeComponentsByType(GridFieldAddNewButton::class) |
||
| 265 | ->removeComponentsByType(GridFieldAddExistingAutocompleter::class) |
||
| 266 | ->addComponents( |
||
| 267 | new GridFieldEditableColumns(), |
||
| 268 | new GridFieldAddNewInlineButton(), |
||
| 269 | new GridFieldEditButton(), |
||
| 270 | new GridFieldDeleteAction() |
||
| 271 | ); |
||
| 272 | |||
| 273 | $custom_field->setConfig($config); |
||
| 274 | } |
||
| 275 | }); |
||
| 276 | |||
| 277 | return parent::getCMSFields(); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the price for a single line item (unit), minus any tax |
||
| 282 | * |
||
| 283 | * @return float |
||
| 284 | */ |
||
| 285 | public function getNoTaxPrice() |
||
| 286 | { |
||
| 287 | $price = $this->getBasePrice(); |
||
| 288 | |||
| 289 | foreach ($this->Customisations() as $customisation) { |
||
| 290 | $price += $customisation->getBasePrice(); |
||
| 291 | } |
||
| 292 | |||
| 293 | $result = $this->getOwner()->filterTaxableExtensionResults( |
||
| 294 | $this->extend("updateNoTaxPrice", $price) |
||
| 295 | ); |
||
| 296 | |||
| 297 | if (!empty($result)) { |
||
| 298 | return $result; |
||
| 299 | } |
||
| 300 | |||
| 301 | return $price; |
||
| 302 | } |
||
| 303 | |||
| 304 | public function getUnitPrice() |
||
| 305 | { |
||
| 306 | return $this->getNoTaxPrice(); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get the amount of tax for a single unit of this item |
||
| 311 | * |
||
| 312 | * **NOTE** Tax is rounded at the single item price to avoid multiplication |
||
| 313 | * weirdness. For example 49.995 + 20% is 59.994 for one product, |
||
| 314 | * but 239.976 for 4 (it should be 239.96) |
||
| 315 | * |
||
| 316 | * @return float |
||
| 317 | */ |
||
| 318 | public function getUnitTax() |
||
| 319 | { |
||
| 320 | // Calculate and round tax now to try and minimise penny rounding issues |
||
| 321 | $total = ($this->UnitPrice / 100) * $this->TaxPercentage; |
||
| 322 | |||
| 323 | $this->extend("updateUnitTax", $total); |
||
| 324 | |||
| 325 | return $total; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Overwrite TaxAmount with unit tax |
||
| 330 | * |
||
| 331 | * @return float |
||
| 332 | */ |
||
| 333 | public function getTaxAmount() |
||
| 334 | { |
||
| 335 | return $this->UnitTax; |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get the total price and tax for a single unit |
||
| 340 | * |
||
| 341 | * @return float |
||
| 342 | */ |
||
| 343 | public function getUnitTotal() |
||
| 344 | { |
||
| 345 | $total = $this->UnitPrice + $this->UnitTax; |
||
| 346 | |||
| 347 | $this->extend("updateUnitTotal", $total); |
||
| 348 | |||
| 349 | return $total; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get the value of this item, minus any tax |
||
| 354 | * |
||
| 355 | * @return float |
||
| 356 | */ |
||
| 357 | public function getSubTotal() |
||
| 358 | { |
||
| 359 | $total = $this->NoTaxPrice * $this->Quantity; |
||
| 360 | |||
| 361 | $this->extend("updateSubTotal", $total); |
||
| 362 | |||
| 363 | return $total; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get the total amount of tax for a single unit of this item |
||
| 368 | * |
||
| 369 | * @return float |
||
| 370 | */ |
||
| 371 | public function getTaxTotal() |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get the value of this item, minus any tax |
||
| 382 | * |
||
| 383 | * @return float |
||
| 384 | */ |
||
| 385 | public function getTotal() |
||
| 386 | { |
||
| 387 | $total = $this->SubTotal + $this->TaxTotal; |
||
| 388 | |||
| 389 | $this->extend("updateTotal", $total); |
||
| 390 | |||
| 391 | return $total; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get an image object associated with this line item. |
||
| 396 | * By default this is retrieved from the base product. |
||
| 397 | * |
||
| 398 | * @return Image | null |
||
| 399 | */ |
||
| 400 | public function Image() |
||
| 401 | { |
||
| 402 | $product = $this->FindStockItem(); |
||
| 403 | |||
| 404 | if ($product && method_exists($product, "SortedImages")) { |
||
| 405 | return $product->SortedImages()->first(); |
||
| 406 | } elseif ($product && method_exists($product, "Images")) { |
||
| 407 | return $product->Images()->first(); |
||
| 408 | } elseif ($product && method_exists($product, "Image") && $product->Image()->exists()) { |
||
| 409 | return $product->Image(); |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Provide a string of customisations seperated by a comma but not |
||
| 415 | * including a price |
||
| 416 | * |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | public function getCustomisationList() |
||
| 420 | { |
||
| 421 | $return = []; |
||
| 422 | $items = $this->Customisations(); |
||
| 423 | |||
| 424 | if ($items && $items->exists()) { |
||
| 425 | foreach ($items as $item) { |
||
| 426 | $return[] = $item->Title . ': ' . $item->Value; |
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | $this->extend("updateCustomisationList", $return); |
||
| 431 | |||
| 432 | return implode(", ", $return); |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Provide a string of customisations seperated by a comma and |
||
| 437 | * including a price |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getCustomisationAndPriceList() |
||
| 442 | { |
||
| 443 | $return = []; |
||
| 444 | $items = $this->Customisations(); |
||
| 445 | |||
| 446 | if ($items && $items->exists()) { |
||
| 447 | foreach ($items as $item) { |
||
| 448 | $return[] = $item->Title . ': ' . $item->Value . ' (' . $item->getFormattedPrice() . ')'; |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | $this->extend("updateCustomisationAndPriceList", $return); |
||
| 453 | |||
| 454 | return implode(", ", $return); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Get list of customisations rendering into a basic |
||
| 459 | * HTML string |
||
| 460 | * |
||
| 461 | * @return HTMLText |
||
| 462 | */ |
||
| 463 | public function CustomisationHTML() |
||
| 464 | { |
||
| 465 | $return = HTMLText::create(); |
||
| 466 | $items = $this->Customisations(); |
||
| 467 | $html = ""; |
||
| 468 | |||
| 469 | if ($items && $items->exists()) { |
||
| 470 | foreach ($items as $item) { |
||
| 471 | $html .= $item->Title . ': ' . $item->Value . ";<br/>"; |
||
| 472 | } |
||
| 473 | } |
||
| 474 | |||
| 475 | $return->setValue($html); |
||
| 476 | |||
| 477 | $this->extend("updateCustomisationHTML", $return); |
||
| 478 | |||
| 479 | return $return; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Match this item to another object in the Database, by the |
||
| 484 | * provided details. |
||
| 485 | * |
||
| 486 | * @param $relation_name = The class name of the related dataobject |
||
| 487 | * @param $relation_col = The column name of the related object |
||
| 488 | * @param $match_col = The column we use to match the two objects |
||
| 489 | * @return DataObject |
||
| 490 | */ |
||
| 491 | public function Match($relation_name = null, $relation_col = "StockID", $match_col = "StockID") |
||
| 492 | { |
||
| 493 | // Try to determine relation name |
||
| 494 | if (!$relation_name && !$this->ProductClass) { |
||
| 495 | $relation_name = "Product"; |
||
| 496 | } elseif (!$relation_name && $this->ProductClass) { |
||
| 497 | $relation_name = $this->ProductClass; |
||
| 498 | } |
||
| 499 | |||
| 500 | return $relation_name::get() |
||
| 501 | ->filter($relation_col, $this->$match_col) |
||
| 502 | ->first(); |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Find our original stock item (useful for adding links back to the |
||
| 507 | * original product). |
||
| 508 | * |
||
| 509 | * This function is a synonym for @Link Match (as a result of) merging |
||
| 510 | * LineItem |
||
| 511 | * |
||
| 512 | * @return DataObject |
||
| 513 | */ |
||
| 514 | public function FindStockItem() |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Check stock levels for this item, will return the actual number |
||
| 521 | * of remaining stock after removing the current quantity |
||
| 522 | * |
||
| 523 | * @param $qty The quantity we want to check against |
||
| 524 | * @return Int |
||
| 525 | */ |
||
| 526 | public function checkStockLevel($qty) |
||
| 527 | { |
||
| 528 | $stock_param = $this->config()->get("stock_param"); |
||
| 529 | $item = $this->Match(); |
||
| 530 | $stock = ($item->$stock_param) ? $item->$stock_param : 0; |
||
| 531 | $return = $stock - $qty; |
||
| 532 | |||
| 533 | $this->extend("updateCheckStockLevel", $return); |
||
| 534 | |||
| 535 | return $return; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Generate a key based on this item and its customisations |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public function generateKey() |
||
| 544 | { |
||
| 545 | // Generate a unique item key based on the current ID and customisations |
||
| 546 | $key = base64_encode( |
||
| 547 | json_encode( |
||
| 548 | $this->Customisations()->map("Title", "Value")->toArray() |
||
| 549 | ) |
||
| 550 | ); |
||
| 551 | return $this->StockID . ':' . $key; |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Only order creators or users with VIEW admin rights can view |
||
| 556 | * |
||
| 557 | * @return Boolean |
||
| 558 | */ |
||
| 559 | public function canView($member = null, $context = []) |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Anyone can create an order item |
||
| 571 | * |
||
| 572 | * @return Boolean |
||
| 573 | */ |
||
| 574 | public function canCreate($member = null, $context = []) |
||
| 575 | { |
||
| 576 | $extended = $this->extend('canCreate', $member); |
||
| 577 | if ($extended && $extended !== null) { |
||
| 578 | return $extended; |
||
| 579 | } |
||
| 580 | |||
| 581 | return true; |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * No one can edit items once they are created |
||
| 586 | * |
||
| 587 | * @return Boolean |
||
| 588 | */ |
||
| 589 | public function canEdit($member = null, $context = []) |
||
| 590 | { |
||
| 591 | $extended = $this->extend('canEdit', $member); |
||
| 592 | if ($extended && $extended !== null) { |
||
| 593 | return $extended; |
||
| 594 | } |
||
| 595 | |||
| 596 | return $this->Parent()->canEdit($member); |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * No one can delete items once they are created |
||
| 601 | * |
||
| 602 | * @return Boolean |
||
| 603 | */ |
||
| 604 | public function canDelete($member = null, $context = []) |
||
| 605 | { |
||
| 606 | $extended = $this->extend('canDelete', $member); |
||
| 607 | if ($extended && $extended !== null) { |
||
| 608 | return $extended; |
||
| 609 | } |
||
| 610 | |||
| 611 | return $this->Parent()->canEdit($member); |
||
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Overwrite default duplicate function |
||
| 616 | * |
||
| 617 | * @param boolean $doWrite (write the cloned object to DB) |
||
| 618 | * @return DataObject $clone The duplicated object |
||
| 619 | */ |
||
| 620 | public function duplicate($doWrite = true, $manyMany = "many_many") |
||
| 621 | { |
||
| 622 | $clone = parent::duplicate($doWrite); |
||
| 623 | |||
| 624 | // Ensure we clone any customisations |
||
| 625 | if ($doWrite) { |
||
| 626 | foreach ($this->Customisations() as $customisation) { |
||
| 627 | $new_item = $customisation->duplicate(false); |
||
| 628 | $new_item->ParentID = $clone->ID; |
||
| 629 | $new_item->write(); |
||
| 630 | } |
||
| 631 | } |
||
| 632 | |||
| 633 | return $clone; |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Pre-write tasks |
||
| 638 | * |
||
| 639 | * @return void |
||
| 640 | */ |
||
| 641 | public function onBeforeWrite() |
||
| 642 | { |
||
| 643 | parent::onBeforeWrite(); |
||
| 644 | $this->Key = $this->generateKey(); |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Clean up DB on deletion |
||
| 649 | * |
||
| 650 | * @return void |
||
| 651 | */ |
||
| 652 | public function onBeforeDelete() |
||
| 658 | } |
||
| 659 | } |
||
| 660 | } |
||
| 661 |
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