Passed
Pull Request — master (#94)
by Gombos
04:36 queued 01:01
created

AbstractSettingsManager::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Glorand\Model\Settings\Managers;
4
5
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
6
use Glorand\Model\Settings\Exceptions\ModelSettingsException;
7
use Glorand\Model\Settings\Traits\HasSettings;
8
use Illuminate\Support\Facades\Validator;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class AbstractSettingsManager
14
 * @package Glorand\Model\Settings\Managers
15
 */
16
abstract class AbstractSettingsManager implements SettingsManagerContract
17
{
18
    /** @var \Illuminate\Database\Eloquent\Model */
19
    protected $model;
20
21
    /** @var array */
22
    protected $defaultSettings = [];
23
24
    /**
25
     * AbstractSettingsManager constructor.
26
     * @param \Illuminate\Database\Eloquent\Model|HasSettings $model
27
     * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
28 198
     */
29
    public function __construct(Model $model)
30 198
    {
31 198
        $this->model = $model;
32 3
        if (!in_array(HasSettings::class, class_uses_recursive($this->model))) {
33
            throw new ModelSettingsException('Wrong model, missing HasSettings trait.');
34 195
        }
35
    }
36
37
    /**
38
     * Check if array is associative and not sequential
39 195
     * @param array $arr
40
     * @return bool
41 195
     */
42 195
    private static function isAssoc(array $arr): bool
43 171
    {
44
        if ([] === $arr) {
45
            return false;
46 186
        }
47
48
        return array_keys($arr) !== range(0, count($arr) - 1);
49
    }
50
51
    /**
52 12
     * Flatten array with dots for settings package
53
     * @param array $array
54 12
     * @param string $prepend
55
     * @return array
56
     */
57
    public static function dotFlatten(array $array, string $prepend = ''): array
58
    {
59
        $results = [];
60 12
        foreach ($array as $key => $value) {
61
            // only re-run if nested array is associative (key-based)
62 12
            if (is_array($value) && static::isAssoc($value) && !empty($value)) {
63
                $results = array_merge($results, static::dotFlatten($value, $prepend . $key . '.'));
64
            } else {
65
                $results[$prepend . $key] = $value;
66
            }
67
        }
68
69 12
        return $results;
70
    }
71 12
72
    /**
73
     * Get nested merged array with all available keys
74
     * @return array
75
     */
76
    public function all(): array
77
    {
78
        return $this->getMultiple();
79 48
    }
80
81 48
    /**
82
     * Get flat merged array with dot-notation keys
83
     * @return array
84
     */
85
    public function allFlattened(): array
86
    {
87
        $flattenedDefaultSettings = static::dotFlatten($this->model->getDefaultSettings());
0 ignored issues
show
Bug introduced by
It seems like $this->model->getDefaultSettings() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $array of Glorand\Model\Settings\M...gsManager::dotFlatten() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        $flattenedDefaultSettings = static::dotFlatten(/** @scrutinizer ignore-type */ $this->model->getDefaultSettings());
Loading history...
88
        $flattenedSettingsValue = static::dotFlatten($this->model->getSettingsValue());
89 12
90
        return array_merge($flattenedDefaultSettings, $flattenedSettingsValue);
91 12
    }
92 12
93 12
    /**
94
     * @return bool
95
     */
96 12
    public function exist(): bool
97
    {
98
        return count($this->all()) > 0;
99
    }
100
101
    /**
102
     * @return bool
103
     */
104 24
    public function empty(): bool
105
    {
106 24
        return count($this->all()) <= 0;
107 24
    }
108
109 24
    /**
110
     * @param string $path
111
     * @return bool
112
     */
113
    public function has(string $path): bool
114
    {
115
        return Arr::has($this->all(), $path);
116
    }
117 12
118
    /**
119 12
     * @param string|null $path
120
     * @param null $default
121
     * @return array|\ArrayAccess|mixed
122
     */
123
    public function get(string $path = null, $default = null)
124
    {
125
        return $path ? Arr::get($this->all(), $path, $default) : $this->all();
126 60
    }
127
128 60
    /**
129 60
     * @param iterable|null $paths
130
     * @param null $default
131 12
     * @return array
132 12
     */
133
    public function getMultiple(iterable $paths = null, $default = null): array
134
    {
135 60
        $array = [];
136
        $allFlattened = $this->allFlattened();
137 60
        $settingsArray = [];
138
        foreach ($allFlattened as $key => $value) {
139
            Arr::set($settingsArray, $key, $value);
140
        }
141
        if (is_null($paths)) {
142
            return $settingsArray;
143 42
        }
144
145 42
        foreach ($paths as $path) {
146
            Arr::set($array, $path, Arr::get($settingsArray, $path, $default));
147
        }
148
149
        return $array;
150
    }
151
152 12
    /**
153
     * @param string $path
154 12
     * @param $value
155 12
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
156 12
     */
157
    public function set(string $path, $value): SettingsManagerContract
158
    {
159 12
        $settings = $this->all();
160
        Arr::set($settings, $path, $value);
161
162
        return $this->apply($settings);
163
    }
164
165
    /**
166 12
     * @param string $path
167
     * @param mixed $value
168 12
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
169 12
     */
170 12
    public function update(string $path, $value): SettingsManagerContract
171
    {
172
        return $this->set($path, $value);
173 12
    }
174
175 12
    /**
176
     * @param string|null $path
177
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
178
     */
179
    public function delete(string $path = null): SettingsManagerContract
180
    {
181
        if (!$path) {
182
            $settings = [];
183
        } else {
184
            $settings = $this->all();
185
            Arr::forget($settings, $path);
186
        }
187
188
        $this->apply($settings);
189
190
        return $this;
191
    }
192
193
    /**
194
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
195
     */
196
    public function clear(): SettingsManagerContract
197
    {
198
        return $this->delete();
199
    }
200
201
    /**
202
     * @param iterable $values
203
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
204
     */
205
    public function setMultiple(iterable $values): SettingsManagerContract
206
    {
207
        $settings = $this->all();
208
        foreach ($values as $path => $value) {
209
            Arr::set($settings, $path, $value);
210
        }
211
212
        return $this->apply($settings);
213
    }
214
215
    /**
216
     * @param iterable $paths
217
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
218
     */
219
    public function deleteMultiple(iterable $paths): SettingsManagerContract
220
    {
221
        $settings = $this->all();
222
        foreach ($paths as $path) {
223
            Arr::forget($settings, $path);
224
        }
225
226
        $this->apply($settings);
227
228
        return $this;
229
    }
230
231
    /**
232
     * @param  array  $settings
233
     * @throws \Illuminate\Validation\ValidationException
234
     * @SuppressWarnings (PHPMD.StaticAccess)
235
     */
236
    protected function validate(array $settings)
237
    {
238
        Validator::make(Arr::wrap($settings), Arr::wrap($this->model->getRules()))->validate();
239
    }
240
}
241