Skip to content

Commit

Permalink
Merge pull request #17 from binafy/add-driver-for-store-cart
Browse files Browse the repository at this point in the history
[1.x] Add CartManager
  • Loading branch information
milwad-dev authored Jul 10, 2024
2 parents f55bcac + 5c3539b commit 271ffb5
Show file tree
Hide file tree
Showing 14 changed files with 548 additions and 12 deletions.
65 changes: 56 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
- [Publish](#publish)
- [Usage](#usage)
- [Configuration](#configuration)
- [Store Cart](#store-cart)
- [Access Itemable](#access-itemable)
- [Create Cart With Storing Items](#create-cart-with-storing-item)
- [Store multiple items](#store-multiple-items)
- [Store Item For a Cart](#store-item-for-a-cart)
- [Delete Item From Cart](#delete-item-from-cart)
- [Delete All Items From Cart](#delete-all-items-from-cart)
- [Increase Quantity](#increase-quantity)
- [Decrease Quantity](#decrease-quantity)
- [Laravel Cart Facade](#laravel-cart-facade)
- [Driver](#driver)
- [Support Drivers](#support-drivers)
- [Laravel Cart Model](#laravel-cart-model)
- [Store Cart](#store-cart)
- [Access Itemable](#access-itemable)
- [Create Cart With Storing Items](#create-cart-with-storing-item)
- [Store multiple items](#store-multiple-items)
- [Store Item For a Cart](#store-item-for-a-cart)
- [Delete Item From Cart](#delete-item-from-cart)
- [Delete All Items From Cart](#delete-all-items-from-cart)
- [Increase Quantity](#increase-quantity)
- [Decrease Quantity](#decrease-quantity)
- [Contributors](#contributors)
- [Security](#security)
- [Changelog](#changelog)
Expand Down Expand Up @@ -87,6 +91,49 @@ After publishing, run the `php artisan migrate` command.

You can config the `Laravel Cart` with `laravel-cart.php` config that exists in `config` folder.

<a name="laravel-cart-facade"></a>
### Laravel Cart Facade

For convenience, you can use Laravel Cart facade to store, delete, and ...:

```php
<?php

use Binafy\LaravelCart\LaravelCart;

LaravelCart::driver('session')->storeItem($item, $userId|null);
LaravelCart::storeItem($item $userId|null);
```

<a name="driver"></a>
### Driver

If you may to using Laravel Cart facade, you can change the driver for store, delete, and ...:

```php
<?php

use Binafy\LaravelCart\LaravelCart;

LaravelCart::driver('database')->storeItem($item, $userId|null);
LaravelCart::driver('session')->removeItem($item);
```

> The default driver is `database` and if you would to change the driver, you need to use the Laravel Cart config file that exists on `config\laravel-cart.php`.
<a name="support-drivers"></a>
### Support Drivers

| Drivers | Name |
|----------|----------|
| Session | session |
| Database | database |

<a name="laravel-cart-model"></a>
### Laravel Cart Model

Also, you are able to use Laravel Cart models for fetch or ... with Laravel Eloquent.

<a name="store-cart"></a>
### Store Cart

Expand Down
7 changes: 7 additions & 0 deletions config/laravel-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@
*/
'table' => 'cart_items',
],

/*
* Driver
*/
'driver' => [
'default' => 'database',
],
];
20 changes: 20 additions & 0 deletions src/Drivers/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Binafy\LaravelCart\Drivers;

use Illuminate\Database\Eloquent\Model;

interface Driver
{
public function storeItem(Model|array $item, ?int $userId = null): Driver;

public function storeItems(array $items): Driver;

public function increaseQuantity(Model $item, int $quantity = 1): Driver;

public function decreaseQuantity(Model $item, int $quantity = 1): Driver;

public function removeItem(Model $item): Driver;

public function emptyCart(): Driver;
}
95 changes: 95 additions & 0 deletions src/Drivers/LaravelCartDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Binafy\LaravelCart\Drivers;

use Binafy\LaravelCart\Models\Cart;
use Illuminate\Database\Eloquent\Model;

class LaravelCartDatabase implements Driver
{
/**
* Store item in cart.
*/
public function storeItem(Model|array $item, ?int $userId = null): static
{
if (is_null($userId)) {
$userId = auth()->id();
}

$cart = Cart::query()->firstOrCreate(['user_id' => $userId]);
$cart->storeItem($item);

return $this;
}

/**
* Store multiple items in cart.
*/
public function storeItems(array $items): static
{
$cart = Cart::query()->firstOrCreate(['user_id' => auth()->id()]);
$cart->storeItems($items);

return $this;
}

/**
* Increase the quantity of the item.
*/
public function increaseQuantity(Model $item, int $quantity = 1): static
{
$cart = Cart::query()->firstOrCreate(['user_id' => auth()->id()]);
$item = $cart->items()->firstWhere('itemable_id', $item->getKey());

if (! $item) {
throw new \RuntimeException('The item not found');
}

$item->increment('quantity', $quantity);

return $this;
}

/**
* Decrease the quantity of the item.
*/
public function decreaseQuantity(Model $item, int $quantity = 1): static
{
$cart = Cart::query()->firstOrCreate(['user_id' => auth()->id()]);
$item = $cart->items()->firstWhere('itemable_id', $item->getKey());

if (! $item) {
throw new \RuntimeException('The item not found');
}

$item->decrement('quantity', $quantity);

return $this;
}

/**
* Remove a single item from the cart
*/
public function removeItem(Model $item): static
{
$cart = Cart::query()->firstOrCreate(['user_id' => auth()->id()]);
$itemToDelete = $cart->items()->find($item->getKey());

if ($itemToDelete) {
$itemToDelete->delete();
}

return $this;
}

/**
* Remove every item from the cart
*/
public function emptyCart(): static
{
$cart = Cart::query()->firstOrCreate(['user_id' => auth()->id()]);
$cart->emptyCart();

return $this;
}
}
29 changes: 29 additions & 0 deletions src/LaravelCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Binafy\LaravelCart;

use Illuminate\Support\Facades\Facade;

/**
* @method static \Binafy\LaravelCart\Drivers\Driver driver(string|null $driver = null)
* @method static \Binafy\LaravelCart\Drivers\Driver storeItem(\Illuminate\Database\Eloquent\Model|array $item, int|null $userId = null)
* @method static \Binafy\LaravelCart\Drivers\Driver storeItems(array $items)
* @method static \Binafy\LaravelCart\Drivers\Driver increaseQuantity(\Illuminate\Database\Eloquent\Model $item, int $quantity = 1)
* @method static \Binafy\LaravelCart\Drivers\Driver decreaseQuantity(\Illuminate\Database\Eloquent\Model $item, int $quantity = 1)
* @method static \Binafy\LaravelCart\Drivers\Driver removeItem(\Illuminate\Database\Eloquent\Model $item)
* @method static \Binafy\LaravelCart\Drivers\Driver emptyCart()
* @method static string getDefaultDriver()
* @method static void setDefaultDriver(string $name)
*
* @see \Binafy\LaravelCart\Manager\LaravelCartManager
*/
class LaravelCart extends Facade
{
/**
* Get the registered name of the component.
*/
protected static function getFacadeAccessor(): string
{
return 'laravel-cart';
}
}
25 changes: 25 additions & 0 deletions src/Manager/LaravelCartManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Binafy\LaravelCart\Manager;

use Binafy\LaravelCart\Drivers\LaravelCartDatabase;
use Illuminate\Support\Manager;

class LaravelCartManager extends Manager
{
/**
* Get the default driver.
*/
public function getDefaultDriver(): string
{
return $this->config->get('laravel-cart.driver.default');
}

/**
* The database driver of laravel cart.
*/
public function createDatabaseDriver(): LaravelCartDatabase
{
return new LaravelCartDatabase;
}
}
10 changes: 7 additions & 3 deletions src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function calculatedPriceByQuantity(): int
}

/**
* Store multiple items.
* Store multiple items in cart.
*/
public function storeItems(array $items): static
{
Expand All @@ -115,7 +115,11 @@ public function storeItem(Model|array $item): static
throw new \RuntimeException('The item must be an instance of Cartable');
}
} else {
$this->items()->save($item);
$this->items()->create([
'itemable_id' => $item->getKey(),
'itemable_type' => get_class($item),
'itemable_quantity' => 1,
]);
}

return $this;
Expand All @@ -138,7 +142,7 @@ public function removeItem(Model $item): static
/**
* Remove every item from the cart
*/
public function emptyCart(): Cart
public function emptyCart(): static
{
$this->items()->delete();

Expand Down
6 changes: 6 additions & 0 deletions src/Providers/LaravelCartServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Binafy\LaravelCart\Providers;

use Binafy\LaravelCart\Manager\LaravelCartManager;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class LaravelCartServiceProvider extends ServiceProvider
Expand All @@ -13,6 +15,10 @@ public function register(): void
{
$this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
$this->mergeConfigFrom(__DIR__.'/../../config/laravel-cart.php', 'laravel-cart');

$this->app->bind('laravel-cart', function (Application $app) {
return new LaravelCartManager($app);
});
}

/**
Expand Down
Loading

0 comments on commit 271ffb5

Please sign in to comment.