Total Complexity | 61 |
Total Lines | 543 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
48 | class LineItem extends DataObject |
||
49 | { |
||
50 | private static $table_name = 'LineItem'; |
||
51 | |||
52 | /** |
||
53 | * The name of the param used on a related product to |
||
54 | * track Stock Levels. |
||
55 | * |
||
56 | * Defaults to StockLevel |
||
57 | * |
||
58 | * @var string |
||
59 | * @config |
||
60 | */ |
||
61 | private static $stock_param = "StockLevel"; |
||
62 | |||
63 | /** |
||
64 | * Standard database columns |
||
65 | * |
||
66 | * @var array |
||
67 | * @config |
||
68 | */ |
||
69 | private static $db = [ |
||
70 | "Key" => "Varchar(255)", |
||
71 | "Title" => "Varchar", |
||
72 | "Content" => "HTMLText", |
||
73 | "Quantity" => "Int", |
||
74 | "Price" => "Currency", |
||
75 | "Weight" => "Decimal", |
||
76 | "StockID" => "Varchar(100)", |
||
77 | "ProductClass" => "Varchar", |
||
78 | "Locked" => "Boolean", |
||
79 | "Stocked" => "Boolean", |
||
80 | "Deliverable" => "Boolean", |
||
81 | "Customisation" => "Text", |
||
82 | ]; |
||
83 | |||
84 | /** |
||
85 | * Foreign key associations in DB |
||
86 | * |
||
87 | * @var array |
||
88 | * @config |
||
89 | */ |
||
90 | private static $has_one = [ |
||
91 | "Parent" => Estimate::class, |
||
92 | "Tax" => 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", |
||
127 | "UnitTax" => "Currency", |
||
128 | "UnitTotal" => "Currency", |
||
129 | "SubTotal" => "Currency", |
||
130 | "TaxRate" => "Decimal", |
||
131 | "TaxTotal" => "Currency", |
||
132 | "Total" => "Currency", |
||
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" => "Quantity", |
||
145 | "Title" => "Title", |
||
146 | "StockID" => "Stock ID", |
||
147 | "Price" => "Item Price", |
||
148 | "TaxID" => "Tax", |
||
149 | "CustomisationAndPriceList" => "Customisations" |
||
150 | ]; |
||
151 | |||
152 | /** |
||
153 | * Modify default field scaffolding in admin |
||
154 | * |
||
155 | * @return FieldList |
||
156 | */ |
||
157 | public function getCMSFields() |
||
158 | { |
||
159 | |||
160 | $this->beforeUpdateCMSFields(function ($fields) { |
||
161 | $config = SiteConfig::current_site_config(); |
||
162 | |||
163 | $fields->removeByName("Customisation"); |
||
164 | |||
165 | $fields->addFieldToTab( |
||
166 | "Root.Main", |
||
167 | ReadonlyField::create("Key"), |
||
|
|||
168 | "Title" |
||
169 | ); |
||
170 | |||
171 | $fields->addFieldToTab( |
||
172 | "Root.Main", |
||
173 | DropdownField::create( |
||
174 | "TaxID", |
||
175 | $this->fieldLabel("TaxID"), |
||
176 | $config->TaxRates()->map() |
||
177 | ), |
||
178 | "Weight" |
||
179 | ); |
||
180 | |||
181 | $fields->addFieldsToTab( |
||
182 | "Root.Description", |
||
183 | [ |
||
184 | HTMLEditorField::create("Content") |
||
185 | ->addExtraClass("stacked") |
||
186 | ] |
||
187 | ); |
||
188 | |||
189 | // Change unlink button to remove on customisation |
||
190 | $custom_field = $fields->dataFieldByName("Customisations"); |
||
191 | |||
192 | if ($custom_field) { |
||
193 | $config = $custom_field->getConfig(); |
||
194 | $config |
||
195 | ->removeComponentsByType(GridFieldDeleteAction::class) |
||
196 | ->removeComponentsByType(GridFieldDataColumns::class) |
||
197 | ->removeComponentsByType(GridFieldEditButton::class) |
||
198 | ->removeComponentsByType(GridFieldAddNewButton::class) |
||
199 | ->removeComponentsByType(GridFieldAddExistingAutocompleter::class) |
||
200 | ->addComponents( |
||
201 | new GridFieldEditableColumns(), |
||
202 | new GridFieldAddNewInlineButton(), |
||
203 | new GridFieldEditButton(), |
||
204 | new GridFieldDeleteAction() |
||
205 | ); |
||
206 | |||
207 | $custom_field->setConfig($config); |
||
208 | } |
||
209 | }); |
||
210 | |||
211 | return parent::getCMSFields(); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get the rate of tax for this item |
||
216 | * |
||
217 | * @return float |
||
218 | */ |
||
219 | public function getTaxRate() |
||
220 | { |
||
221 | $rate = ($this->Tax()->exists()) ? $this->Tax()->Rate : 0; |
||
222 | |||
223 | $this->extend("updateTaxRate", $rate); |
||
224 | |||
225 | return $rate; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Get the price for a single line item (unit), minus any |
||
230 | * tax |
||
231 | * |
||
232 | * @return float |
||
233 | */ |
||
234 | public function getUnitPrice() |
||
235 | { |
||
236 | $total = $this->Price; |
||
237 | |||
238 | foreach ($this->Customisations() as $customisation) { |
||
239 | $total += $customisation->Price; |
||
240 | } |
||
241 | |||
242 | $this->extend("updateUnitPrice", $total); |
||
243 | |||
244 | return $total; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Get the amount of tax for a single unit of this item |
||
249 | * |
||
250 | * @return float |
||
251 | */ |
||
252 | public function getUnitTax() |
||
253 | { |
||
254 | $total = ($this->UnitPrice / 100) * $this->TaxRate; |
||
255 | |||
256 | $this->extend("updateUnitTax", $total); |
||
257 | |||
258 | return $total; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Get the total price and tax for a single unit |
||
263 | * |
||
264 | * @return float |
||
265 | */ |
||
266 | public function getUnitTotal() |
||
267 | { |
||
268 | $total = $this->UnitPrice + $this->UnitTax; |
||
269 | |||
270 | $this->extend("updateUnitTotal", $total); |
||
271 | |||
272 | return $total; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Get the value of this item, minus any tax |
||
277 | * |
||
278 | * @return float |
||
279 | */ |
||
280 | public function getSubTotal() |
||
281 | { |
||
282 | $total = $this->UnitPrice * $this->Quantity; |
||
283 | |||
284 | $this->extend("updateSubTotal", $total); |
||
285 | |||
286 | return $total; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Get the total amount of tax for a single unit of this item |
||
291 | * |
||
292 | * @return float |
||
293 | */ |
||
294 | public function getTaxTotal() |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Get the value of this item, minus any tax |
||
305 | * |
||
306 | * @return float |
||
307 | */ |
||
308 | public function getTotal() |
||
309 | { |
||
310 | $total = $this->SubTotal + $this->TaxTotal; |
||
311 | |||
312 | $this->extend("updateTotal", $total); |
||
313 | |||
314 | return $total; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Get an image object associated with this line item. |
||
319 | * By default this is retrieved from the base product. |
||
320 | * |
||
321 | * @return Image | null |
||
322 | */ |
||
323 | public function Image() |
||
324 | { |
||
325 | $product = $this->FindStockItem(); |
||
326 | |||
327 | if ($product && method_exists($product, "SortedImages")) { |
||
328 | return $product->SortedImages()->first(); |
||
329 | } elseif ($product && method_exists($product, "Images")) { |
||
330 | return $product->Images()->first(); |
||
331 | } elseif ($product && method_exists($product, "Image") && $product->Image()->exists()) { |
||
332 | return $product->Image(); |
||
333 | } |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Provide a string of customisations seperated by a comma but not |
||
338 | * including a price |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | public function getCustomisationList() |
||
343 | { |
||
344 | $return = []; |
||
345 | $items = $this->Customisations(); |
||
346 | |||
347 | if ($items && $items->exists()) { |
||
348 | foreach ($items as $item) { |
||
349 | $return[] = $item->Title . ': ' . $item->Value; |
||
350 | } |
||
351 | } |
||
352 | |||
353 | $this->extend("updateCustomisationList", $return); |
||
354 | |||
355 | return implode(", ", $return); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Provide a string of customisations seperated by a comma and |
||
360 | * including a price |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | public function getCustomisationAndPriceList() |
||
365 | { |
||
366 | $return = []; |
||
367 | $items = $this->Customisations(); |
||
368 | |||
369 | if ($items && $items->exists()) { |
||
370 | foreach ($items as $item) { |
||
371 | $return[] = $item->Title . ': ' . $item->Value . ' (' . $item->dbObject("Price")->Nice() . ')'; |
||
372 | } |
||
373 | } |
||
374 | |||
375 | $this->extend("updateCustomisationAndPriceList", $return); |
||
376 | |||
377 | return implode(", ", $return); |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Get list of customisations rendering into a basic |
||
382 | * HTML string |
||
383 | * |
||
384 | * @return HTMLText |
||
385 | */ |
||
386 | public function CustomisationHTML() |
||
387 | { |
||
388 | $return = HTMLText::create(); |
||
389 | $items = $this->Customisations(); |
||
390 | $html = ""; |
||
391 | |||
392 | if ($items && $items->exists()) { |
||
393 | foreach ($items as $item) { |
||
394 | $html .= $item->Title . ': ' . $item->Value . ";<br/>"; |
||
395 | } |
||
396 | } |
||
397 | |||
398 | $return->setValue($html); |
||
399 | |||
400 | $this->extend("updateCustomisationHTML", $return); |
||
401 | |||
402 | return $return; |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Match this item to another object in the Database, by the |
||
407 | * provided details. |
||
408 | * |
||
409 | * @param $relation_name = The class name of the related dataobject |
||
410 | * @param $relation_col = The column name of the related object |
||
411 | * @param $match_col = The column we use to match the two objects |
||
412 | * @return DataObject |
||
413 | */ |
||
414 | public function Match($relation_name = null, $relation_col = "StockID", $match_col = "StockID") |
||
415 | { |
||
416 | // Try to determine relation name |
||
417 | if (!$relation_name && !$this->ProductClass) { |
||
418 | $relation_name = "Product"; |
||
419 | } elseif (!$relation_name && $this->ProductClass) { |
||
420 | $relation_name = $this->ProductClass; |
||
421 | } |
||
422 | |||
423 | return $relation_name::get() |
||
424 | ->filter($relation_col, $this->$match_col) |
||
425 | ->first(); |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Find our original stock item (useful for adding links back to the |
||
430 | * original product). |
||
431 | * |
||
432 | * This function is a synonym for @Link Match (as a result of) merging |
||
433 | * LineItem |
||
434 | * |
||
435 | * @return DataObject |
||
436 | */ |
||
437 | public function FindStockItem() |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * Check stock levels for this item, will return the actual number |
||
444 | * of remaining stock after removing the current quantity |
||
445 | * |
||
446 | * @param $qty The quantity we want to check against |
||
447 | * @return Int |
||
448 | */ |
||
449 | public function checkStockLevel($qty) |
||
450 | { |
||
451 | $stock_param = $this->config()->get("stock_param"); |
||
452 | $item = $this->Match(); |
||
453 | $stock = ($item->$stock_param) ? $item->$stock_param : 0; |
||
454 | $return = $stock - $qty; |
||
455 | |||
456 | $this->extend("updateCheckStockLevel", $return); |
||
457 | |||
458 | return $return; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Only order creators or users with VIEW admin rights can view |
||
463 | * |
||
464 | * @return Boolean |
||
465 | */ |
||
466 | public function canView($member = null, $context = []) |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Anyone can create an order item |
||
478 | * |
||
479 | * @return Boolean |
||
480 | */ |
||
481 | public function canCreate($member = null, $context = []) |
||
482 | { |
||
483 | $extended = $this->extend('canCreate', $member); |
||
484 | if ($extended && $extended !== null) { |
||
485 | return $extended; |
||
486 | } |
||
487 | |||
488 | return true; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * No one can edit items once they are created |
||
493 | * |
||
494 | * @return Boolean |
||
495 | */ |
||
496 | public function canEdit($member = null, $context = []) |
||
497 | { |
||
498 | $extended = $this->extend('canEdit', $member); |
||
499 | if ($extended && $extended !== null) { |
||
500 | return $extended; |
||
501 | } |
||
502 | |||
503 | return $this->Parent()->canEdit($member); |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * No one can delete items once they are created |
||
508 | * |
||
509 | * @return Boolean |
||
510 | */ |
||
511 | public function canDelete($member = null, $context = []) |
||
512 | { |
||
513 | $extended = $this->extend('canDelete', $member); |
||
514 | if ($extended && $extended !== null) { |
||
515 | return $extended; |
||
516 | } |
||
517 | |||
518 | return $this->Parent()->canEdit($member); |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Overwrite default duplicate function |
||
523 | * |
||
524 | * @param boolean $doWrite (write the cloned object to DB) |
||
525 | * @return DataObject $clone The duplicated object |
||
526 | */ |
||
527 | public function duplicate($doWrite = true, $manyMany = "many_many") |
||
528 | { |
||
529 | $clone = parent::duplicate($doWrite); |
||
530 | |||
531 | // Ensure we clone any customisations |
||
532 | if ($doWrite) { |
||
533 | foreach ($this->Customisations() as $customisation) { |
||
534 | $new_item = $customisation->duplicate(false); |
||
535 | $new_item->ParentID = $clone->ID; |
||
536 | $new_item->write(); |
||
537 | } |
||
538 | } |
||
539 | |||
540 | return $clone; |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * Pre-write tasks |
||
545 | * |
||
546 | * @return void |
||
547 | */ |
||
548 | public function onBeforeWrite() |
||
549 | { |
||
550 | parent::onBeforeWrite(); |
||
551 | |||
552 | // Generate a unique item key based on |
||
553 | $this->Key = $this->StockID . ':' . base64_encode(json_encode($this->Customisations()->toArray())); |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * Perform post-DB write functions |
||
558 | * |
||
559 | * @return void |
||
560 | */ |
||
561 | public function onAfterWrite() |
||
562 | { |
||
563 | parent::onAfterWrite(); |
||
564 | |||
565 | if ($this->Customisation) { |
||
566 | $data = unserialize($this->Customisation); |
||
567 | |||
568 | if ($data instanceof ArrayList) { |
||
569 | foreach ($data as $data_item) { |
||
570 | $data_item->ParentID = $this->ID; |
||
571 | $data_item->write(); |
||
572 | } |
||
573 | |||
574 | $this->Customisation = null; |
||
575 | $this->write(); |
||
576 | } |
||
577 | } |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * Clean up DB on deletion |
||
582 | * |
||
583 | * @return void |
||
584 | */ |
||
585 | public function onBeforeDelete() |
||
591 | } |
||
592 | } |
||
593 | } |
||
594 |