SettingService::delete()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Settings\Services;
4
5
use Bavix\Settings\Models\Setting;
6
use Bavix\Settings\Traits\HasSettings;
7
use Illuminate\Database\Eloquent\Collection;
8
use Bavix\Settings\Interfaces\Settingable as Model;
9
10
class SettingService
11
{
12
13
    /**
14
     * @param Model $model
15
     * @param string $key
16
     * @param string $cast
17
     * @param mixed $value
18
     * @return Setting
19
     */
20 19
    public function create(Model $model, string $key, string $cast, $value): Setting
21
    {
22
        /**
23
         * @var HasSettings $model
24
         * @var Setting $setting
25
         */
26 19
        $setting = $model->settings()
27 19
            ->create(\compact('key', 'cast', 'value'));
28
29
        /**
30
         * @var Collection $collection
31
         */
32 19
        $collection = $model->settings;
33 19
        $collection->push($setting);
34
35 19
        return $setting;
36
    }
37
38
    /**
39
     * @param Model $model
40
     * @param string $key
41
     * @return bool
42
     * @throws
43
     */
44 2
    public function delete(Model $model, string $key): bool
45
    {
46 2
        $setting = app(ReadableService::class)
47 2
            ->getSetting($model, $key);
48
49 2
        if ($setting) {
50 2
            foreach ($model->settings as $index => $item) {
51 2
                if ($item === $setting) {
52 2
                    $model->settings->forget($index);
53 2
                    break;
54
                }
55
            }
56
57 2
            return $setting->delete();
58
        }
59
60 2
        return false;
61
    }
62
63
}
64