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

FieldSettingsManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 45
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 6 1
A apply() 0 8 2
A delete() 0 12 2
1
<?php
2
3
namespace Glorand\Model\Settings\Managers;
4
5
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
6
use Illuminate\Support\Arr;
7
8
/**
9
 * Class FieldSettingsManager
10
 * @package Glorand\Model\Settings\Managers
11
 * @property \Illuminate\Database\Eloquent\Model|\Glorand\Model\Settings\Traits\HasSettingsField $model
12
 */
13
class FieldSettingsManager extends AbstractSettingsManager
14
{
15
    /**
16
     * @param array $settings
17
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
18 12
     */
19
    public function apply(array $settings = []): SettingsManagerContract
20 12
    {
21 12
        $this->model->{$this->model->getSettingsFieldName()} = json_encode($settings);
22 12
        if ($this->model->isPersistSettings()) {
23
            $this->model->save();
1 ignored issue
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

23
            $this->model->/** @scrutinizer ignore-call */ 
24
                          save();
Loading history...
24
        }
25 12
26
        return $this;
27
    }
28
29
    /**
30
     * @param string|null $path
31
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
32 3
     */
33
    public function delete(string $path = null): SettingsManagerContract
34 3
    {
35 3
        if (!$path) {
36
            $settings = [];
37 1
        } else {
38 1
            $settings = $this->all();
39
            Arr::forget($settings, $path);
40
        }
41 3
42
        $this->apply($settings);
43 3
44
        return $this;
45
    }
46
47
    /**
48
     * @param string $path
49
     * @param mixed $value
50 1
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
51
     */
52 1
    public function set(string $path, $value): SettingsManagerContract
53 1
    {
54 1
        $settings = $this->all();
55
        Arr::set($settings, $path, $value);
56
57 1
        return $this->apply($settings);
58
    }
59
}
60