WritableService::setSettingBool()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Bavix\Settings\Services;
4
5
use Bavix\Settings\Models\Setting;
6
use Bavix\Settings\Interfaces\Settingable as Model;
7
8
class WritableService
9
{
10
11
    /**
12
     * @param Model $model
13
     * @param string $key
14
     * @param int|null $value
15
     * @return Setting
16
     */
17 5
    public function setSettingInt(Model $model, string $key, ?int $value = null): Setting
18
    {
19 5
        return $this->setSetting($model, $key, 'int', $value);
20
    }
21
22
    /**
23
     * @param Model $model
24
     * @param string $key
25
     * @param float|null $value
26
     * @return float|null
27
     */
28 3
    public function setSettingFloat(Model $model, string $key, ?float $value = null): Setting
29
    {
30 3
        return $this->setSetting($model, $key, 'float', $value);
31
    }
32
33
    /**
34
     * @param Model $model
35
     * @param string $key
36
     * @param bool|null $value
37
     * @return bool|null
38
     */
39 3
    public function setSettingBool(Model $model, string $key, ?bool $value = null): Setting
40
    {
41 3
        return $this->setSetting($model, $key, 'bool', $value);
42
    }
43
44
    /**
45
     * @param Model $model
46
     * @param string $key
47
     * @param string|null $value
48
     * @return string|null
49
     */
50 3
    public function setSettingString(Model $model, string $key, ?string $value = null): Setting
51
    {
52 3
        return $this->setSetting($model, $key, 'string', $value);
53
    }
54
55
    /**
56
     * @param Model $model
57
     * @param string $key
58
     * @param array|null $value
59
     * @return array|null
60
     */
61 3
    public function setSettingArray(Model $model, string $key, ?array $value = null): Setting
62
    {
63 3
        return $this->setSetting($model, $key, 'array', $value);
64
    }
65
66
    /**
67
     * @param Model $model
68
     * @param string $key
69
     * @param string $cast
70
     * @param mixed $value
71
     * @return Setting
72
     */
73 19
    public function setSetting(Model $model, string $key, string $cast, $value): Setting
74
    {
75 19
        $setting = app(ReadableService::class)
76 19
            ->getSetting($model, $key);
77
78 19
        if (!$setting) {
79 19
            return app(SettingService::class)
80 19
                ->create($model, $key, $cast, $value);
81
        }
82
83 6
        $setting->update(compact('cast', 'value'));
84 6
        return $setting;
85
    }
86
87
}
88