Skip to content

Commit 271ffb5

Browse files
authored
Merge pull request #17 from binafy/add-driver-for-store-cart
[1.x] Add CartManager
2 parents f55bcac + 5c3539b commit 271ffb5

File tree

14 files changed

+548
-12
lines changed

14 files changed

+548
-12
lines changed

README.md

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@
1414
- [Publish](#publish)
1515
- [Usage](#usage)
1616
- [Configuration](#configuration)
17-
- [Store Cart](#store-cart)
18-
- [Access Itemable](#access-itemable)
19-
- [Create Cart With Storing Items](#create-cart-with-storing-item)
20-
- [Store multiple items](#store-multiple-items)
21-
- [Store Item For a Cart](#store-item-for-a-cart)
22-
- [Delete Item From Cart](#delete-item-from-cart)
23-
- [Delete All Items From Cart](#delete-all-items-from-cart)
24-
- [Increase Quantity](#increase-quantity)
25-
- [Decrease Quantity](#decrease-quantity)
17+
- [Laravel Cart Facade](#laravel-cart-facade)
18+
- [Driver](#driver)
19+
- [Support Drivers](#support-drivers)
20+
- [Laravel Cart Model](#laravel-cart-model)
21+
- [Store Cart](#store-cart)
22+
- [Access Itemable](#access-itemable)
23+
- [Create Cart With Storing Items](#create-cart-with-storing-item)
24+
- [Store multiple items](#store-multiple-items)
25+
- [Store Item For a Cart](#store-item-for-a-cart)
26+
- [Delete Item From Cart](#delete-item-from-cart)
27+
- [Delete All Items From Cart](#delete-all-items-from-cart)
28+
- [Increase Quantity](#increase-quantity)
29+
- [Decrease Quantity](#decrease-quantity)
2630
- [Contributors](#contributors)
2731
- [Security](#security)
2832
- [Changelog](#changelog)
@@ -87,6 +91,49 @@ After publishing, run the `php artisan migrate` command.
8791

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

94+
<a name="laravel-cart-facade"></a>
95+
### Laravel Cart Facade
96+
97+
For convenience, you can use Laravel Cart facade to store, delete, and ...:
98+
99+
```php
100+
<?php
101+
102+
use Binafy\LaravelCart\LaravelCart;
103+
104+
LaravelCart::driver('session')->storeItem($item, $userId|null);
105+
LaravelCart::storeItem($item $userId|null);
106+
```
107+
108+
<a name="driver"></a>
109+
### Driver
110+
111+
If you may to using Laravel Cart facade, you can change the driver for store, delete, and ...:
112+
113+
```php
114+
<?php
115+
116+
use Binafy\LaravelCart\LaravelCart;
117+
118+
LaravelCart::driver('database')->storeItem($item, $userId|null);
119+
LaravelCart::driver('session')->removeItem($item);
120+
```
121+
122+
> 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`.
123+
124+
<a name="support-drivers"></a>
125+
### Support Drivers
126+
127+
| Drivers | Name |
128+
|----------|----------|
129+
| Session | session |
130+
| Database | database |
131+
132+
<a name="laravel-cart-model"></a>
133+
### Laravel Cart Model
134+
135+
Also, you are able to use Laravel Cart models for fetch or ... with Laravel Eloquent.
136+
90137
<a name="store-cart"></a>
91138
### Store Cart
92139

config/laravel-cart.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,11 @@
4646
*/
4747
'table' => 'cart_items',
4848
],
49+
50+
/*
51+
* Driver
52+
*/
53+
'driver' => [
54+
'default' => 'database',
55+
],
4956
];

src/Drivers/Driver.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Binafy\LaravelCart\Drivers;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
interface Driver
8+
{
9+
public function storeItem(Model|array $item, ?int $userId = null): Driver;
10+
11+
public function storeItems(array $items): Driver;
12+
13+
public function increaseQuantity(Model $item, int $quantity = 1): Driver;
14+
15+
public function decreaseQuantity(Model $item, int $quantity = 1): Driver;
16+
17+
public function removeItem(Model $item): Driver;
18+
19+
public function emptyCart(): Driver;
20+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Binafy\LaravelCart\Drivers;
4+
5+
use Binafy\LaravelCart\Models\Cart;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class LaravelCartDatabase implements Driver
9+
{
10+
/**
11+
* Store item in cart.
12+
*/
13+
public function storeItem(Model|array $item, ?int $userId = null): static
14+
{
15+
if (is_null($userId)) {
16+
$userId = auth()->id();
17+
}
18+
19+
$cart = Cart::query()->firstOrCreate(['user_id' => $userId]);
20+
$cart->storeItem($item);
21+
22+
return $this;
23+
}
24+
25+
/**
26+
* Store multiple items in cart.
27+
*/
28+
public function storeItems(array $items): static
29+
{
30+
$cart = Cart::query()->firstOrCreate(['user_id' => auth()->id()]);
31+
$cart->storeItems($items);
32+
33+
return $this;
34+
}
35+
36+
/**
37+
* Increase the quantity of the item.
38+
*/
39+
public function increaseQuantity(Model $item, int $quantity = 1): static
40+
{
41+
$cart = Cart::query()->firstOrCreate(['user_id' => auth()->id()]);
42+
$item = $cart->items()->firstWhere('itemable_id', $item->getKey());
43+
44+
if (! $item) {
45+
throw new \RuntimeException('The item not found');
46+
}
47+
48+
$item->increment('quantity', $quantity);
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* Decrease the quantity of the item.
55+
*/
56+
public function decreaseQuantity(Model $item, int $quantity = 1): static
57+
{
58+
$cart = Cart::query()->firstOrCreate(['user_id' => auth()->id()]);
59+
$item = $cart->items()->firstWhere('itemable_id', $item->getKey());
60+
61+
if (! $item) {
62+
throw new \RuntimeException('The item not found');
63+
}
64+
65+
$item->decrement('quantity', $quantity);
66+
67+
return $this;
68+
}
69+
70+
/**
71+
* Remove a single item from the cart
72+
*/
73+
public function removeItem(Model $item): static
74+
{
75+
$cart = Cart::query()->firstOrCreate(['user_id' => auth()->id()]);
76+
$itemToDelete = $cart->items()->find($item->getKey());
77+
78+
if ($itemToDelete) {
79+
$itemToDelete->delete();
80+
}
81+
82+
return $this;
83+
}
84+
85+
/**
86+
* Remove every item from the cart
87+
*/
88+
public function emptyCart(): static
89+
{
90+
$cart = Cart::query()->firstOrCreate(['user_id' => auth()->id()]);
91+
$cart->emptyCart();
92+
93+
return $this;
94+
}
95+
}

src/LaravelCart.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Binafy\LaravelCart;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static \Binafy\LaravelCart\Drivers\Driver driver(string|null $driver = null)
9+
* @method static \Binafy\LaravelCart\Drivers\Driver storeItem(\Illuminate\Database\Eloquent\Model|array $item, int|null $userId = null)
10+
* @method static \Binafy\LaravelCart\Drivers\Driver storeItems(array $items)
11+
* @method static \Binafy\LaravelCart\Drivers\Driver increaseQuantity(\Illuminate\Database\Eloquent\Model $item, int $quantity = 1)
12+
* @method static \Binafy\LaravelCart\Drivers\Driver decreaseQuantity(\Illuminate\Database\Eloquent\Model $item, int $quantity = 1)
13+
* @method static \Binafy\LaravelCart\Drivers\Driver removeItem(\Illuminate\Database\Eloquent\Model $item)
14+
* @method static \Binafy\LaravelCart\Drivers\Driver emptyCart()
15+
* @method static string getDefaultDriver()
16+
* @method static void setDefaultDriver(string $name)
17+
*
18+
* @see \Binafy\LaravelCart\Manager\LaravelCartManager
19+
*/
20+
class LaravelCart extends Facade
21+
{
22+
/**
23+
* Get the registered name of the component.
24+
*/
25+
protected static function getFacadeAccessor(): string
26+
{
27+
return 'laravel-cart';
28+
}
29+
}

src/Manager/LaravelCartManager.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Binafy\LaravelCart\Manager;
4+
5+
use Binafy\LaravelCart\Drivers\LaravelCartDatabase;
6+
use Illuminate\Support\Manager;
7+
8+
class LaravelCartManager extends Manager
9+
{
10+
/**
11+
* Get the default driver.
12+
*/
13+
public function getDefaultDriver(): string
14+
{
15+
return $this->config->get('laravel-cart.driver.default');
16+
}
17+
18+
/**
19+
* The database driver of laravel cart.
20+
*/
21+
public function createDatabaseDriver(): LaravelCartDatabase
22+
{
23+
return new LaravelCartDatabase;
24+
}
25+
}

src/Models/Cart.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function calculatedPriceByQuantity(): int
8888
}
8989

9090
/**
91-
* Store multiple items.
91+
* Store multiple items in cart.
9292
*/
9393
public function storeItems(array $items): static
9494
{
@@ -115,7 +115,11 @@ public function storeItem(Model|array $item): static
115115
throw new \RuntimeException('The item must be an instance of Cartable');
116116
}
117117
} else {
118-
$this->items()->save($item);
118+
$this->items()->create([
119+
'itemable_id' => $item->getKey(),
120+
'itemable_type' => get_class($item),
121+
'itemable_quantity' => 1,
122+
]);
119123
}
120124

121125
return $this;
@@ -138,7 +142,7 @@ public function removeItem(Model $item): static
138142
/**
139143
* Remove every item from the cart
140144
*/
141-
public function emptyCart(): Cart
145+
public function emptyCart(): static
142146
{
143147
$this->items()->delete();
144148

src/Providers/LaravelCartServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Binafy\LaravelCart\Providers;
44

5+
use Binafy\LaravelCart\Manager\LaravelCartManager;
6+
use Illuminate\Foundation\Application;
57
use Illuminate\Support\ServiceProvider;
68

79
class LaravelCartServiceProvider extends ServiceProvider
@@ -13,6 +15,10 @@ public function register(): void
1315
{
1416
$this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
1517
$this->mergeConfigFrom(__DIR__.'/../../config/laravel-cart.php', 'laravel-cart');
18+
19+
$this->app->bind('laravel-cart', function (Application $app) {
20+
return new LaravelCartManager($app);
21+
});
1622
}
1723

1824
/**

0 commit comments

Comments
 (0)