1 | <?php |
||
2 | |||
3 | namespace QuickCheckout; |
||
4 | |||
5 | use Illuminate\Session\Store; |
||
6 | use Illuminate\Support\Collection; |
||
7 | use QuickCheckout\Contracts\ProductInterface; |
||
8 | |||
9 | class Cart |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * Session store |
||
14 | */ |
||
15 | private Store $session; |
||
16 | |||
17 | /** |
||
18 | * Key name to save in session. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private string $sessionKey; |
||
23 | |||
24 | /** |
||
25 | * Cart validator |
||
26 | */ |
||
27 | private CartValidator $validator; |
||
28 | |||
29 | /** |
||
30 | * Cart line items. |
||
31 | * |
||
32 | * @var Collection |
||
33 | */ |
||
34 | protected Collection $lineItems; |
||
35 | |||
36 | /** |
||
37 | * @param Store $session |
||
38 | * @param string $sessionKey |
||
39 | * @param CartValidator $validator |
||
40 | */ |
||
41 | 5 | public function __construct(Store $session, string $sessionKey, CartValidator $validator) |
|
42 | { |
||
43 | 5 | $this->purge(); |
|
44 | 5 | $this->session = $session; |
|
45 | 5 | $this->sessionKey = $sessionKey; |
|
46 | 5 | $this->validator = $validator; |
|
47 | 5 | } |
|
48 | |||
49 | /** |
||
50 | * @return Store |
||
51 | */ |
||
52 | 1 | public function getSession(): Store |
|
53 | { |
||
54 | 1 | return $this->session; |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return string |
||
59 | */ |
||
60 | 1 | public function getSessionKey(): string |
|
61 | { |
||
62 | 1 | return $this->sessionKey; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return CartValidator |
||
67 | */ |
||
68 | 1 | public function getValidator(): CartValidator |
|
69 | { |
||
70 | 1 | return $this->validator; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param CartValidator $validator |
||
75 | * |
||
76 | * @return static |
||
77 | */ |
||
78 | 1 | public function setValidator(CartValidator $validator): static |
|
79 | { |
||
80 | 1 | $this->validator = $validator; |
|
81 | |||
82 | 1 | return $this; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Lofty add line item to cart. |
||
87 | * |
||
88 | * @param LineItem $lineItem |
||
89 | * @param bool $override |
||
90 | * |
||
91 | * @return static |
||
92 | * @throws \Exception |
||
93 | */ |
||
94 | 4 | public function addLineItem(LineItem $lineItem, bool $strict = false): static |
|
95 | { |
||
96 | 4 | if (!$this->validator->lineItemCanBeAdded($this, $lineItem)) { |
|
97 | 1 | if ($strict) { |
|
98 | 1 | throw new \Exception('Line item can;t be added'); |
|
99 | } else { |
||
100 | 1 | return $this; |
|
101 | } |
||
102 | } |
||
103 | 4 | $this->lineItems->push($lineItem); |
|
104 | |||
105 | 4 | return $this; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Crete line item from product |
||
110 | * @param array|ProductInterface $product |
||
111 | * @param int $quantity |
||
112 | * @param string|null $id |
||
113 | * |
||
114 | * @return $this |
||
115 | * @throws \Exception |
||
116 | */ |
||
117 | 3 | public function withLineItem(array|ProductInterface $product, int $quantity = 1, ?string $id = null): static |
|
118 | { |
||
119 | 3 | return $this->addLineItem(Checkout::makeLineItem($product, $quantity, $id)); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Convert class to array. |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 2 | public function toArray(): array |
|
128 | { |
||
129 | return [ |
||
130 | 2 | 'line_items' => $this->lineItems->map(fn (LineItem $i) => $i->toArray())->toArray(), |
|
131 | ]; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Initialise object from array. |
||
136 | */ |
||
137 | 2 | public function fromArray(array $cart): static |
|
138 | { |
||
139 | 2 | $this->purge(); |
|
140 | 2 | if (!empty($cart['line_items']) && is_array($cart['line_items'])) { |
|
141 | 2 | foreach ($cart['line_items'] as $data) { |
|
142 | 2 | if ($li = LineItem::fromArray($data)) { |
|
143 | 2 | $this->addLineItem($li, true); |
|
144 | } |
||
145 | } |
||
146 | } |
||
147 | |||
148 | 2 | return $this; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Put product to session. |
||
153 | */ |
||
154 | 2 | public function putToSession(): static |
|
155 | { |
||
156 | 2 | $this->session->put($this->sessionKey, $this->toArray()); |
|
157 | |||
158 | 2 | return $this; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get cart from session. |
||
163 | * |
||
164 | * @param bool $forget |
||
165 | * |
||
166 | * @return static|null |
||
167 | * @throws \Exception |
||
168 | */ |
||
169 | 3 | public function fromSession(bool $forget = false): ?static |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
170 | { |
||
171 | 3 | $cart = $this->session->get($this->sessionKey); |
|
172 | |||
173 | 3 | if ($forget) { |
|
174 | 1 | $this->session->forget($this->sessionKey); |
|
175 | } |
||
176 | 3 | if (is_array($cart)) { |
|
177 | 2 | return $this->fromArray($cart); |
|
178 | } |
||
179 | |||
180 | 1 | return null; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return bool |
||
185 | */ |
||
186 | 2 | public function isEmpty(): bool |
|
187 | { |
||
188 | 2 | return $this->lineItems->isEmpty(); |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return Collection |
||
193 | */ |
||
194 | 2 | public function lineItems(): Collection |
|
195 | { |
||
196 | 2 | return $this->lineItems; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return int |
||
201 | * @throws \Exception |
||
202 | */ |
||
203 | 1 | public function total(): int |
|
204 | { |
||
205 | 1 | return (int) $this->lineItems->reduce(fn ($carry, LineItem $item) => $carry + $item->total(), 0); |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Forget cart from session. |
||
210 | * |
||
211 | * @return static |
||
212 | */ |
||
213 | 1 | public function forget(): static |
|
214 | { |
||
215 | 1 | $this->session->forget($this->sessionKey); |
|
216 | |||
217 | 1 | return $this; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Forget cart from session. |
||
222 | * |
||
223 | * @return static |
||
224 | */ |
||
225 | 5 | public function purge(): static |
|
226 | { |
||
227 | 5 | $this->lineItems = collect(); |
|
228 | |||
229 | 5 | return $this; |
|
230 | } |
||
231 | } |
||
232 |