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

TableSettingsManager::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 9
cp 0
crap 6
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\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