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 |
||
| 17 | class LaraCart implements LaraCartContract |
||
| 18 | { |
||
| 19 | const QTY = 'qty'; |
||
| 20 | const HASH = 'generateCartHash'; |
||
| 21 | const PRICE = 'price'; |
||
| 22 | const SERVICE = 'laracart'; |
||
| 23 | const RANHASH = 'generateRandomCartItemHash'; |
||
| 24 | |||
| 25 | protected $events; |
||
| 26 | protected $session; |
||
| 27 | protected $authManager; |
||
| 28 | protected $prefix; |
||
| 29 | |||
| 30 | public $cart; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * LaraCart constructor. |
||
| 34 | * |
||
| 35 | * @param SessionManager $session |
||
| 36 | * @param Dispatcher $events |
||
| 37 | * @param AuthManager $authManager |
||
| 38 | */ |
||
| 39 | public function __construct(SessionManager $session, Dispatcher $events, AuthManager $authManager) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Gets all current instances inside the session |
||
| 51 | * |
||
| 52 | * @return mixed |
||
| 53 | */ |
||
| 54 | public function getInstances() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Sets and Gets the instance of the cart in the session we should be using |
||
| 61 | * |
||
| 62 | * @param string $instance |
||
| 63 | * |
||
| 64 | * @return LaraCart |
||
| 65 | */ |
||
| 66 | public function setInstance($instance = 'default') |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Gets the instance in the session |
||
| 82 | * |
||
| 83 | * @param string $instance |
||
| 84 | * |
||
| 85 | * @return $this cart instance |
||
| 86 | */ |
||
| 87 | public function get($instance = 'default') |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Gets an an attribute from the cart |
||
| 105 | * |
||
| 106 | * @param $attribute |
||
| 107 | * @param $defaultValue |
||
| 108 | * |
||
| 109 | * @return mixed |
||
| 110 | */ |
||
| 111 | public function getAttribute($attribute, $defaultValue = null) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Gets all the carts attributes |
||
| 118 | * |
||
| 119 | * @return mixed |
||
| 120 | */ |
||
| 121 | public function getAttributes() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Adds an Attribute to the cart |
||
| 128 | * |
||
| 129 | * @param $attribute |
||
| 130 | * @param $value |
||
| 131 | */ |
||
| 132 | public function setAttribute($attribute, $value) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Updates cart session |
||
| 141 | */ |
||
| 142 | public function update() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Removes an attribute from the cart |
||
| 156 | * |
||
| 157 | * @param $attribute |
||
| 158 | */ |
||
| 159 | public function removeAttribute($attribute) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Creates a CartItem and then adds it to cart |
||
| 168 | * |
||
| 169 | * @param string|int $itemID |
||
| 170 | * @param null $name |
||
| 171 | * @param int $qty |
||
| 172 | * @param string $price |
||
| 173 | * @param array $options |
||
| 174 | * @param bool|true $taxable |
||
| 175 | * |
||
| 176 | * @return CartItem |
||
| 177 | */ |
||
| 178 | public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $taxable = true) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Creates a CartItem and then adds it to cart |
||
| 185 | * |
||
| 186 | * @param $itemID |
||
| 187 | * @param null $name |
||
| 188 | * @param int $qty |
||
| 189 | * @param string $price |
||
| 190 | * @param array $options |
||
| 191 | * @param bool|false $taxable |
||
| 192 | * @param bool|false $lineItem |
||
| 193 | * |
||
| 194 | * @return CartItem |
||
| 195 | */ |
||
| 196 | public function add( |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Adds the cartItem into the cart session |
||
| 238 | * |
||
| 239 | * @param CartItem $cartItem |
||
| 240 | * |
||
| 241 | * @return CartItem |
||
| 242 | */ |
||
| 243 | public function addItem(CartItem $cartItem) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Increment the quantity of a cartItem based on the itemHash |
||
| 260 | * |
||
| 261 | * @param $itemHash |
||
| 262 | * |
||
| 263 | * @return CartItem | null |
||
| 264 | */ |
||
| 265 | public function increment($itemHash) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Decrement the quantity of a cartItem based on the itemHash |
||
| 276 | * |
||
| 277 | * @param $itemHash |
||
| 278 | * |
||
| 279 | * @return CartItem | null |
||
| 280 | */ |
||
| 281 | public function decrement($itemHash) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Find items in the cart matching a data set |
||
| 298 | * |
||
| 299 | * |
||
| 300 | * param $data |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function find($data) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Finds a cartItem based on the itemHash |
||
| 318 | * |
||
| 319 | * @param $itemHash |
||
| 320 | * |
||
| 321 | * @return CartItem | null |
||
| 322 | */ |
||
| 323 | public function getItem($itemHash) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Gets all the items within the cart |
||
| 330 | * |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | public function getItems() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Updates an items attributes |
||
| 347 | * |
||
| 348 | * @param $itemHash |
||
| 349 | * @param $key |
||
| 350 | * @param $value |
||
| 351 | * |
||
| 352 | * @return CartItem |
||
| 353 | * |
||
| 354 | * @throws Exceptions\InvalidPrice |
||
| 355 | * @throws Exceptions\InvalidQuantity |
||
| 356 | */ |
||
| 357 | public function updateItem($itemHash, $key, $value) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Removes a CartItem based on the itemHash |
||
| 372 | * |
||
| 373 | * @param $itemHash |
||
| 374 | */ |
||
| 375 | public function removeItem($itemHash) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Empties the carts items |
||
| 391 | */ |
||
| 392 | public function emptyCart() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Completely destroys cart and anything associated with it |
||
| 403 | */ |
||
| 404 | public function destroyCart() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Gets the coupons for the current cart |
||
| 419 | * |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | public function getCoupons() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Finds a specific coupon in the cart |
||
| 429 | * |
||
| 430 | * @param $code |
||
| 431 | * @return mixed |
||
| 432 | */ |
||
| 433 | public function findCoupon($code) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Applies a coupon to the cart |
||
| 440 | * |
||
| 441 | * @param CouponContract $coupon |
||
| 442 | */ |
||
| 443 | public function addCoupon(CouponContract $coupon) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Removes a coupon in the cart |
||
| 456 | * |
||
| 457 | * @param $code |
||
| 458 | */ |
||
| 459 | public function removeCoupon($code) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Gets a specific fee from the fees array |
||
| 476 | * |
||
| 477 | * @param $name |
||
| 478 | * |
||
| 479 | * @return mixed |
||
| 480 | */ |
||
| 481 | public function getFee($name) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Allows to charge for additional fees that may or may not be taxable |
||
| 488 | * ex - service fee , delivery fee, tips |
||
| 489 | * |
||
| 490 | * @param $name |
||
| 491 | * @param $amount |
||
| 492 | * @param bool|false $taxable |
||
| 493 | * @param array $options |
||
| 494 | */ |
||
| 495 | public function addFee($name, $amount, $taxable = false, array $options = []) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Removes a fee from the fee array |
||
| 504 | * |
||
| 505 | * @param $name |
||
| 506 | */ |
||
| 507 | public function removeFee($name) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Removes all the fees set in the cart |
||
| 516 | * |
||
| 517 | */ |
||
| 518 | public function removeFees() |
||
| 519 | { |
||
| 520 | $this->cart->fees = []; |
||
| 521 | |||
| 522 | $this->update(); |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Gets the total tax for the cart |
||
| 527 | * |
||
| 528 | * @param bool|true $format |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | public function taxTotal($format = true) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Gets the total of the cart with or without tax |
||
| 565 | * |
||
| 566 | * @param boolean $format |
||
| 567 | * @param boolean $withDiscount |
||
| 568 | * |
||
| 569 | * @return string |
||
| 570 | */ |
||
| 571 | public function total($format = true, $withDiscount = true, $withTax = true) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Gets the subtotal of the cart with or without tax |
||
| 588 | * |
||
| 589 | * @param boolean $format |
||
| 590 | * @param boolean $withDiscount |
||
| 591 | * |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | public function subTotal($format = true, $withDiscount = true) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Get the count based on qty, or number of unique items |
||
| 609 | * |
||
| 610 | * @param bool $withItemQty |
||
| 611 | * |
||
| 612 | * @return int |
||
| 613 | */ |
||
| 614 | public function count($withItemQty = true) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * |
||
| 631 | * Formats the number into a money format based on the locale and international formats |
||
| 632 | * |
||
| 633 | * @param $number |
||
| 634 | * @param $locale |
||
| 635 | * @param $internationalFormat |
||
| 636 | * @param $format |
||
| 637 | * |
||
| 638 | * @return string |
||
| 639 | */ |
||
| 640 | public static function formatMoney($number, $locale = null, $internationalFormat = null, $format = true) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Gets all the fee totals |
||
| 660 | * |
||
| 661 | * @param boolean $format |
||
| 662 | * |
||
| 663 | * @return string |
||
| 664 | */ |
||
| 665 | public function feeTotals($format = true, $withTax = false) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Gets all the fees on the cart object |
||
| 682 | * |
||
| 683 | * @return mixed |
||
| 684 | */ |
||
| 685 | public function getFees() |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Gets the total amount discounted |
||
| 692 | * |
||
| 693 | * @param boolean $format |
||
| 694 | * |
||
| 695 | * @return string |
||
| 696 | */ |
||
| 697 | public function totalDiscount($format = true) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Checks to see if its an item model |
||
| 712 | * |
||
| 713 | * @param $itemModel |
||
| 714 | * |
||
| 715 | * @return bool |
||
| 716 | */ |
||
| 717 | private function isItemModel($itemModel) |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Gets the item models options based the config |
||
| 726 | * |
||
| 727 | * @param Model $itemModel |
||
| 728 | * @param array $options |
||
| 729 | * |
||
| 730 | * @return array |
||
| 731 | */ |
||
| 732 | private function getItemModelOptions(Model $itemModel, $options = []) |
||
| 747 | |||
| 748 | } |
||
| 749 |