SettingRepositoryProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
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\Provider;
20
21
use SettingsManager\Behavior\SettingProviderTrait;
22
use SettingsManager\Contract\SettingRepository;
23
use SettingsManager\Contract\SettingProvider;
24
use SettingsManager\Model\SettingValue;
25
26
/**
27
 * Settings Repository provider
28
 *
29
 * Reads settings from an instance that implements the `Contract\SettingRepositoryInterface`
30
 *
31
 * @author Casey McLaughlin <[email protected]>
32
 */
33
class SettingRepositoryProvider implements SettingProvider
34
{
35
    use SettingProviderTrait;
36
37
    /**
38
     * @var SettingRepository
39
     */
40
    private $repository;
41
42
    /**
43
     * @var string
44
     */
45
    private $name;
46
47
    /**
48
     * @var string
49
     */
50
    private $description;
51
52
    /**
53
     * DoctrineOrmProvider constructor.
54
     *
55
     * @param SettingRepository $repository
56
     * @param string $name
57
     * @param string $description
58
     */
59 27
    public function __construct(
60
        SettingRepository $repository,
61
        string $name = 'database',
62
        string $description = 'Settings stored in database'
63
    ) {
64 27
        $this->repository = $repository;
65 27
        $this->name = $name;
66 27
        $this->description = $description;
67 27
    }
68
69
    /**
70
     * @return string
71
     */
72 27
    public function getName(): string
73
    {
74 27
        return $this->name;
75
    }
76
77
    /**
78
     * @return string
79
     */
80 3
    public function getDisplayName(): string
81
    {
82 3
        return $this->description;
83
    }
84
85
    /**
86
     * Return a key/value set of setting names/values
87
     *
88
     * @return iterable|SettingValue[]
89
     */
90 3
    public function getSettingValues(): iterable
91
    {
92 3
        foreach ($this->repository->listValues() as $name => $value) {
93 3
            $out[] = new SettingValue($name, $this, true, $value);
94
        }
95
96 3
        return $out ?? [];
97
    }
98
99 18
    public function findValueInstance(string $settingName): ?SettingValue
100
    {
101 18
        return ($value = $this->repository->findValue($settingName))
102 9
            ? new SettingValue($settingName, $this, true, $value)
103 18
            : null;
104
    }
105
}
106