Test Failed
Push — master ( d9eb19...0a106d )
by Gombos
03:48
created

AbstractSettingsManager::deleteMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
     */
28
    public function __construct(Model $model)
29 28
    {
30 28
        $this->model = $model;
31 1
        if (!in_array(HasSettings::class, class_uses_recursive($this->model))) {
32
            throw new ModelSettingsException('Wrong model, missing HasSettings trait.');
33 27
        }
34
    }
35
36
    /**
37
     * @return array
38 27
     */
39
    public function all(): array
40 27
    {
41
        return array_merge($this->model->getDefaultSettings(), $this->model->getSettingsValue());
2 ignored issues
show
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

41
        return array_merge($this->model->getDefaultSettings(), /** @scrutinizer ignore-type */ $this->model->getSettingsValue());
Loading history...
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

41
        return array_merge(/** @scrutinizer ignore-type */ $this->model->getDefaultSettings(), $this->model->getSettingsValue());
Loading history...
42
    }
43
44
    /**
45
     * @param string $path
46
     * @return bool
47 2
     */
48
    public function has(string $path): bool
49 2
    {
50
        return Arr::has($this->all(), $path);
51
    }
52
53
    /**
54
     * @param string|null $path
55
     * @param null $default
56
     * @return array|mixed
57 8
     */
58
    public function get(string $path = null, $default = null)
59 8
    {
60
        return $path ? Arr::get($this->all(), $path, $default) : $this->all();
61
    }
62
63
    /**
64
     * @param iterable|null $paths
65
     * @param null $default
66
     * @return iterable
67 2
     */
68
    public function getMultiple(iterable $paths = null, $default = null): iterable
69 2
    {
70 2
        $values = [];
71 2
        foreach ($paths as $path) {
72
            $values[$path] = $this->get($path, $default);
73
        }
74 2
75
        return $values;
76
    }
77
78
    /**
79
     * @param string $path
80
     * @param mixed $value
81
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
82 2
     */
83
    public function update(string $path, $value): SettingsManagerContract
84 2
    {
85
        return $this->set($path, $value);
86
    }
87
88
    /**
89
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
90 2
     */
91
    public function clear(): SettingsManagerContract
92 2
    {
93
        return $this->delete();
94
    }
95
96
    /**
97
     * @param iterable $values
98
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
99 2
     */
100
    public function setMultiple(iterable $values): SettingsManagerContract
101 2
    {
102 2
        $settings = $this->all();
103 2
        foreach ($values as $path => $value) {
104
            Arr::set($settings, $path, $value);
105
        }
106 2
107
        return $this->apply($settings);
108
    }
109
110
    /**
111
     * @param iterable $paths
112
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
113
     */
114
    public function deleteMultiple(iterable $paths): SettingsManagerContract
115
    {
116
        $settings = $this->all();
117
        foreach ($paths as $path) {
118
            Arr::forget($settings, $path);
119
        }
120
121
        $this->apply($settings);
122
123
        return $this;
124
    }
125
}
126