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

FieldSettingsManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 61
ccs 22
cts 22
cp 1
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 8 2
A delete() 0 12 2
A set() 0 6 1
A deleteMultiple() 0 10 2
1
<?php
2
3
namespace Glorand\Model\Settings\Managers;
4
5
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
6
7
/**
8
 * Class FieldSettingsManager
9
 * @package Glorand\Model\Settings\Managers
10
 * @property \Illuminate\Database\Eloquent\Model|\Glorand\Model\Settings\Traits\HasSettingsField $model
11
 */
12
class FieldSettingsManager extends AbstractSettingsManager
13
{
14
    /**
15
     * @param array $settings
16
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
17
     */
18 11
    public function apply(array $settings = []): SettingsManagerContract
19
    {
20 11
        $this->model->{$this->model->getSettingsFieldName()} = json_encode($settings);
21 11
        if ($this->model->isPersistSettings()) {
22 11
            $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

22
            $this->model->/** @scrutinizer ignore-call */ 
23
                          save();
Loading history...
23
        }
24
25 11
        return $this;
26
    }
27
28
    /**
29
     * @param string|null $path
30
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
31
     */
32 3
    public function delete(string $path = null): SettingsManagerContract
33
    {
34 3
        if (!$path) {
35 3
            $settings = [];
36
        } else {
37 1
            $settings = $this->all();
38 1
            array_forget($settings, $path);
0 ignored issues
show
Deprecated Code introduced by
The function array_forget() has been deprecated: Arr::forget() 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

38
            /** @scrutinizer ignore-deprecated */ array_forget($settings, $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...
39
        }
40
41 3
        $this->apply($settings);
42
43 3
        return $this;
44
    }
45
46
    /**
47
     * @param iterable $paths
48
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
49
     */
50 1
    public function deleteMultiple(iterable $paths): SettingsManagerContract
51
    {
52 1
        $settings = $this->all();
53 1
        foreach ($paths as $path) {
54 1
            array_forget($settings, $path);
0 ignored issues
show
Deprecated Code introduced by
The function array_forget() has been deprecated: Arr::forget() 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

54
            /** @scrutinizer ignore-deprecated */ array_forget($settings, $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...
55
        }
56
57 1
        $this->apply($settings);
58
59 1
        return $this;
60
    }
61
62
    /**
63
     * @param string $path
64
     * @param mixed $value
65
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
66
     */
67 2
    public function set(string $path, $value): SettingsManagerContract
68
    {
69 2
        $settings = $this->all();
70 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

70
        /** @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...
71
72 2
        return $this->apply($settings);
73
    }
74
}
75