Passed
Push — master ( c22c2e...c08184 )
by Бабичев
03:17
created

SettingService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
dl 0
loc 55
ccs 17
cts 17
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 21 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 Illuminate\Database\Eloquent\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 18
    public function create(Model $model, string $key, string $cast, $value): Setting
21
    {
22
        /**
23
         * @var HasSettings $model
24
         * @var Setting $setting
25
         */
26 18
        $setting = $model->settings()
27 18
            ->create(\compact('key', 'cast', 'value'));
28
29
        /**
30
         * @var Collection $collection
31
         */
32 18
        $collection = $model->settings;
33 18
        $collection->push($setting);
34
35 18
        return $setting;
36
    }
37
38
    /**
39
     * @param Model $model
40
     * @param string $key
41
     * @return bool
42
     * @throws
43
     */
44 1
    public function delete(Model $model, string $key): bool
45
    {
46 1
        $setting = app(ReadableService::class)
47 1
            ->getSetting($model, $key);
48
49 1
        if ($setting) {
50
            /**
51
             * @var Collection $collection
52
             */
53 1
            $collection = $model->settings;
54 1
            foreach ($collection as $index => $item) {
55 1
                if ($item === $setting) {
56 1
                    $collection->forget($index);
57 1
                    break;
58
                }
59
            }
60
61 1
            return $setting->delete();
62
        }
63
64 1
        return false;
65
    }
66
67
}
68