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

conedevelopment / root / 15084089635

17 May 2025 10:00AM UTC coverage: 77.93% (+0.04%) from 77.891%
15084089635

push

github

web-flow
Modernize back-end.yml (#240)

3291 of 4223 relevant lines covered (77.93%)

36.04 hits per line

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

67.92
/src/Settings/Repository.php
1
<?php
2

3
namespace Cone\Root\Settings;
4

5
use ArrayAccess;
6
use Cone\Root\Interfaces\Settings\Repository as Contract;
7
use Cone\Root\Models\Setting;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Database\Eloquent\Builder;
10

11
/**
12
 * @template TKey of array-key
13
 * @template TValue
14
 */
15
class Repository implements Arrayable, ArrayAccess, Contract
16
{
17
    /**
18
     * The repository cache.
19
     */
20
    protected array $cache = [];
21

22
    /**
23
     * The value casts.
24
     */
25
    protected array $casts = [];
26

27
    /**
28
     * Get the setting model.
29
     */
30
    public function model(): Setting
4✔
31
    {
32
        return Setting::proxy();
4✔
33
    }
34

35
    /**
36
     * Get the base query for the repository.
37
     */
38
    public function query(): Builder
4✔
39
    {
40
        return $this->model()->newQuery();
4✔
41
    }
42

43
    /**
44
     * Set the value cast.
45
     */
46
    public function cast(string $key, string $type): void
1✔
47
    {
48
        $this->casts[$key] = $type;
1✔
49
    }
50

51
    /**
52
     * Merge the casts.
53
     */
54
    public function mergeCasts(array $casts): void
×
55
    {
56
        $this->casts = array_merge($this->casts, $casts);
×
57
    }
58

59
    /**
60
     * Remove the given casts.
61
     */
62
    public function removeCasts(string|array $keys): void
×
63
    {
64
        foreach ((array) $keys as $key) {
×
65
            unset($this->casts[$key]);
×
66
        }
67
    }
68

69
    /**
70
     * Remove the given casts.
71
     */
72
    public function clearCasts(): void
×
73
    {
74
        $this->casts = [];
×
75
    }
76

77
    /**
78
     * Get the value casts.
79
     */
80
    public function getCasts(): array
×
81
    {
82
        return $this->casts;
×
83
    }
84

85
    /**
86
     * Get the value for the given key.
87
     */
88
    public function get(string $key, mixed $default = null, bool $fresh = false): mixed
3✔
89
    {
90
        if (! $fresh && $this->offsetExists($key)) {
3✔
91
            return $this->offsetGet($key);
2✔
92
        }
93

94
        return $this->refresh($key, $default);
3✔
95
    }
96

97
    /**
98
     * Refresh the given key.
99
     */
100
    public function refresh(string $key, mixed $default = null): mixed
3✔
101
    {
102
        $model = $this->query()->firstWhere('key', '=', $key);
3✔
103

104
        $value = is_null($model)
3✔
105
            ? $default
3✔
106
            : $model->castValue($this->casts[$key] ?? null)->value;
×
107

108
        $this->offsetSet($key, $value);
3✔
109

110
        return $value;
3✔
111
    }
112

113
    /**
114
     * Set the value for the given key.
115
     */
116
    public function set(string $key, mixed $value): mixed
3✔
117
    {
118
        $model = $this->query()->firstOrNew(['key' => $key]);
3✔
119

120
        $model->castValue($this->casts[$key] ?? null);
3✔
121

122
        $model->fill(['value' => $value]);
3✔
123

124
        $model->save();
3✔
125

126
        $this->offsetSet($key, $model->value);
3✔
127

128
        return $this->offsetGet($key);
3✔
129
    }
130

131
    /**
132
     * Delete the given keys.
133
     */
134
    public function delete(string|array $keys): void
1✔
135
    {
136
        foreach ((array) $keys as $key) {
1✔
137
            $this->offsetUnset($key);
1✔
138
        }
139

140
        $this->query()->whereIn('key', (array) $keys)->delete();
1✔
141
    }
142

143
    /**
144
     * Flush the cache.
145
     */
146
    public function flush(): void
×
147
    {
148
        $this->cache = [];
×
149
    }
150

151
    /**
152
     * Get all the settings.
153
     */
154
    public function all(): array
×
155
    {
156
        return $this->toArray();
×
157
    }
158

159
    /**
160
     * Convert the repository to an array.
161
     */
162
    public function toArray(): array
×
163
    {
164
        return $this->cache;
×
165
    }
166

167
    /**
168
     * Determine if an item exists at an offset.
169
     *
170
     * @param  TKey  $key
171
     */
172
    public function offsetExists($key): bool
3✔
173
    {
174
        return isset($this->cache[$key]);
3✔
175
    }
176

177
    /**
178
     * Get an item at a given offset.
179
     *
180
     * @param  TKey  $key
181
     */
182
    public function offsetGet($key): mixed
3✔
183
    {
184
        return $this->cache[$key];
3✔
185
    }
186

187
    /**
188
     * Set the item at a given offset.
189
     *
190
     * @param  TKey|null  $key
191
     * @param  TValue  $value
192
     */
193
    public function offsetSet($key, $value): void
4✔
194
    {
195
        if (is_null($key)) {
4✔
196
            $this->cache[] = $value;
×
197
        } else {
198
            $this->cache[$key] = $value;
4✔
199
        }
200
    }
201

202
    /**
203
     * Unset the item at a given offset.
204
     *
205
     * @param  TKey  $key
206
     */
207
    public function offsetUnset($key): void
1✔
208
    {
209
        unset($this->cache[$key]);
1✔
210
    }
211
}
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

© 2025 Coveralls, Inc