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( |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Adds the cartItem into the cart session |
||
| 239 | * |
||
| 240 | * @param CartItem $cartItem |
||
| 241 | * |
||
| 242 | * @return CartItem |
||
| 243 | */ |
||
| 244 | public function addItem(CartItem $cartItem) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Increment the quantity of a cartItem based on the itemHash |
||
| 261 | * |
||
| 262 | * @param $itemHash |
||
| 263 | * |
||
| 264 | * @return CartItem | null |
||
| 265 | */ |
||
| 266 | public function increment($itemHash) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Decrement the quantity of a cartItem based on the itemHash |
||
| 277 | * |
||
| 278 | * @param $itemHash |
||
| 279 | * |
||
| 280 | * @return CartItem | null |
||
| 281 | */ |
||
| 282 | public function decrement($itemHash) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Find items in the cart matching a data set |
||
| 299 | * |
||
| 300 | * |
||
| 301 | * param $data |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function find($data) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Finds a cartItem based on the itemHash |
||
| 319 | * |
||
| 320 | * @param $itemHash |
||
| 321 | * |
||
| 322 | * @return CartItem | null |
||
| 323 | */ |
||
| 324 | public function getItem($itemHash) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Gets all the items within the cart |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | public function getItems() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Updates an items attributes |
||
| 348 | * |
||
| 349 | * @param $itemHash |
||
| 350 | * @param $key |
||
| 351 | * @param $value |
||
| 352 | * |
||
| 353 | * @return CartItem |
||
| 354 | * |
||
| 355 | * @throws Exceptions\InvalidPrice |
||
| 356 | * @throws Exceptions\InvalidQuantity |
||
| 357 | */ |
||
| 358 | public function updateItem($itemHash, $key, $value) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Removes a CartItem based on the itemHash |
||
| 373 | * |
||
| 374 | * @param $itemHash |
||
| 375 | */ |
||
| 376 | public function removeItem($itemHash) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Empties the carts items |
||
| 392 | */ |
||
| 393 | public function emptyCart() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Completely destroys cart and anything associated with it |
||
| 404 | */ |
||
| 405 | public function destroyCart() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Gets the coupons for the current cart |
||
| 420 | * |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | public function getCoupons() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Finds a specific coupon in the cart |
||
| 430 | * |
||
| 431 | * @param $code |
||
| 432 | * @return mixed |
||
| 433 | */ |
||
| 434 | public function findCoupon($code) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Applies a coupon to the cart |
||
| 441 | * |
||
| 442 | * @param CouponContract $coupon |
||
| 443 | */ |
||
| 444 | public function addCoupon(CouponContract $coupon) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Removes a coupon in the cart |
||
| 457 | * |
||
| 458 | * @param $code |
||
| 459 | */ |
||
| 460 | public function removeCoupon($code) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Gets a speific fee from the fees array |
||
| 477 | * |
||
| 478 | * @param $name |
||
| 479 | * |
||
| 480 | * @return mixed |
||
| 481 | */ |
||
| 482 | public function getFee($name) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Allows to charge for additional fees that may or may not be taxable |
||
| 489 | * ex - service fee , delivery fee, tips |
||
| 490 | * |
||
| 491 | * @param $name |
||
| 492 | * @param $amount |
||
| 493 | * @param bool|false $taxable |
||
| 494 | * @param array $options |
||
| 495 | */ |
||
| 496 | public function addFee($name, $amount, $taxable = false, array $options = []) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Reemoves a fee from the fee array |
||
| 505 | * |
||
| 506 | * @param $name |
||
| 507 | */ |
||
| 508 | public function removeFee($name) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Gets the total tax for the cart |
||
| 517 | * |
||
| 518 | * @param bool|true $format |
||
| 519 | * |
||
| 520 | * @return string |
||
| 521 | */ |
||
| 522 | public function taxTotal($format = true) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Gets the total of the cart with or without tax |
||
| 555 | * |
||
| 556 | * @param boolean $format |
||
| 557 | * @param boolean $withDiscount |
||
| 558 | * |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public function total($format = true, $withDiscount = true, $withTax = true) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Gets the subtotal of the cart with or without tax |
||
| 578 | * |
||
| 579 | * @param boolean $format |
||
| 580 | * @param boolean $withDiscount |
||
| 581 | * |
||
| 582 | * @return string |
||
| 583 | */ |
||
| 584 | public function subTotal($format = true, $withDiscount = true) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Get the count based on qty, or number of unique items |
||
| 599 | * |
||
| 600 | * @param bool $withItemQty |
||
| 601 | * |
||
| 602 | * @return int |
||
| 603 | */ |
||
| 604 | public function count($withItemQty = true) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * |
||
| 621 | * Formats the number into a money format based on the locale and international formats |
||
| 622 | * |
||
| 623 | * @param $number |
||
| 624 | * @param $locale |
||
| 625 | * @param $internationalFormat |
||
| 626 | * @param $format |
||
| 627 | * |
||
| 628 | * @return string |
||
| 629 | */ |
||
| 630 | public static function formatMoney($number, $locale = null, $internationalFormat = null, $format = true) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Gets all the fee totals |
||
| 650 | * |
||
| 651 | * @param boolean $format |
||
| 652 | * |
||
| 653 | * @return string |
||
| 654 | */ |
||
| 655 | public function feeTotals($format = true, $withTax = false) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Gets all the fees on the cart object |
||
| 672 | * |
||
| 673 | * @return mixed |
||
| 674 | */ |
||
| 675 | public function getFees() |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Gets the total amount discounted |
||
| 682 | * |
||
| 683 | * @param boolean $format |
||
| 684 | * |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | public function totalDiscount($format = true) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Checks to see if its an item model |
||
| 702 | * |
||
| 703 | * @param $itemModel |
||
| 704 | * |
||
| 705 | * @return bool |
||
| 706 | */ |
||
| 707 | private function isItemModel($itemModel) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Gets the item models options based the config |
||
| 716 | * |
||
| 717 | * @param Model $itemModel |
||
| 718 | * @param array $options |
||
| 719 | * |
||
| 720 | * @return array |
||
| 721 | */ |
||
| 722 | private function getItemModelOptions(Model $itemModel, $options = []) |
||
| 737 | |||
| 738 | } |
||
| 739 |