CartRepository.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Shop\Carts\Repositories;
  3. use Jsdecena\Baserepo\BaseRepository;
  4. use App\Shop\Carts\Exceptions\ProductInCartNotFoundException;
  5. use App\Shop\Carts\Repositories\Interfaces\CartRepositoryInterface;
  6. use App\Shop\Carts\ShoppingCart;
  7. use App\Shop\Couriers\Courier;
  8. use App\Shop\Customers\Customer;
  9. use App\Shop\Products\Product;
  10. use App\Shop\Products\Repositories\ProductRepository;
  11. use Gloudemans\Shoppingcart\Cart;
  12. use Gloudemans\Shoppingcart\CartItem;
  13. use Gloudemans\Shoppingcart\Exceptions\InvalidRowIDException;
  14. use Illuminate\Support\Collection;
  15. class CartRepository extends BaseRepository implements CartRepositoryInterface
  16. {
  17. /**
  18. * CartRepository constructor.
  19. * @param ShoppingCart $cart
  20. */
  21. public function __construct(ShoppingCart $cart)
  22. {
  23. $this->model = $cart;
  24. }
  25. /**
  26. * @param Product $product
  27. * @param int $int
  28. * @param array $options
  29. * @return CartItem
  30. */
  31. public function addToCart(Product $product, int $int, $options = []) : CartItem
  32. {
  33. return $this->model->add($product, $int, $options);
  34. }
  35. /**
  36. * @return \Illuminate\Support\Collection
  37. */
  38. public function getCartItems() : Collection
  39. {
  40. return $this->model->content();
  41. }
  42. /**
  43. * @param string $rowId
  44. *
  45. * @throws ProductInCartNotFoundException
  46. */
  47. public function removeToCart(string $rowId)
  48. {
  49. try {
  50. $this->model->remove($rowId);
  51. } catch (InvalidRowIDException $e) {
  52. throw new ProductInCartNotFoundException('Product in cart not found.');
  53. }
  54. }
  55. /**
  56. * Count the items in the cart
  57. *
  58. * @return int
  59. */
  60. public function countItems() : int
  61. {
  62. return $this->model->count();
  63. }
  64. /**
  65. * Get the sub total of all the items in the cart
  66. *
  67. * @param int $decimals
  68. * @return float
  69. */
  70. public function getSubTotal(int $decimals = 2)
  71. {
  72. return $this->model->subtotal($decimals, '.', '');
  73. }
  74. /**
  75. * Get the final total of all the items in the cart minus tax
  76. *
  77. * @param int $decimals
  78. * @param float $shipping
  79. * @return float
  80. */
  81. public function getTotal(int $decimals = 2, $shipping = 0.00)
  82. {
  83. return $this->model->total($decimals, '.', '', $shipping);
  84. }
  85. /**
  86. * @param string $rowId
  87. * @param int $quantity
  88. * @return CartItem
  89. */
  90. public function updateQuantityInCart(string $rowId, int $quantity) : CartItem
  91. {
  92. return $this->model->update($rowId, $quantity);
  93. }
  94. /**
  95. * Return the specific item in the cart
  96. *
  97. * @param string $rowId
  98. * @return \Gloudemans\Shoppingcart\CartItem
  99. */
  100. public function findItem(string $rowId) : CartItem
  101. {
  102. return $this->model->get($rowId);
  103. }
  104. /**
  105. * Returns the tax
  106. *
  107. * @param int $decimals
  108. * @return float
  109. */
  110. public function getTax(int $decimals = 2)
  111. {
  112. return $this->model->tax($decimals);
  113. }
  114. /**
  115. * @param Courier $courier
  116. * @return mixed
  117. */
  118. public function getShippingFee(Courier $courier)
  119. {
  120. return number_format($courier->cost, 2);
  121. }
  122. /**
  123. * Clear the cart content
  124. */
  125. public function clearCart()
  126. {
  127. $this->model->destroy();
  128. }
  129. /**
  130. * @param Customer $customer
  131. * @param string $instance
  132. */
  133. public function saveCart(Customer $customer, $instance = 'default')
  134. {
  135. $this->model->instance($instance)->store($customer->email);
  136. }
  137. /**
  138. * @param Customer $customer
  139. * @param string $instance
  140. * @return Cart
  141. */
  142. public function openCart(Customer $customer, $instance = 'default')
  143. {
  144. $this->model->instance($instance)->restore($customer->email);
  145. return $this->model;
  146. }
  147. /**
  148. * @return Collection
  149. */
  150. public function getCartItemsTransformed() : Collection
  151. {
  152. return $this->getCartItems()->map(function ($item) {
  153. $productRepo = new ProductRepository(new Product());
  154. $product = $productRepo->findProductById($item->id);
  155. $item->product = $product;
  156. $item->cover = $product->cover;
  157. $item->description = $product->description;
  158. return $item;
  159. });
  160. }
  161. }