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

TableSettingsManager::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
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
use Illuminate\Support\Arr;
8
9
/**
10
 * Class TableSettingsManager
11
 * @package Glorand\Model\Settings\Managers
12
 * @property  \Illuminate\Database\Eloquent\Model|\Glorand\Model\Settings\Traits\HasSettingsTable $model
13
 */
14
class TableSettingsManager extends AbstractSettingsManager
15
{
16
    /**
17
     * @param array $settings
18
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
19 11
     */
20
    public function apply(array $settings = []): SettingsManagerContract
21 11
    {
22 11
        if (!$modelSettings = $this->model->modelSettings()->first()) {
23 11
            $modelSettings = new ModelSettings();
24
            $modelSettings->model()->associate($this->model);
1 ignored issue
show
Bug introduced by
It seems like $this->model can also be of type Glorand\Model\Settings\Traits\HasSettingsTable; however, parameter $model of Illuminate\Database\Eloq...ns\MorphTo::associate() does only seem to accept Illuminate\Database\Eloquent\Model, maybe add an additional type check? ( Ignorable by Annotation )

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

24
            $modelSettings->model()->associate(/** @scrutinizer ignore-type */ $this->model);
Loading history...
25 11
        }
26 11
        $modelSettings->settings = $settings;
27
        $modelSettings->save();
28 11
29
        return $this;
30
    }
31
32
    /**
33
     * @param string|null $path
34
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
35
     * @throws \Exception
36 2
     */
37
    public function delete(string $path = null): SettingsManagerContract
38 2
    {
39
        if (!$path) {
40 2
            /** @var ModelSettings $modelSettings */
41 2
            if ($modelSettings = $this->model->modelSettings()->first()) {
42
                $modelSettings->delete();
43
            }
44 1
        } else {
45 1
            $settings = $this->all();
46 1
            Arr::forget($settings, $path);
47
            $this->apply($settings);
48
        }
49 2
50
        return $this;
51
    }
52
53
    /**
54
     * @param string $path
55
     * @param mixed $value
56 1
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
57
     */
58 1
    public function set(string $path, $value): SettingsManagerContract
59 1
    {
60 1
        $settings = $this->all();
61
        Arr::set($settings, $path, $value);
62
63 1
        return $this->apply($settings);
64
    }
65
}
66