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

AbstractSettingsManager::has()   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 1
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 183
    public function __construct(Model $model)
29
    {
30 183
        $this->model = $model;
31 183
        if (!in_array(HasSettings::class, class_uses_recursive($this->model))) {
32 3
            throw new ModelSettingsException('Wrong model, missing HasSettings trait.');
33
        }
34 180
    }
35
36
    /**
37
     * @return array
38
     */
39 180
    public function all(): array
40
    {
41 180
        $array = [];
42 180
        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 156
            Arr::set($array, $key, $value);
44
        }
45
46 171
        return $array;
47
    }
48
49 12
    public function exist(): bool
50
    {
51 12
        $all = $this->all();
52
53 12
        return count($all) > 0;
54
    }
55
56
    /**
57
     * @param string $path
58
     * @return bool
59
     */
60 12
    public function has(string $path): bool
61
    {
62 12
        return Arr::has($this->all(), $path);
63
    }
64
65
    /**
66
     * @param string|null $path
67
     * @param null $default
68
     * @return array|mixed
69
     */
70 48
    public function get(string $path = null, $default = null)
71
    {
72 48
        return $path ? Arr::get($this->all(), $path, $default) : $this->all();
73
    }
74
75
    /**
76
     * @param iterable|null $paths
77
     * @param null $default
78
     * @return iterable
79
     */
80 12
    public function getMultiple(iterable $paths = null, $default = null): iterable
81
    {
82 12
        $values = [];
83 12
        foreach ($paths as $path) {
84 12
            $values[$path] = $this->get($path, $default);
85
        }
86
87 12
        return $values;
88
    }
89
90
    /**
91
     * @param string $path
92
     * @param $value
93
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
94
     */
95 24
    public function set(string $path, $value): SettingsManagerContract
96
    {
97 24
        $settings = $this->all();
98 24
        Arr::set($settings, $path, $value);
99
100 24
        return $this->apply($settings);
101
    }
102
103
    /**
104
     * @param string $path
105
     * @param mixed $value
106
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
107
     */
108 12
    public function update(string $path, $value): SettingsManagerContract
109
    {
110 12
        return $this->set($path, $value);
111
    }
112
113
    /**
114
     * @param string|null $path
115
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
116
     */
117 45
    public function delete(string $path = null): SettingsManagerContract
118
    {
119 45
        if (!$path) {
120 45
            $settings = [];
121
        } else {
122 12
            $settings = $this->all();
123 12
            Arr::forget($settings, $path);
124
        }
125
126 45
        $this->apply($settings);
127
128 45
        return $this;
129
    }
130
131
    /**
132
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
133
     */
134 27
    public function clear(): SettingsManagerContract
135
    {
136 27
        return $this->delete();
137
    }
138
139
    /**
140
     * @param iterable $values
141
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
142
     */
143 12
    public function setMultiple(iterable $values): SettingsManagerContract
144
    {
145 12
        $settings = $this->all();
146 12
        foreach ($values as $path => $value) {
147 12
            Arr::set($settings, $path, $value);
148
        }
149
150 12
        return $this->apply($settings);
151
    }
152
153
    /**
154
     * @param iterable $paths
155
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
156
     */
157 12
    public function deleteMultiple(iterable $paths): SettingsManagerContract
158
    {
159 12
        $settings = $this->all();
160 12
        foreach ($paths as $path) {
161 12
            Arr::forget($settings, $path);
162
        }
163
164 12
        $this->apply($settings);
165
166 12
        return $this;
167
    }
168
}
169