• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

conedevelopment / bazar / 20259690714

16 Dec 2025 07:15AM UTC coverage: 62.261% (-1.2%) from 63.48%
20259690714

Pull #235

github

web-flow
Merge 839eb6e7a into 090ff8496
Pull Request #235: Coupons & Discounts

78 of 147 new or added lines in 18 files covered. (53.06%)

14 existing lines in 3 files now uncovered.

1008 of 1619 relevant lines covered (62.26%)

14.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.42
/src/Cart/Driver.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Cone\Bazar\Cart;
6

7
use Cone\Bazar\Bazar;
8
use Cone\Bazar\Exceptions\CartException;
9
use Cone\Bazar\Gateway\Response;
10
use Cone\Bazar\Interfaces\Buyable;
11
use Cone\Bazar\Models\Address;
12
use Cone\Bazar\Models\Cart;
13
use Cone\Bazar\Models\Item;
14
use Cone\Bazar\Models\Shipping;
15
use Cone\Bazar\Support\Facades\Gateway;
16
use Illuminate\Http\Request;
17
use Illuminate\Support\Collection;
18
use Illuminate\Support\Facades\App;
19

20
abstract class Driver
21
{
22
    /**
23
     * The driver config.
24
     */
25
    protected array $config = [];
26

27
    /**
28
     * The cart instance.
29
     */
30
    protected ?Cart $cart = null;
31

32
    /**
33
     * Create a new driver instance.
34
     */
35
    public function __construct(array $config = [])
36
    {
37
        $this->config = $config;
14✔
38
    }
39

40
    /**
41
     * Resolve the cart instance.
42
     */
43
    abstract protected function resolve(Request $request): Cart;
44

45
    /**
46
     * The callback after the cart instance is resolved.
47
     */
48
    protected function resolved(Request $request, Cart $cart): void
49
    {
50
        if (! $cart->exists || ($request->user() && $cart->user_id !== $request->user()->getKey())) {
14✔
51
            $cart->user()->associate($request->user())->save();
14✔
52
        }
53

54
        $cart->loadMissing(['items', 'items.buyable']);
14✔
55
    }
56

57
    /**
58
     * Get the cart model.
59
     */
60
    public function getModel(): Cart
61
    {
62
        if (is_null($this->cart)) {
14✔
63
            $this->cart = App::call(function (Request $request): Cart {
14✔
64
                return tap($this->resolve($request), function (Cart $cart) use ($request): void {
14✔
65
                    $this->resolved($request, $cart);
14✔
66
                });
14✔
67
            });
14✔
68
        }
69

70
        return tap($this->cart, static function (Cart $cart): void {
14✔
71
            if (! $cart->locked && $cart->currency !== Bazar::getCurrency()) {
14✔
UNCOV
72
                $cart->setAttribute('currency', Bazar::getCurrency());
×
73
                $cart->syncItems();
×
74
                $cart->shipping->calculateFee();
×
75
                $cart->shipping->calculateTaxes();
×
76
            }
77
        });
14✔
78
    }
79

80
    /**
81
     * Get the item with the given id.
82
     */
83
    public function getItem(string $id): ?Item
84
    {
85
        return $this->getItems()->firstWhere('id', $id);
2✔
86
    }
87

88
    /**
89
     * Add the product with the given properties to the cart.
90
     */
91
    public function addItem(Buyable $buyable, float $quantity = 1, array $properties = []): Item
92
    {
93
        if (! $buyable->buyable($this->getModel())) {
14✔
UNCOV
94
            throw new CartException(sprintf('Unable to add [%s] item to the cart.', get_class($buyable)));
×
95
        }
96

97
        $item = $buyable->toItem(
14✔
98
            $this->getModel(),
14✔
99
            ['quantity' => $quantity, 'properties' => $properties]
14✔
100
        );
14✔
101

102
        $this->getModel()->mergeItem($item)->save();
14✔
103

104
        $this->sync();
14✔
105

106
        return $item;
14✔
107
    }
108

109
    /**
110
     * Remove the given cart item.
111
     */
112
    public function removeItem(string $id): void
113
    {
114
        if ($item = $this->getItem($id)) {
1✔
115
            $key = $this->getItems()->search(static function (Item $item) use ($id) {
1✔
116
                return $item->getKey() === $id;
1✔
117
            });
1✔
118

119
            $item->delete();
1✔
120

121
            $this->getItems()->forget($key);
1✔
122

123
            $this->sync();
1✔
124
        }
125
    }
126

127
    /**
128
     * Remove the given cart items.
129
     */
130
    public function removeItems(array $ids): void
131
    {
132
        $count = $this->getModel()->items()->whereIn('id', $ids)->delete();
1✔
133

134
        if ($count > 0) {
1✔
135
            $keys = $this->getItems()->reduce(static function (array $keys, Item $item, int $key) use ($ids): array {
1✔
136
                return in_array($item->getKey(), $ids) ? array_merge($keys, [$key]) : $keys;
1✔
137
            }, []);
1✔
138

139
            $this->getItems()->forget($keys);
1✔
140

141
            $this->sync();
1✔
142
        }
143
    }
144

145
    /**
146
     * Update the given cart item.
147
     */
148
    public function updateItem(string $id, array $properties = []): void
149
    {
150
        if ($item = $this->getItem($id)) {
1✔
151
            $item->fill($properties)->save();
1✔
152
            $item->calculateTaxes();
1✔
153

154
            $this->sync();
1✔
155
        }
156
    }
157

158
    /**
159
     * Update the given cart items.
160
     */
161
    public function updateItems(array $data): void
162
    {
163
        $items = $this->getItems()->whereIn('id', array_keys($data));
1✔
164

165
        $items->each(static function (Item $item) use ($data): void {
1✔
166
            $item->fill($data[$item->getKey()])->save();
1✔
167
            $item->calculateTaxes();
1✔
168
        });
1✔
169

170
        if ($items->isNotEmpty()) {
1✔
171
            $this->sync();
1✔
172
        }
173
    }
174

175
    /**
176
     * Get the cart items.
177
     */
178
    public function getItems(): Collection
179
    {
180
        return $this->getModel()->getItems();
4✔
181
    }
182

183
    /**
184
     * Get the billing address that belongs to the cart.
185
     */
186
    public function getBilling(): Address
187
    {
188
        return $this->getModel()->address;
2✔
189
    }
190

191
    /**
192
     * Update the billing address.
193
     */
194
    public function updateBilling(array $attributes): void
195
    {
196
        $this->getBilling()->fill($attributes)->save();
1✔
197

198
        $this->sync();
1✔
199
    }
200

201
    /**
202
     * Get the shipping that belongs to the cart.
203
     */
204
    public function getShipping(): Shipping
205
    {
206
        return $this->getModel()->shipping;
14✔
207
    }
208

209
    /**
210
     * Update the shipping address and driver.
211
     */
212
    public function updateShipping(array $attributes = [], ?string $driver = null): void
213
    {
214
        if (! is_null($driver)) {
1✔
215
            $this->getShipping()->setAttribute('driver', $driver);
1✔
216
        }
217

218
        $this->getShipping()->address->fill($attributes);
1✔
219

220
        if (! empty($attributes) || ! is_null($driver)) {
1✔
221
            $this->sync();
1✔
222
        }
223

224
        $this->getShipping()->address->save();
1✔
225
    }
226

227
    /**
228
     * Empty the cart.
229
     */
230
    public function empty(): void
231
    {
232
        $this->getModel()->items()->delete();
1✔
233
        $this->getModel()->setRelation('items', $this->getModel()->items()->getRelated()->newCollection());
1✔
234

235
        $this->getShipping()->update(['fee' => 0]);
1✔
236
        $this->getShipping()->taxes()->delete();
1✔
237
    }
238

239
    /**
240
     * Get the number of the cart items.
241
     */
242
    public function count(): float
243
    {
244
        return $this->getItems()->sum('quantity');
3✔
245
    }
246

247
    /**
248
     * Determine if the cart is empty.
249
     */
250
    public function isEmpty(): bool
251
    {
252
        return $this->getItems()->isEmpty();
1✔
253
    }
254

255
    /**
256
     * Perform the checkout using the given driver.
257
     */
258
    public function checkout(string $driver): Response
259
    {
260
        return App::call(function (Request $request) use ($driver): Response {
1✔
261
            return Gateway::driver($driver)->handleCheckout($request, $this->getModel()->toOrder());
1✔
262
        });
1✔
263
    }
264

265
    /**
266
     * Determine if the cart is not empty.
267
     */
268
    public function isNotEmpty(): bool
269
    {
270
        return ! $this->isEmpty();
1✔
271
    }
272

273
    /**
274
     * Sync the cart.
275
     */
276
    public function sync(): void
277
    {
278
        $this->getShipping()->calculateFee();
14✔
279

280
        $this->getModel()->calculateTax();
14✔
281
    }
282

283
    /**
284
     * Handle dynamic method calls into the driver.
285
     */
286
    public function __call(string $method, array $parameters): mixed
287
    {
288
        return call_user_func_array([$this->getModel(), $method], $parameters);
2✔
289
    }
290
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc