123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace App\Shop\Carts\Repositories;
- use Jsdecena\Baserepo\BaseRepository;
- use App\Shop\Carts\Exceptions\ProductInCartNotFoundException;
- use App\Shop\Carts\Repositories\Interfaces\CartRepositoryInterface;
- use App\Shop\Carts\ShoppingCart;
- use App\Shop\Couriers\Courier;
- use App\Shop\Customers\Customer;
- use App\Shop\Products\Product;
- use App\Shop\Products\Repositories\ProductRepository;
- use Gloudemans\Shoppingcart\Cart;
- use Gloudemans\Shoppingcart\CartItem;
- use Gloudemans\Shoppingcart\Exceptions\InvalidRowIDException;
- use Illuminate\Support\Collection;
- class CartRepository extends BaseRepository implements CartRepositoryInterface
- {
-
- public function __construct(ShoppingCart $cart)
- {
- $this->model = $cart;
- }
-
- public function addToCart(Product $product, int $int, $options = []) : CartItem
- {
- return $this->model->add($product, $int, $options);
- }
-
- public function getCartItems() : Collection
- {
- return $this->model->content();
- }
-
- public function removeToCart(string $rowId)
- {
- try {
- $this->model->remove($rowId);
- } catch (InvalidRowIDException $e) {
- throw new ProductInCartNotFoundException('Product in cart not found.');
- }
- }
-
- public function countItems() : int
- {
- return $this->model->count();
- }
-
- public function getSubTotal(int $decimals = 2)
- {
- return $this->model->subtotal($decimals, '.', '');
- }
-
- public function getTotal(int $decimals = 2, $shipping = 0.00)
- {
- return $this->model->total($decimals, '.', '', $shipping);
- }
-
- public function updateQuantityInCart(string $rowId, int $quantity) : CartItem
- {
- return $this->model->update($rowId, $quantity);
- }
-
- public function findItem(string $rowId) : CartItem
- {
- return $this->model->get($rowId);
- }
-
- public function getTax(int $decimals = 2)
- {
- return $this->model->tax($decimals);
- }
-
- public function getShippingFee(Courier $courier)
- {
- return number_format($courier->cost, 2);
- }
-
- public function clearCart()
- {
- $this->model->destroy();
- }
-
- public function saveCart(Customer $customer, $instance = 'default')
- {
- $this->model->instance($instance)->store($customer->email);
- }
-
- public function openCart(Customer $customer, $instance = 'default')
- {
- $this->model->instance($instance)->restore($customer->email);
- return $this->model;
- }
-
- public function getCartItemsTransformed() : Collection
- {
- return $this->getCartItems()->map(function ($item) {
- $productRepo = new ProductRepository(new Product());
- $product = $productRepo->findProductById($item->id);
- $item->product = $product;
- $item->cover = $product->cover;
- $item->description = $product->description;
- return $item;
- });
- }
- }
|