|
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.destroy', []); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|