Passed
Push — master ( 08e0de...df0894 )
by Gombos
03:08
created

AbstractSettingsManager::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Database\Eloquent\Model;
9
use Illuminate\Support\Arr;
10
11
/**
12
 * Class AbstractSettingsManager
13
 * @package Glorand\Model\Settings\Managers
14
 */
15
abstract class AbstractSettingsManager implements SettingsManagerContract
16
{
17
    /** @var \Illuminate\Database\Eloquent\Model */
18
    protected $model;
19
20
    /** @var array */
21
    protected $defaultSettings = [];
22
23
    /**
24
     * AbstractSettingsManager constructor.
25
     * @param \Illuminate\Database\Eloquent\Model|HasSettings $model
26
     * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
27
     */
28 195
    public function __construct(Model $model)
29
    {
30 195
        $this->model = $model;
31 195
        if (!in_array(HasSettings::class, class_uses_recursive($this->model))) {
32 3
            throw new ModelSettingsException('Wrong model, missing HasSettings trait.');
33
        }
34 192
    }
35
36
    /**
37
     * @return array
38
     */
39 192
    public function all(): array
40
    {
41 192
        $array = [];
42 192
        foreach (array_merge($this->model->getDefaultSettings(), $this->model->getSettingsValue()) as $key => $value) {
0 ignored issues
show
Bug introduced by
It seems like $this->model->getDefaultSettings() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $array1 of array_merge() 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

42
        foreach (array_merge(/** @scrutinizer ignore-type */ $this->model->getDefaultSettings(), $this->model->getSettingsValue()) as $key => $value) {
Loading history...
Bug introduced by
It seems like $this->model->getSettingsValue() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $array2 of array_merge() does only seem to accept array|null, 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

42
        foreach (array_merge($this->model->getDefaultSettings(), /** @scrutinizer ignore-type */ $this->model->getSettingsValue()) as $key => $value) {
Loading history...
43 168
            Arr::set($array, $key, $value);
44
        }
45
46 183
        return $array;
47
    }
48
49
    /**
50
     * @return bool
51
     */
52 12
    public function exist(): bool
53
    {
54 12
        return count($this->all()) > 0;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60 12
    public function empty(): bool
61
    {
62 12
        return count($this->all()) <= 0;
63
    }
64
65
    /**
66
     * @param string $path
67
     * @return bool
68
     */
69 12
    public function has(string $path): bool
70
    {
71 12
        return Arr::has($this->all(), $path);
72
    }
73
74
    /**
75
     * @param string|null $path
76
     * @param null $default
77
     * @return array|mixed
78
     */
79 48
    public function get(string $path = null, $default = null)
80
    {
81 48
        return $path ? Arr::get($this->all(), $path, $default) : $this->all();
82
    }
83
84
    /**
85
     * @param iterable|null $paths
86
     * @param null $default
87
     * @return iterable
88
     */
89 12
    public function getMultiple(iterable $paths = null, $default = null): iterable
90
    {
91 12
        $values = [];
92 12
        foreach ($paths as $path) {
93 12
            $values[$path] = $this->get($path, $default);
94
        }
95
96 12
        return $values;
97
    }
98
99
    /**
100
     * @param string $path
101
     * @param $value
102
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
103
     */
104 24
    public function set(string $path, $value): SettingsManagerContract
105
    {
106 24
        $settings = $this->all();
107 24
        Arr::set($settings, $path, $value);
108
109 24
        return $this->apply($settings);
110
    }
111
112
    /**
113
     * @param string $path
114
     * @param mixed $value
115
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
116
     */
117 12
    public function update(string $path, $value): SettingsManagerContract
118
    {
119 12
        return $this->set($path, $value);
120
    }
121
122
    /**
123
     * @param string|null $path
124
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
125
     */
126 60
    public function delete(string $path = null): SettingsManagerContract
127
    {
128 60
        if (!$path) {
129 60
            $settings = [];
130
        } else {
131 12
            $settings = $this->all();
132 12
            Arr::forget($settings, $path);
133
        }
134
135 60
        $this->apply($settings);
136
137 60
        return $this;
138
    }
139
140
    /**
141
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
142
     */
143 42
    public function clear(): SettingsManagerContract
144
    {
145 42
        return $this->delete();
146
    }
147
148
    /**
149
     * @param iterable $values
150
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
151
     */
152 12
    public function setMultiple(iterable $values): SettingsManagerContract
153
    {
154 12
        $settings = $this->all();
155 12
        foreach ($values as $path => $value) {
156 12
            Arr::set($settings, $path, $value);
157
        }
158
159 12
        return $this->apply($settings);
160
    }
161
162
    /**
163
     * @param iterable $paths
164
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
165
     */
166 12
    public function deleteMultiple(iterable $paths): SettingsManagerContract
167
    {
168 12
        $settings = $this->all();
169 12
        foreach ($paths as $path) {
170 12
            Arr::forget($settings, $path);
171
        }
172
173 12
        $this->apply($settings);
174
175 12
        return $this;
176
    }
177
}
178