Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Add CartManager #17

Merged
merged 32 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ff81549
Create LaravelCart.php
milwad-dev Jul 7, 2024
9a29236
wip
milwad-dev Jul 7, 2024
973e11f
Update LaravelCartManager.php
milwad-dev Jul 7, 2024
c1b7a3a
Update laravel-cart.php
milwad-dev Jul 7, 2024
818ba92
Update LaravelCartManager.php
milwad-dev Jul 7, 2024
f417b90
Create LaravelCartDatabase.php
milwad-dev Jul 7, 2024
87c992f
Create Driver.php
milwad-dev Jul 7, 2024
4b3bb90
Update LaravelCartDatabase.php
milwad-dev Jul 7, 2024
c4678d2
Update LaravelCartServiceProvider.php
milwad-dev Jul 7, 2024
1f2755e
wip
milwad-dev Jul 7, 2024
d2a230a
chrore: move tests
milwad-dev Jul 7, 2024
825f8a2
add CartStoreTest
milwad-dev Jul 7, 2024
784cd6e
add `can store products in cart with facade` test
milwad-dev Jul 7, 2024
cae9025
wip
milwad-dev Jul 7, 2024
c35084a
Create CartUpdateQuantityTest.php
milwad-dev Jul 7, 2024
39c689b
add `can decrease quantity of the item in cart with facade` test
milwad-dev Jul 7, 2024
ffe3efb
wip
milwad-dev Jul 7, 2024
87403fe
create CartDeleteTest
milwad-dev Jul 7, 2024
5269271
optimize tests
milwad-dev Jul 8, 2024
8b6fc22
add `can not increase quantity of the item in cart with facade when i…
milwad-dev Jul 8, 2024
d71c0b3
add `can not decrease quantity of the item in cart with facade when i…
milwad-dev Jul 8, 2024
0426d77
chrore
milwad-dev Jul 8, 2024
d9a39ae
Update CartStoreTest.php
milwad-dev Jul 8, 2024
024b523
wip
milwad-dev Jul 8, 2024
6a54d47
add driver methods to LaravelCart facade
milwad-dev Jul 8, 2024
71b281d
Update LaravelCart.php
milwad-dev Jul 8, 2024
c5e34a5
fix pint
milwad-dev Jul 8, 2024
f120290
Update README.md
milwad-dev Jul 8, 2024
0221b3f
Update README.md
milwad-dev Jul 10, 2024
b26db17
Update README.md
milwad-dev Jul 10, 2024
f5e02a1
Update README.md
milwad-dev Jul 10, 2024
5c3539b
Update README.md
milwad-dev Jul 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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