Passed
Pull Request — main (#3)
by Carsten
04:00
created

Cart   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 56
ccs 9
cts 15
cp 0.6
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 5 1
A __construct() 0 5 1
A remove() 0 5 1
A update() 0 9 3
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\StorageInterface;
10
11
class Cart extends Basket
12
{
13
    /**
14
     * Instance of the event dispatcher.
15
     *
16
     * @var Dispatcher
17
     */
18
    private Dispatcher $events;
19
20 4
    public function __construct(StorageInterface $store, IdentifierInterface $identifier, Dispatcher $events)
21
    {
22 4
        $this->events = $events;
23
24 4
        parent::__construct($store, $identifier);
25
    }
26
27
    /**
28
     * Update an item.
29
     *
30
     * @param string $itemIdentifier The unique item identifier
31
     * @param mixed $key The key to update, or an array of key-value pairs
32
     * @param mixed $value The value to set $key to
33
     */
34
    public function update(string $itemIdentifier, $key, $value = null): void
35
    {
36
        /** @var Item $item */
37
        foreach ($this->contents() as $item) {
38
            if ($item->identifier == $itemIdentifier) {
39
                $item->update($key, $value);
40
                $this->events->dispatch('cart.updated', $this->item($itemIdentifier));
41
42
                break;
43
            }
44
        }
45
    }
46
47
    /**
48
     * Remove an item from the basket.
49
     *
50
     * @param string $identifier Unique item identifier
51
     */
52 1
    public function remove(string $identifier): void
53
    {
54 1
        $this->events->dispatch('cart.removed', $this->item($identifier));
55
56 1
        $this->store->remove($identifier);
57
    }
58
59
    /**
60
     * Destroy/empty the basket.
61
     */
62 1
    public function destroy(): void
63
    {
64 1
        $this->store->destroy();
65
66 1
        $this->events->dispatch('cart.destroyed', []);
67
    }
68
}
69