ReadableService::getSettingString()   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 ReadableService
9
{
10
11
    /**
12
     * @param Model $model
13
     * @param string $key
14
     * @return Setting|null
15
     */
16 31
    public function getSetting(Model $model, string $key): ?Setting
17
    {
18 31
        foreach ($model->settings as $setting) {
19 31
            if ($setting->key === $key) {
20 31
                return $setting;
21
            }
22
        }
23
24 21
        return null;
25
    }
26
27
    /**
28
     * @param Model $model
29
     * @param string $key
30
     * @param int|null $default
31
     * @return int|null
32
     */
33 5
    public function getSettingInt(Model $model, string $key, ?int $default = null): ?int
34
    {
35 5
        return $this->getSetting($model, $key)->value ?? $default;
36
    }
37
38
    /**
39
     * @param Model $model
40
     * @param string $key
41
     * @param float|null $default
42
     * @return float|null
43
     */
44 5
    public function getSettingFloat(Model $model, string $key, ?float $default = null): ?float
45
    {
46 5
        return $this->getSetting($model, $key)->value ?? $default;
47
    }
48
49
    /**
50
     * @param Model $model
51
     * @param string $key
52
     * @param bool|null $default
53
     * @return bool|null
54
     */
55 5
    public function getSettingBool(Model $model, string $key, ?bool $default = null): ?bool
56
    {
57 5
        return $this->getSetting($model, $key)->value ?? $default;
58
    }
59
60
    /**
61
     * @param Model $model
62
     * @param string $key
63
     * @param string|null $default
64
     * @return string|null
65
     */
66 5
    public function getSettingString(Model $model, string $key, ?string $default = null): ?string
67
    {
68 5
        return $this->getSetting($model, $key)->value ?? $default;
69
    }
70
71
    /**
72
     * @param Model $model
73
     * @param string $key
74
     * @param array|null $default
75
     * @return array|null
76
     */
77 5
    public function getSettingArray(Model $model, string $key, ?array $default = null): ?array
78
    {
79 5
        return $this->getSetting($model, $key)->value ?? $default;
80
    }
81
82
}
83