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

conedevelopment / bazar / 20694116184

04 Jan 2026 02:08PM UTC coverage: 68.615% (+4.5%) from 64.117%
20694116184

push

github

iamgergo
version

1679 of 2447 relevant lines covered (68.61%)

25.06 hits per line

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

85.58
/src/Models/Shipping.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Cone\Bazar\Models;
6

7
use Cone\Bazar\Database\Factories\ShippingFactory;
8
use Cone\Bazar\Enums\Currency;
9
use Cone\Bazar\Interfaces\Models\Shipping as Contract;
10
use Cone\Bazar\Support\Facades\Shipping as Manager;
11
use Cone\Bazar\Traits\Addressable;
12
use Cone\Bazar\Traits\InteractsWithDiscounts;
13
use Cone\Bazar\Traits\InteractsWithTaxes;
14
use Cone\Root\Traits\InteractsWithProxy;
15
use Illuminate\Database\Eloquent\Casts\Attribute;
16
use Illuminate\Database\Eloquent\Factories\HasFactory;
17
use Illuminate\Database\Eloquent\Model;
18
use Illuminate\Database\Eloquent\Relations\MorphTo;
19
use Illuminate\Support\Collection;
20
use Throwable;
21

22
class Shipping extends Model implements Contract
23
{
24
    use Addressable;
25
    use HasFactory;
26
    use InteractsWithDiscounts;
27
    use InteractsWithProxy;
28
    use InteractsWithTaxes;
29

30
    /**
31
     * The attributes that should have default values.
32
     *
33
     * @var array<string, mixed>
34
     */
35
    protected $attributes = [
36
        'fee' => 0,
37
    ];
38

39
    /**
40
     * The attributes that are mass assignable.
41
     *
42
     * @var list<string>
43
     */
44
    protected $fillable = [
45
        'fee',
46
        'driver',
47
    ];
48

49
    /**
50
     * The table associated with the model.
51
     *
52
     * @var string
53
     */
54
    protected $table = 'bazar_shippings';
55

56
    /**
57
     * The "booted" method of the model.
58
     */
59
    protected static function booted(): void
38✔
60
    {
61
        static::creating(static function (self $shipping): void {
38✔
62
            $shipping->driver = $shipping->driver ?: Manager::getDefaultDriver();
32✔
63
        });
38✔
64
    }
65

66
    /**
67
     * Get the proxied interface.
68
     */
69
    public static function getProxiedInterface(): string
1✔
70
    {
71
        return Contract::class;
1✔
72
    }
73

74
    /**
75
     * Create a new factory instance for the model.
76
     */
77
    protected static function newFactory(): ShippingFactory
11✔
78
    {
79
        return ShippingFactory::new();
11✔
80
    }
81

82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getMorphClass(): string
30✔
86
    {
87
        return static::getProxiedClass();
30✔
88
    }
89

90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function casts(): array
38✔
94
    {
95
        return [
38✔
96
            'fee' => 'float',
38✔
97
        ];
38✔
98
    }
99

100
    /**
101
     * Get the shippable model for the shipping.
102
     */
103
    public function shippable(): MorphTo
14✔
104
    {
105
        return $this->morphTo()->withDefault(static function (): Cart {
14✔
106
            return Cart::proxy()->newInstance();
1✔
107
        });
14✔
108
    }
109

110
    /**
111
     * Get the driver attribute.
112
     *
113
     * @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
114
     */
115
    protected function driver(): Attribute
32✔
116
    {
117
        return new Attribute(
32✔
118
            get: static function (?string $value = null): string {
32✔
119
                return $value ?: Manager::getDefaultDriver();
32✔
120
            }
32✔
121
        );
32✔
122
    }
123

124
    /**
125
     * Get the total attribute.
126
     *
127
     * @return \Illuminate\Database\Eloquent\Casts\Attribute<float, never>
128
     */
129
    protected function total(): Attribute
2✔
130
    {
131
        return new Attribute(
2✔
132
            get: function (): float {
2✔
133
                return $this->getTotal();
1✔
134
            }
2✔
135
        );
2✔
136
    }
137

138
    /**
139
     * Get the formatted total attribute.
140
     *
141
     * @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
142
     */
143
    protected function formattedTotal(): Attribute
2✔
144
    {
145
        return new Attribute(
2✔
146
            get: function (): string {
2✔
147
                return $this->getFormattedTotal();
1✔
148
            }
2✔
149
        );
2✔
150
    }
151

152
    /**
153
     * Get the subtotal attribute.
154
     *
155
     * @return \Illuminate\Database\Eloquent\Casts\Attribute<float, never>
156
     */
157
    protected function subtotal(): Attribute
2✔
158
    {
159
        return new Attribute(
2✔
160
            get: function (): float {
2✔
161
                return $this->getSubtotal();
1✔
162
            }
2✔
163
        );
2✔
164
    }
165

166
    /**
167
     * Get the formatted subtotal attribute.
168
     *
169
     * @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
170
     */
171
    protected function formattedSubtotal(): Attribute
2✔
172
    {
173
        return new Attribute(
2✔
174
            get: function (): string {
2✔
175
                return $this->getFormattedSubtotal();
1✔
176
            }
2✔
177
        );
2✔
178
    }
179

180
    /**
181
     * Get the name of the shipping method.
182
     *
183
     * @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
184
     */
185
    protected function driverName(): Attribute
2✔
186
    {
187
        return new Attribute(
2✔
188
            get: static function (mixed $value, array $attributes): string {
2✔
189
                try {
190
                    return Manager::driver($attributes['driver'])->getName();
1✔
191
                } catch (Throwable $exception) {
1✔
192
                    return $attributes['driver'];
1✔
193
                }
194
            }
2✔
195
        );
2✔
196
    }
197

198
    /**
199
     * Get the name.
200
     */
201
    public function getName(): string
×
202
    {
203
        return $this->driverName;
×
204
    }
205

206
    /**
207
     * Get the tax base.
208
     */
209
    public function getTaxBase(): float
2✔
210
    {
211
        return $this->fee;
2✔
212
    }
213

214
    /**
215
     * Get the price.
216
     */
217
    public function getPrice(): float
11✔
218
    {
219
        return $this->fee;
11✔
220
    }
221

222
    /**
223
     * Get the formatted price.
224
     */
225
    public function getFormattedPrice(): string
×
226
    {
227
        return $this->shippable->getCurrency()->format($this->getPrice());
×
228
    }
229

230
    /**
231
     * Get the gross price.
232
     */
233
    public function getGrossPrice(): float
10✔
234
    {
235
        return $this->getPrice() + $this->getTax();
10✔
236
    }
237

238
    /**
239
     * Get the formatted price.
240
     */
241
    public function getFormattedGrossPrice(): string
×
242
    {
243
        return $this->shippable->getCurrency()->format($this->getGrossPrice());
×
244
    }
245

246
    /**
247
     * Get the shipping's total.
248
     */
249
    public function getTotal(): float
10✔
250
    {
251
        return $this->getGrossPrice() * $this->getQuantity();
10✔
252
    }
253

254
    /**
255
     * Get the shipping's formatted total.
256
     */
257
    public function getFormattedTotal(): string
1✔
258
    {
259
        return $this->shippable->getCurrency()->format($this->getTotal());
1✔
260
    }
261

262
    /**
263
     * Get the shipping's subtotal.
264
     */
265
    public function getSubtotal(): float
2✔
266
    {
267
        return $this->getPrice();
2✔
268
    }
269

270
    /**
271
     * Get the shipping's formatted subtotal.
272
     */
273
    public function getFormattedSubtotal(): string
1✔
274
    {
275
        return $this->shippable->getCurrency()->format($this->getSubtotal());
1✔
276
    }
277

278
    /**
279
     * Get the formatted tax total.
280
     */
281
    public function getFormattedTax(): string
×
282
    {
283
        return $this->shippable->getCurrency()->format($this->getTax());
×
284
    }
285

286
    /**
287
     * Get the formatted tax total.
288
     */
289
    public function getFormattedTaxTotal(): string
×
290
    {
291
        return $this->shippable->getCurrency()->format($this->getTaxTotal());
×
292
    }
293

294
    /**
295
     * Get the quantity.
296
     */
297
    public function getQuantity(): float
23✔
298
    {
299
        return 1.0;
23✔
300
    }
301

302
    /**
303
     * Calculate the fee.
304
     */
305
    public function calculateFee(): float
15✔
306
    {
307
        try {
308
            $fee = Manager::driver($this->driver)->calculate($this->shippable);
15✔
309

310
            $this->fill(['fee' => $fee])->save();
15✔
311
        } catch (Throwable $exception) {
×
312
            //
313
        }
314

315
        return $this->fee;
15✔
316
    }
317

318
    /**
319
     * Calculate the taxes.
320
     */
321
    public function calculateTaxes(): float
15✔
322
    {
323
        $this->taxes()->detach();
15✔
324

325
        TaxRate::proxy()
15✔
326
            ->newQuery()
15✔
327
            ->applicableForShipping()
15✔
328
            ->get()
15✔
329
            ->each(fn (TaxRate $taxRate): null => $taxRate->apply($this));
15✔
330

331
        return $this->getTaxTotal();
15✔
332
    }
333

334
    /**
335
     * Get the applicable discount rules.
336
     */
337
    public function getApplicableDiscountRules(): Collection
14✔
338
    {
339
        return $this->shippable->getApplicableDiscountRules()
14✔
340
            ->where('discountable_type', $this->shippable_type);
14✔
341
    }
342

343
    /**
344
     * Get the discountable quantity.
345
     */
346
    public function getDiscountableQuantity(): float
×
347
    {
348
        return $this->getQuantity();
×
349
    }
350

351
    /**
352
     * Get the discountable currency.
353
     */
354
    public function getDiscountableCurrency(): Currency
×
355
    {
356
        return $this->shippable->getCurrency();
×
357
    }
358

359
    /**
360
     * Validate the shipping address.
361
     */
362
    public function validate(): bool
1✔
363
    {
364
        return $this->address->validate();
1✔
365
    }
366
}
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