Skip to content

Commit

Permalink
Merge pull request #12 from binafy/add-store-item-method
Browse files Browse the repository at this point in the history
[1.x] Add store item method
  • Loading branch information
milwad-dev authored Jul 3, 2024
2 parents 8c07070 + e7cbbb5 commit ea921c5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
20 changes: 19 additions & 1 deletion src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,29 @@ public function calculatedPriceByQuantity(): int
public function storeItems(array $items): Cart
{
foreach ($items as $item) {
$this->storeItem($item);
}

return $this;
}

/**
* Store cart item in cart.
*/
public function storeItem(Model|array $item): Cart
{
if (is_array($item)) {
$item['itemable_id'] = $item['itemable']->getKey();
$item['itemable_type'] = get_class($item['itemable']);
$item['quantity'] = (int) $item['quantity'];

$this->items()->create($item);
if ($item['itemable'] instanceof Cartable) {
$this->items()->create($item);
} else {
throw new \RuntimeException('The item must be an instance of Cartable');
}
} else {
$this->items()->save($item);
}

return $this;
Expand Down
6 changes: 3 additions & 3 deletions src/Observers/CartItemObserve.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CartItemObserve
public function creating(CartItem $cartItem): void
{
if (! new $cartItem->itemable_type instanceof Cartable) {
throw new \Exception('The item must be an instance of Cartable');
throw new \RuntimeException('The item must be an instance of Cartable');
}
}

Expand All @@ -23,7 +23,7 @@ public function creating(CartItem $cartItem): void
public function updating(CartItem $cartItem): void
{
if (! new $cartItem->itemable_type instanceof Cartable) {
throw new \Exception('The item must be an instance of Cartable');
throw new \RuntimeException('The item must be an instance of Cartable');
}
}

Expand All @@ -33,7 +33,7 @@ public function updating(CartItem $cartItem): void
public function saving(CartItem $cartItem): void
{
if (! new $cartItem->itemable_type instanceof Cartable) {
throw new \Exception('The item must be an instance of Cartable');
throw new \RuntimeException('The item must be an instance of Cartable');
}
}
}
11 changes: 4 additions & 7 deletions tests/Feature/CartStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,11 @@

test('can not store product in cart when item is not instance of cartable', function () {
$user = User::query()->create(['name' => 'Milwad', 'email' => '[email protected]']);
$product1 = new class extends \Illuminate\Database\Eloquent\Model
{

};
$user2 = User::query()->create(['name' => 'Binafy', 'email' => '[email protected]']);

$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);

$item['itemable'] = $product1;
$item['itemable'] = $user2;
$item['quantity'] = 2;

$cart->storeItem($item);
Expand All @@ -200,8 +197,8 @@
assertDatabaseCount('carts', 1);
assertDatabaseCount('cart_items', 0);
assertDatabaseMissing('cart_items', [
'itemable_id' => $product1->id,
'itemable_type' => $product1::class,
'itemable_id' => $user2->id,
'itemable_type' => $user2::class,
'quantity' => 2,
]);
})->expectExceptionMessage('The item must be an instance of Cartable');

0 comments on commit ea921c5

Please sign in to comment.