SettingService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
dl 0
loc 51
ccs 16
cts 16
cp 1
rs 10
c 1
b 0
f 1
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 1
A delete() 0 17 4
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