Complex classes like LaraCart 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 LaraCart, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class LaraCart implements LaraCartContract |
||
| 19 | { |
||
| 20 | const SERVICE = 'laracart'; |
||
| 21 | const HASH = 'generateCartHash'; |
||
| 22 | const RANHASH = 'generateRandomCartItemHash'; |
||
| 23 | |||
| 24 | protected $events; |
||
| 25 | protected $session; |
||
| 26 | protected $authManager; |
||
| 27 | |||
| 28 | public $cart; |
||
| 29 | public $prefix; |
||
| 30 | public $itemModel; |
||
| 31 | public $itemModelRelations; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * LaraCart constructor. |
||
| 35 | * |
||
| 36 | * @param SessionManager $session |
||
| 37 | * @param Dispatcher $events |
||
| 38 | * @param AuthManager $authManager |
||
| 39 | */ |
||
| 40 | public function __construct(SessionManager $session, Dispatcher $events, AuthManager $authManager) |
||
| 41 | { |
||
| 42 | $this->session = $session; |
||
| 43 | $this->events = $events; |
||
| 44 | $this->authManager = $authManager; |
||
| 45 | $this->prefix = config('laracart.cache_prefix', 'laracart'); |
||
| 46 | $this->itemModel = config('laracart.item_model', null); |
||
| 47 | $this->itemModelRelations = config('laracart.item_model_relations', []); |
||
| 48 | |||
| 49 | $this->setInstance($this->session->get($this->prefix . '.instance', 'default')); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Gets all current instances inside the session |
||
| 54 | * |
||
| 55 | * @return mixed |
||
| 56 | */ |
||
| 57 | public function getInstances() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Sets and Gets the instance of the cart in the session we should be using |
||
| 64 | * |
||
| 65 | * @param string $instance |
||
| 66 | * |
||
| 67 | * @return LaraCart |
||
| 68 | */ |
||
| 69 | public function setInstance($instance = 'default') |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Gets the instance in the session |
||
| 85 | * |
||
| 86 | * @param string $instance |
||
| 87 | * |
||
| 88 | * @return $this cart instance |
||
| 89 | */ |
||
| 90 | public function get($instance = 'default') |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Gets an an attribute from the cart |
||
| 108 | * |
||
| 109 | * @param $attribute |
||
| 110 | * @param $defaultValue |
||
| 111 | * |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | public function getAttribute($attribute, $defaultValue = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Gets all the carts attributes |
||
| 121 | * |
||
| 122 | * @return mixed |
||
| 123 | */ |
||
| 124 | public function getAttributes() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Adds an Attribute to the cart |
||
| 131 | * |
||
| 132 | * @param $attribute |
||
| 133 | * @param $value |
||
| 134 | */ |
||
| 135 | public function setAttribute($attribute, $value) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Updates cart session |
||
| 144 | */ |
||
| 145 | public function update() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Removes an attribute from the cart |
||
| 161 | * |
||
| 162 | * @param $attribute |
||
| 163 | */ |
||
| 164 | public function removeAttribute($attribute) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Creates a CartItem and then adds it to cart |
||
| 173 | * |
||
| 174 | * @param string|int $itemID |
||
| 175 | * @param null $name |
||
| 176 | * @param int $qty |
||
| 177 | * @param string $price |
||
| 178 | * @param array $options |
||
| 179 | * @param bool|true $taxable |
||
| 180 | * |
||
| 181 | * @return CartItem |
||
| 182 | */ |
||
| 183 | public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $taxable = true) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Creates a CartItem and then adds it to cart |
||
| 190 | * |
||
| 191 | * @param $itemID |
||
| 192 | * @param null $name |
||
| 193 | * @param int $qty |
||
| 194 | * @param string $price |
||
| 195 | * @param array $options |
||
| 196 | * @param bool|false $taxable |
||
| 197 | * @param bool|false $lineItem |
||
| 198 | * |
||
| 199 | * @return CartItem |
||
| 200 | * |
||
| 201 | * @throws ModelNotFound |
||
| 202 | */ |
||
| 203 | public function add( |
||
| 204 | $itemID, |
||
| 205 | $name = null, |
||
| 206 | $qty = 1, |
||
| 207 | $price = '0.00', |
||
| 208 | $options = [], |
||
| 209 | $taxable = true, |
||
| 210 | $lineItem = false |
||
| 211 | ) { |
||
| 212 | |||
| 213 | if (!empty(config('laracart.item_model'))) { |
||
|
|
|||
| 214 | |||
| 215 | $itemModel = $itemID; |
||
| 216 | |||
| 217 | if (!$this->isItemModel($itemModel)) { |
||
| 218 | $itemModel = new $this->itemModel; |
||
| 219 | $itemModel->with($this->itemModelRelations)->find($itemID); |
||
| 220 | } |
||
| 221 | |||
| 222 | if (empty($itemModel)) { |
||
| 223 | throw new ModelNotFound('Could not find the item '.$itemID); |
||
| 224 | } |
||
| 225 | |||
| 226 | $bindings = config('laracart.item_model_bindings'); |
||
| 227 | |||
| 228 | $itemID = $itemModel[$bindings[\LukePOLO\LaraCart\CartItem::ITEM_ID]]; |
||
| 229 | $name = $itemModel[$bindings[\LukePOLO\LaraCart\CartItem::ITEM_NAME]]; |
||
| 230 | |||
| 231 | if (empty($qty = $name) || !is_int($name)) { |
||
| 232 | $qty = 1; |
||
| 233 | } |
||
| 234 | |||
| 235 | $price = $itemModel[$bindings[\LukePOLO\LaraCart\CartItem::ITEM_PRICE]]; |
||
| 236 | $options = $this->getItemModelOptions($itemModel, $bindings[\LukePOLO\LaraCart\CartItem::ITEM_OPTIONS]); |
||
| 237 | $taxable = $itemModel[$bindings[\LukePOLO\LaraCart\CartItem::ITEM_TAXABLE]]; |
||
| 238 | } |
||
| 239 | |||
| 240 | $item = $this->addItem(new CartItem( |
||
| 241 | $itemID, |
||
| 242 | $name, |
||
| 243 | $qty, |
||
| 244 | $price, |
||
| 245 | $options, |
||
| 246 | $taxable, |
||
| 247 | $lineItem |
||
| 248 | )); |
||
| 249 | |||
| 250 | $this->update(); |
||
| 251 | |||
| 252 | return $this->getItem($item->getHash()); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Adds the cartItem into the cart session |
||
| 257 | * |
||
| 258 | * @param CartItem $cartItem |
||
| 259 | * |
||
| 260 | * @return CartItem |
||
| 261 | */ |
||
| 262 | public function addItem(CartItem $cartItem) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Increment the quantity of a cartItem based on the itemHash |
||
| 279 | * |
||
| 280 | * @param $itemHash |
||
| 281 | * |
||
| 282 | * @return CartItem | null |
||
| 283 | */ |
||
| 284 | public function increment($itemHash) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Decrement the quantity of a cartItem based on the itemHash |
||
| 295 | * |
||
| 296 | * @param $itemHash |
||
| 297 | * |
||
| 298 | * @return CartItem | null |
||
| 299 | */ |
||
| 300 | public function decrement($itemHash) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Find items in the cart matching a data set |
||
| 317 | * |
||
| 318 | * |
||
| 319 | * param $data |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | public function find($data) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Finds a cartItem based on the itemHash |
||
| 337 | * |
||
| 338 | * @param $itemHash |
||
| 339 | * |
||
| 340 | * @return CartItem | null |
||
| 341 | */ |
||
| 342 | public function getItem($itemHash) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Gets all the items within the cart |
||
| 349 | * |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | public function getItems() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Updates an items attributes |
||
| 366 | * |
||
| 367 | * @param $itemHash |
||
| 368 | * @param $key |
||
| 369 | * @param $value |
||
| 370 | * |
||
| 371 | * @return CartItem |
||
| 372 | * |
||
| 373 | * @throws Exceptions\InvalidPrice |
||
| 374 | * @throws Exceptions\InvalidQuantity |
||
| 375 | */ |
||
| 376 | public function updateItem($itemHash, $key, $value) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Removes a CartItem based on the itemHash |
||
| 391 | * |
||
| 392 | * @param $itemHash |
||
| 393 | */ |
||
| 394 | public function removeItem($itemHash) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Empties the carts items |
||
| 410 | */ |
||
| 411 | public function emptyCart() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Completely destroys cart and anything associated with it |
||
| 422 | */ |
||
| 423 | public function destroyCart() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Gets the coupons for the current cart |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function getCoupons() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Finds a specific coupon in the cart |
||
| 448 | * |
||
| 449 | * @param $code |
||
| 450 | * @return mixed |
||
| 451 | */ |
||
| 452 | public function findCoupon($code) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Applies a coupon to the cart |
||
| 459 | * |
||
| 460 | * @param CouponContract $coupon |
||
| 461 | */ |
||
| 462 | public function addCoupon(CouponContract $coupon) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Removes a coupon in the cart |
||
| 475 | * |
||
| 476 | * @param $code |
||
| 477 | */ |
||
| 478 | public function removeCoupon($code) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Gets a specific fee from the fees array |
||
| 495 | * |
||
| 496 | * @param $name |
||
| 497 | * |
||
| 498 | * @return mixed |
||
| 499 | */ |
||
| 500 | public function getFee($name) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Allows to charge for additional fees that may or may not be taxable |
||
| 507 | * ex - service fee , delivery fee, tips |
||
| 508 | * |
||
| 509 | * @param $name |
||
| 510 | * @param $amount |
||
| 511 | * @param bool|false $taxable |
||
| 512 | * @param array $options |
||
| 513 | */ |
||
| 514 | public function addFee($name, $amount, $taxable = false, array $options = []) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Removes a fee from the fee array |
||
| 523 | * |
||
| 524 | * @param $name |
||
| 525 | */ |
||
| 526 | public function removeFee($name) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Removes all the fees set in the cart |
||
| 535 | * |
||
| 536 | */ |
||
| 537 | public function removeFees() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Gets the total tax for the cart |
||
| 546 | * |
||
| 547 | * @param bool|true $format |
||
| 548 | * |
||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | public function taxTotal($format = true) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Gets the total of the cart with or without tax |
||
| 584 | * |
||
| 585 | * @param boolean $format |
||
| 586 | * @param boolean $withDiscount |
||
| 587 | * |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | public function total($format = true, $withDiscount = true, $withTax = true) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Gets the subtotal of the cart with or without tax |
||
| 607 | * |
||
| 608 | * @param boolean $format |
||
| 609 | * @param boolean $withDiscount |
||
| 610 | * |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | public function subTotal($format = true, $withDiscount = true) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Get the count based on qty, or number of unique items |
||
| 628 | * |
||
| 629 | * @param bool $withItemQty |
||
| 630 | * |
||
| 631 | * @return int |
||
| 632 | */ |
||
| 633 | public function count($withItemQty = true) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * |
||
| 650 | * Formats the number into a money format based on the locale and international formats |
||
| 651 | * |
||
| 652 | * @param $number |
||
| 653 | * @param $locale |
||
| 654 | * @param $internationalFormat |
||
| 655 | * @param $format |
||
| 656 | * |
||
| 657 | * @return string |
||
| 658 | */ |
||
| 659 | public static function formatMoney($number, $locale = null, $internationalFormat = null, $format = true) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Gets all the fee totals |
||
| 679 | * |
||
| 680 | * @param boolean $format |
||
| 681 | * |
||
| 682 | * @return string |
||
| 683 | */ |
||
| 684 | public function feeTotals($format = true, $withTax = false) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Gets all the fees on the cart object |
||
| 701 | * |
||
| 702 | * @return mixed |
||
| 703 | */ |
||
| 704 | public function getFees() |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Gets the total amount discounted |
||
| 711 | * |
||
| 712 | * @param boolean $format |
||
| 713 | * |
||
| 714 | * @return string |
||
| 715 | */ |
||
| 716 | public function totalDiscount($format = true) |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Checks to see if its an item model |
||
| 731 | * |
||
| 732 | * @param $itemModel |
||
| 733 | * |
||
| 734 | * @return bool |
||
| 735 | */ |
||
| 736 | private function isItemModel($itemModel) |
||
| 737 | { |
||
| 738 | if (is_object($itemModel) && get_class($itemModel) == config('laracart.item_model')) { |
||
| 739 | return true; |
||
| 740 | } |
||
| 741 | } |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Gets the item models options based the config |
||
| 745 | * |
||
| 746 | * @param Model $itemModel |
||
| 747 | * @param array $options |
||
| 748 | * |
||
| 749 | * @return array |
||
| 750 | */ |
||
| 751 | private function getItemModelOptions(Model $itemModel, $options = []) |
||
| 752 | { |
||
| 753 | $itemOptions = []; |
||
| 754 | foreach ($options as $option) { |
||
| 755 | $itemOptions[$option] = $this->getFromModel($itemModel, $option); |
||
| 756 | } |
||
| 757 | |||
| 758 | return array_filter($itemOptions, function ($value) { |
||
| 759 | if ($value !== false && empty($value)) { |
||
| 760 | return false; |
||
| 761 | } |
||
| 762 | |||
| 763 | return true; |
||
| 764 | }); |
||
| 765 | } |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Gets a option from the model |
||
| 769 | * |
||
| 770 | * @param Model $itemModel |
||
| 771 | * @param $attr |
||
| 772 | * @param null $defaultValue |
||
| 773 | * |
||
| 774 | * @return Model|null |
||
| 775 | */ |
||
| 776 | private function getFromModel(Model $itemModel, $attr, $defaultValue = null) |
||
| 791 | |||
| 792 | } |
||
| 793 |