Passed
Pull Request — master (#1)
by Gombos
01:47
created

TableSettingsManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 35
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 10 2
A set() 0 6 1
A delete() 0 13 3
1
<?php
2
3
namespace Glorand\Model\Settings\Managers;
4
5
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
6
use Glorand\Model\Settings\Models\ModelSettings;
7
8
class TableSettingsManager extends AbstractSettingsManager
9
{
10
    public function apply(array $settings = []): SettingsManagerContract
11
    {
12
        if (!$modelSettings = $this->model->modelSettings) {
13
            $modelSettings = new ModelSettings();
14
            $modelSettings->model()->associate($this->model);
15
        }
16
        $modelSettings->settings = $settings;
17
        $modelSettings->save();
18
19
        return $this;
20
    }
21
22
    public function delete(string $path = null): SettingsManagerContract
23
    {
24
        if (!$path) {
25
            if ($this->model->modelSettings) {
26
                $this->model->modelSettings->delete();
27
            }
28
        } else {
29
            $settings = $this->all();
30
            array_forget($settings, $path);
31
            $this->apply($settings);
32
        }
33
34
        return $this;
35
    }
36
37
    public function set(string $path, $value): SettingsManagerContract
38
    {
39
        $settings = $this->all();
40
        array_set($settings, $path, $value);
41
42
        return $this->apply($settings);
43
    }
44
}
45