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

lonnieezell / Bonfire2 / 13618451914

02 Mar 2025 07:07PM UTC coverage: 45.094% (-0.5%) from 45.594%
13618451914

Pull #564

github

web-flow
Merge 4efcc8f7f into 49088d21e
Pull Request #564: Prevent unnecessary queries related to widgets

47 of 88 new or added lines in 7 files covered. (53.41%)

20 existing lines in 1 file now uncovered.

1590 of 3526 relevant lines covered (45.09%)

63.96 hits per line

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

54.55
/src/Widgets/Types/Stats/StatsItem.php
1
<?php
2

3
/**
4
 * This file is part of Bonfire.
5
 *
6
 * (c) Lonnie Ezell <lonnieje@gmail.com>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11

12
namespace Bonfire\Widgets\Types\Stats;
13

14
use Bonfire\Widgets\Interfaces\Item;
15

16
/**
17
 * Represents an individual widget stats.
18
 *
19
 * @property string $bgColor
20
 * @property string $faIcon
21
 * @property string $id
22
 * @property string $title
23
 * @property string $url
24
 * @property string $value
25
 */
26
class StatsItem implements Item
27
{
28
    /**
29
     * @var string|null
30
     */
31
    protected $title;
32

33
    /**
34
     * Unique identifier for the widget, used for storage in settings.
35
     *
36
     * @var string
37
     */
38
    protected $id;
39

40
    /**
41
     * @var string|null
42
     */
43
    protected $value;
44

45
    /**
46
     * FontAwesome 5 icon name
47
     *
48
     * @var string|null
49
     */
50
    protected $faIcon;
51

52
    /**
53
     * @var string|null
54
     */
55
    protected $url;
56

57
    /**
58
     * The assignable background color on the statistics widget
59
     *
60
     * Possible values are:
61
     * bg-blue
62
     * bg-red
63
     * bg-orange
64
     * bg-light
65
     * bg-dark
66
     * bg-inverse
67
     * bg-indigo
68
     * bg-purple
69
     * bg-pink
70
     * bg-yellow
71
     * bg-green
72
     * bg-teal
73
     * bg-lime
74
     * bg-cyan
75
     * bg-white
76
     * bg-gray
77
     * bg-gray-dark
78
     */
79
    protected $bgColor;
80

81
    /**
82
     * @var bool
83
     */
84
    protected $dashboardRoute = false;
85

86
    public function __construct(?array $data = null)
87
    {
88
        if (! is_array($data)) {
438✔
89
            return;
438✔
90
        }
91

92
        foreach ($data as $key => $value) {
438✔
93
            $method = 'set' . ucfirst($key);
438✔
94
            if (method_exists($this, $method)) {
438✔
95
                $this->{$method}($value);
438✔
96
            }
97
        }
98

99
        // true if we are on Dashboard page
100
        $this->dashboardRoute = current_url() === config('App')->baseURL . '/' . ADMIN_AREA;
438✔
101
    }
102

103
    public function setTitle(?string $title): StatsItem
104
    {
105
        $this->title = $title;
438✔
106

107
        return $this;
438✔
108
    }
109

110
    public function setId(?string $id): StatsItem
111
    {
112
        $this->id = $id;
438✔
113

114
        return $this;
438✔
115
    }
116

117
    public function setValue(?string $value): StatsItem
118
    {
119
        $this->value = $value;
438✔
120

121
        return $this;
438✔
122
    }
123

124
    public function setFaIcon(?string $faIcon): StatsItem
125
    {
126
        $this->faIcon = $faIcon;
438✔
127

128
        return $this;
438✔
129
    }
130

131
    public function setUrl(string $url = '#'): StatsItem
132
    {
133
        $this->url = str_contains($url, '://')
438✔
134
            ? $url
×
135
            : '/' . ltrim($url, '/ ');
438✔
136

137
        return $this;
438✔
138
    }
139

140
    /**
141
     * The assignable background color on the statistics widget
142
     *
143
     * Possible values are:
144
     * bg-blue
145
     * bg-red
146
     * bg-orange
147
     * bg-light
148
     * bg-dark
149
     * bg-inverse
150
     * bg-indigo
151
     * bg-purple
152
     * bg-pink
153
     * bg-yellow
154
     * bg-green
155
     * bg-teal
156
     * bg-lime
157
     * bg-cyan
158
     * bg-white
159
     * bg-gray
160
     * bg-gray-dark
161
     */
162
    public function setBgColor(string $bgColor = 'bg-blue'): StatsItem
163
    {
164
        $this->bgColor = $bgColor;
438✔
165

166
        return $this;
438✔
167
    }
168

169
    public function __get(string $key)
170
    {
171
        if (method_exists($this, $key)) {
60✔
172
            return $this->{$key}();
18✔
173
        }
174
    }
175

176
    public function title(): ?string
177
    {
178
        return mb_strtoupper((string) $this->title);
66✔
179
    }
180

181
    public function id(): ?string
182
    {
183
        return $this->id;
30✔
184
    }
185

186
    public function value(): ?string
187
    {
188
        return $this->value;
18✔
189
    }
190

191
    public function faIcon(): ?string
192
    {
193
        return $this->faIcon;
18✔
194
    }
195

196
    public function url(): ?string
197
    {
198
        return $this->url;
18✔
199
    }
200

201
    public function bgColor(): ?string
202
    {
203
        return $this->bgColor;
18✔
204
    }
205

206
    public function addValue(string $tableName, ?string $whereString = null, string $selectMode = 'count'): StatsItem
207
    {
208
        // Check if we are on Dashboard page and the chart is enabled
209
        if (! $this->dashboardRoute || setting('Stats.Stats_' . $this->id) !== 'on') {
438✔
210
            return $this;
438✔
211
        }
212

213
        // Chart Section Begin
NEW
214
        $query = db_connect()->table($tableName);
×
NEW
215
        $query->where('deleted_at', null);
×
NEW
216
        if ($whereString) {
×
NEW
217
            $query->where($whereString);
×
218
        }
219

NEW
220
        $query = match ($selectMode) {
×
NEW
221
            'count' => $query->countAllResults(),
×
NEW
222
            'avg'   => $query->selectAvg('value')->get()->getRow()->value,
×
NEW
223
            'max'   => $query->selectMax('value')->get()->getRow()->value,
×
NEW
224
            'min'   => $query->selectMin('value')->get()->getRow()->value,
×
NEW
225
            'sum'   => $query->selectSum('value')->get()->getRow()->value,
×
NEW
226
            default => $query->countAllResults(),
×
NEW
227
        };
×
228

229
        // Check if the result is a float and format accordingly
NEW
230
        if (is_float($query)) {
×
231
            // todo: format the value dynamically based on locale
NEW
232
            $this->setValue(number_format($query, 2, '.', ''));
×
233
        } else {
NEW
234
            $this->setValue((string) $query);
×
235
        }
236

NEW
237
        return $this;
×
238
    }
239

240
    public function addValueByFreeQuery(string $query): StatsItem
241
    {
242
        // Check if we are on Dashboard page and the chart is enabled
NEW
243
        if (! $this->dashboardRoute || setting('Stats.Stats_' . $this->id) !== 'on') {
×
NEW
244
            return $this;
×
245
        }
246

247
        // Execute the query
NEW
248
        $result = db_connect()->query($query)->getRow();
×
249

250
        // Assuming the query returns a single value in the first column
NEW
251
        $value = reset($result);
×
252

253
        // Check if the result is a float and format accordingly
NEW
254
        if (is_float($value)) {
×
NEW
255
            $this->setValue(number_format($value, 2, '.', ''));
×
256
        } else {
NEW
257
            $this->setValue((string) $value);
×
258
        }
259

NEW
260
        return $this;
×
261
    }
262
}
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