• 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

13.16
/src/Traits/InteractsWithStock.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Cone\Bazar\Traits;
6

7
use Illuminate\Support\Facades\Config;
8

9
trait InteractsWithStock
10
{
11
    /**
12
     * Get the formatted dimensions.
13
     */
14
    public function getFormattedDimensions(): ?string
×
15
    {
16
        $dimensions = $this->metaData->whereIn('key', ['length', 'width', 'height'])->filter()->values();
×
17

18
        if ($dimensions->isEmpty()) {
×
19
            return null;
×
20
        }
21

22
        return sprintf('%s %s', $dimensions->implode('value', 'x'), Config::get('bazar.dimension_unit'));
×
23
    }
24

25
    /**
26
     * Get the formatted weight.
27
     */
28
    public function getFormattedWeight(): ?string
×
29
    {
30
        $weight = $this->metaData->firstWhere('key', 'weight');
×
31

32
        if (is_null($weight) || empty($weight->value)) {
×
33
            return null;
×
34
        }
35

36
        return sprintf('%s %s', $weight->value, Config::get('bazar.weight_unit'));
×
37
    }
38

39
    /**
40
     * Determine if the stockable model is virtual.
41
     */
42
    public function isVirtual(): bool
23✔
43
    {
44
        $meta = $this->metaData->firstWhere('key', 'virtual');
23✔
45

46
        return ! is_null($meta) && (bool) $meta->value;
23✔
47
    }
48

49
    /**
50
     * Determine if the stockable model is physical.
51
     */
52
    public function isPhysical(): bool
23✔
53
    {
54
        return ! $this->isVirtual();
23✔
55
    }
56

57
    /**
58
     * Determine if the stockable model is downloadable.
59
     */
60
    public function isDownloadable(): bool
×
61
    {
62
        $meta = $this->metaData->firstWhere('key', 'downloadable');
×
63

64
        return ! is_null($meta) && (bool) $meta->value;
×
65
    }
66

67
    /**
68
     * Determine if the stockable model tracks quantity.
69
     */
70
    public function tracksQuantity(): bool
×
71
    {
72
        $meta = $this->metaData->firstWhere('key', 'quantity');
×
73

74
        return ! is_null($meta) && ! empty($meta->value);
×
75
    }
76

77
    /**
78
     * Determine if the stockable model is available.
79
     */
80
    public function isAvailable(float $quantity = 1): bool
×
81
    {
82
        if (! $this->tracksQuantity()) {
×
83
            return true;
×
84
        }
85

86
        $stock = $this->metaData->firstWhere('key', 'quantity')?->value ?: 0;
×
87

88
        return min($stock, $quantity) > 0 && $stock >= $quantity;
×
89
    }
90

91
    /**
92
     * Increment the quantity by the given value.
93
     */
94
    public function incrementQuantity(float $quantity = 1): void
×
95
    {
96
        if ($this->tracksQuantity()) {
×
97
            $meta = $this->metaData->firstWhere('key', 'quantity')
×
98
                ?: $this->metaData()->make(['key' => 'quantity', 'value' => 0]);
×
99

100
            $meta->value = ((float) $meta->value) + $quantity;
×
101

102
            $meta->save();
×
103
        }
104
    }
105

106
    /**
107
     * Decrement the quantity by the given value.
108
     */
109
    public function decrementQuantity(float $quantity = 1): void
×
110
    {
111
        if ($this->tracksQuantity()) {
×
112
            $meta = $this->metaData->firstWhere('key', 'quantity')
×
113
                ?: $this->metaData()->make(['key' => 'quantity', 'value' => 0]);
×
114

115
            $meta->value = max(((float) $meta->value) - $quantity, 0);
×
116

117
            $meta->save();
×
118
        }
119
    }
120
}
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