-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from binafy/add-driver-for-store-cart
[1.x] Add CartManager
- Loading branch information
Showing
14 changed files
with
548 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,11 @@ | |
*/ | ||
'table' => 'cart_items', | ||
], | ||
|
||
/* | ||
* Driver | ||
*/ | ||
'driver' => [ | ||
'default' => 'database', | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.