Passed
Push — master ( 0c664b...4bf842 )
by Gombos
04:37
created

AbstractSettingsManager::getMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
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
10
/**
11
 * Class AbstractSettingsManager
12
 * @package Glorand\Model\Settings\Managers
13
 */
14
abstract class AbstractSettingsManager implements SettingsManagerContract
15
{
16
    /** @var \Illuminate\Database\Eloquent\Model */
17
    protected $model;
18
19
    /**
20
     * AbstractSettingsManager constructor.
21
     * @param \Illuminate\Database\Eloquent\Model $model
22
     * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
23
     */
24 26
    public function __construct(Model $model)
25
    {
26 26
        $this->model = $model;
27 26
        if (!in_array(HasSettings::class, class_uses_recursive($this->model))) {
28 1
            throw new ModelSettingsException('Wrong model, missing HasSettings trait.');
29
        }
30 25
    }
31
32
    /**
33
     * @return array
34
     */
35 25
    public function all(): array
36
    {
37 25
        return $this->model->getSettingsValue();
38
    }
39
40
    /**
41
     * @param string $path
42
     * @return bool
43
     */
44 2
    public function has(string $path): bool
45
    {
46 2
        return array_has($this->all(), $path);
0 ignored issues
show
Deprecated Code introduced by
The function array_has() has been deprecated: Arr::has() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

46
        return /** @scrutinizer ignore-deprecated */ array_has($this->all(), $path);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
47
    }
48
49
    /**
50
     * @param string|null $path
51
     * @param null $default
52
     * @return array|mixed
53
     */
54 8
    public function get(string $path = null, $default = null)
55
    {
56 8
        return $path ? array_get($this->all(), $path, $default) : $this->all();
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated: Arr::get() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

56
        return $path ? /** @scrutinizer ignore-deprecated */ array_get($this->all(), $path, $default) : $this->all();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
57
    }
58
59
    /**
60
     * @param iterable|null $paths
61
     * @param null $default
62
     * @return iterable
63
     */
64 2
    public function getMultiple(iterable $paths = null, $default = null): iterable
65
    {
66 2
        $values = [];
67 2
        foreach ($paths as $path) {
68 2
            $values[$path] = $this->get($path, $default);
69
        }
70
71 2
        return $values;
72
    }
73
74
    /**
75
     * @param string $path
76
     * @param mixed $value
77
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
78
     */
79 2
    public function update(string $path, $value): SettingsManagerContract
80
    {
81 2
        return $this->set($path, $value);
82
    }
83
84
    /**
85
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
86
     */
87 2
    public function clear(): SettingsManagerContract
88
    {
89 2
        return $this->delete();
90
    }
91
92
    /**
93
     * @param iterable $values
94
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
95
     */
96 2
    public function setMultiple(iterable $values): SettingsManagerContract
97
    {
98 2
        $settings = $this->all();
99 2
        foreach ($values as $path => $value) {
100 2
            array_set($settings, $path, $value);
0 ignored issues
show
Deprecated Code introduced by
The function array_set() has been deprecated: Arr::set() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

100
            /** @scrutinizer ignore-deprecated */ array_set($settings, $path, $value);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
101
        }
102
103 2
        return $this->apply($settings);
104
    }
105
}
106