Passed
Push — main ( 94bef5...f87ad3 )
by Carsten
09:09 queued 05:14
created

Cart::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Lenius\LaravelEcommerce;
4
5
use Illuminate\Contracts\Events\Dispatcher;
6
use Lenius\Basket\Basket;
7
use Lenius\Basket\IdentifierInterface;
8
use Lenius\Basket\Item;
9
use Lenius\Basket\ItemInterface;
10
use Lenius\Basket\StorageInterface;
11
use Lenius\LaravelEcommerce\Events\CartDestroyed;
12
use Lenius\LaravelEcommerce\Events\CartItemDecreased;
13
use Lenius\LaravelEcommerce\Events\CartItemIncremented;
14
use Lenius\LaravelEcommerce\Events\CartItemRemoved;
15
use Lenius\LaravelEcommerce\Events\CartItemUpdated;
16
17
class Cart extends Basket
18
{
19
    /**
20
     * Instance of the event dispatcher.
21
     *
22
     * @var Dispatcher
23
     */
24
    private Dispatcher $events;
25
26 5
    public function __construct(StorageInterface $store, IdentifierInterface $identifier, Dispatcher $events)
27
    {
28 5
        $this->events = $events;
29
30 5
        parent::__construct($store, $identifier);
31
    }
32
33
    /**
34
     * Update an item.
35
     *
36
     * @param string $itemIdentifier The unique item identifier
37
     * @param mixed $key The key to update, or an array of key-value pairs
38
     * @param mixed $value The value to set $key to
39
     */
40
    public function update(string $itemIdentifier, $key, $value = null): void
41
    {
42
        /** @var Item $item */
43
        foreach ($this->contents() as $item) {
44
            if ($item->identifier == $itemIdentifier) {
45
                $item->update($key, $value);
46
                $this->events->dispatch(new CartItemUpdated($this->item($itemIdentifier)));
47
48
                break;
49
            }
50
        }
51
    }
52
53
    /**
54
     * Remove an item from the cart.
55
     *
56
     * @param string $identifier Unique item identifier
57
     */
58 1
    public function remove(string $identifier): void
59
    {
60 1
        $this->events->dispatch(new CartItemRemoved($this->item($identifier)));
61
62 1
        $this->store->remove($identifier);
63
    }
64
65
    /**
66
     * Destroy/empty the basket.
67
     */
68 2
    public function destroy(): void
69
    {
70 2
        $this->store->destroy();
71
72 2
        $this->events->dispatch(new CartDestroyed());
73
    }
74
75
    /**
76
     * Increment an item from the cart.
77
     *
78
     * @param string $itemIdentifier Unique item identifier
79
     * @return ItemInterface|bool
80
     */
81 1
    public function inc(string $itemIdentifier): ItemInterface|bool
82
    {
83
        /** @var ItemInterface $item */
84 1
        if ($item = Cart::item($itemIdentifier)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Lenius\Basket\Basket::item() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        if ($item = Cart::/** @scrutinizer ignore-call */ item($itemIdentifier)) {
Loading history...
85 1
            $item->quantity++;
86 1
            $this->events->dispatch(new CartItemIncremented($this->item($itemIdentifier)));
87
88 1
            return $item;
89
        }
90
91
        return false;
92
    }
93
94
    /**
95
     * Decrement an item from the cart.
96
     *
97
     * @param string $itemIdentifier Unique item identifier
98
     * @return ItemInterface|bool
99
     */
100 1
    public function dec(string $itemIdentifier): ItemInterface|bool
101
    {
102
        /** @var ItemInterface $item */
103 1
        if ($item = Cart::item($itemIdentifier)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Lenius\Basket\Basket::item() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        if ($item = Cart::/** @scrutinizer ignore-call */ item($itemIdentifier)) {
Loading history...
104 1
            if ($item->quantity > 1) {
105
                $item->quantity--;
106
                $this->events->dispatch(new CartItemDecreased($this->item($itemIdentifier)));
107
                return $item;
108
            } else {
109 1
                $this->events->dispatch(new CartItemRemoved($item));
110 1
                Cart::remove($itemIdentifier);
0 ignored issues
show
Bug Best Practice introduced by
The method Lenius\LaravelEcommerce\Cart::remove() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
                Cart::/** @scrutinizer ignore-call */ 
111
                      remove($itemIdentifier);
Loading history...
111
            }
112
        }
113
114 1
        return false;
115
    }
116
}
117