Passed
Push — master ( 0a106d...6f5789 )
by Gombos
08:09 queued 03:26
created

AbstractSettingsManager::deleteMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
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\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 120
    public function __construct(Model $model)
29
    {
30 120
        $this->model = $model;
31 120
        if (!in_array(HasSettings::class, class_uses_recursive($this->model))) {
32 3
            throw new ModelSettingsException('Wrong model, missing HasSettings trait.');
33
        }
34 117
    }
35
36
    /**
37
     * @return array
38
     */
39 117
    public function all(): array
40
    {
41 117
        return array_merge($this->model->getDefaultSettings(), $this->model->getSettingsValue());
42
    }
43
44
    /**
45
     * @param string $path
46
     * @return bool
47
     */
48 9
    public function has(string $path): bool
49
    {
50 9
        return Arr::has($this->all(), $path);
51
    }
52
53
    /**
54
     * @param string|null $path
55
     * @param null $default
56
     * @return array|mixed
57
     */
58 36
    public function get(string $path = null, $default = null)
59
    {
60 36
        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
     */
68 9
    public function getMultiple(iterable $paths = null, $default = null): iterable
69
    {
70 9
        $values = [];
71 9
        foreach ($paths as $path) {
72 9
            $values[$path] = $this->get($path, $default);
73
        }
74
75 9
        return $values;
76
    }
77
78
    /**
79
     * @param string $path
80
     * @param mixed $value
81
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
82
     */
83 9
    public function update(string $path, $value): SettingsManagerContract
84
    {
85 9
        return $this->set($path, $value);
86
    }
87
88
    /**
89
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
90
     */
91 21
    public function clear(): SettingsManagerContract
92
    {
93 21
        return $this->delete();
94
    }
95
96
    /**
97
     * @param iterable $values
98
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
99
     */
100 9
    public function setMultiple(iterable $values): SettingsManagerContract
101
    {
102 9
        $settings = $this->all();
103 9
        foreach ($values as $path => $value) {
104 9
            Arr::set($settings, $path, $value);
105
        }
106
107 9
        return $this->apply($settings);
108
    }
109
110
    /**
111
     * @param iterable $paths
112
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
113
     */
114 9
    public function deleteMultiple(iterable $paths): SettingsManagerContract
115
    {
116 9
        $settings = $this->all();
117 9
        foreach ($paths as $path) {
118 9
            Arr::forget($settings, $path);
119
        }
120
121 9
        $this->apply($settings);
122
123 9
        return $this;
124
    }
125
}
126