SettingProviderTrait::findValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * Settings Manager
5
 *
6
 * @license http://opensource.org/licenses/MIT
7
 * @link https://github.com/caseyamcl/settings-manager
8
 * @package caseyamcl/settings-manager
9
 * @author Casey McLaughlin <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 *
14
 *  ------------------------------------------------------------------
15
 */
16
17
declare(strict_types=1);
18
19
namespace SettingsManager\Behavior;
20
21
use SettingsManager\Exception\SettingValueNotFoundException;
22
use SettingsManager\Model\SettingValue;
23
24
/**
25
 * Trait SettingProviderTrait
26
 *
27
 * @author Casey McLaughlin <[email protected]>
28
 */
29
trait SettingProviderTrait
30
{
31
    /**
32
     * Get a value for setting or throw exception
33
     *
34
     * @param string $settingName
35
     * @return SettingValue
36
     */
37 27
    public function getValueInstance(string $settingName): SettingValue
38
    {
39 27
        if ($value = $this->findValueInstance($settingName)) {
40 15
            return $value;
41
        } else {
42 12
            throw SettingValueNotFoundException::fromName($settingName);
43
        }
44
    }
45
46
    /**
47
     * Get a setting value from name or throw exception
48
     *
49
     * @param string $settingName
50
     * @return mixed
51
     */
52 3
    public function getValue(string $settingName)
53
    {
54 3
        return $this->getValueInstance($settingName)->getValue();
55
    }
56
57
    /**
58
     * Find value for setting
59
     *
60
     * @param string $settingName
61
     * @return mixed|null
62
     */
63 24
    public function findValue(string $settingName)
64
    {
65 24
        if ($setting = $this->findValueInstance($settingName)) {
66 12
            return $setting->getValue();
67
        } else {
68 12
            return null;
69
        }
70
    }
71
72
    /**
73
     * Find a setting value instance
74
     *
75
     * @param string $settingName
76
     * @return SettingValue|null
77
     */
78
    abstract public function findValueInstance(string $settingName): ?SettingValue;
79
}
80