Completed
Pull Request — master (#14)
by Vytautas
64:55
created

SettingsCollection::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Svycka\Settings\Collection;
4
5
use Svycka\Settings\Entity\SettingInterface;
6
use Svycka\Settings\Exception\RuntimeException;
7
use Svycka\Settings\Exception\SettingDoesNotExistException;
8
use Svycka\Settings\Options\CollectionOptionsInterface;
9
use Svycka\Settings\Provider\OwnerProviderInterface;
10
use Svycka\Settings\Storage\StorageAdapterInterface;
11
use Svycka\Settings\Type\TypesManager;
12
13
/**
14
 * @author Vytautas Stankus <[email protected]>
15
 * @license MIT
16
 */
17
class SettingsCollection implements CollectionInterface
18
{
19
    /**
20
     * @var StorageAdapterInterface
21
     */
22
    protected $adapter;
23
24
    /**
25
     * @var OwnerProviderInterface
26
     */
27
    protected $ownerProvider;
28
29
    /**
30
     * @var CollectionOptionsInterface
31
     */
32
    protected $options;
33
34
    /**
35
     * @var TypesManager
36
     */
37
    protected $typesManager;
38
39
    /**
40
     * @param CollectionOptionsInterface $options
41
     * @param StorageAdapterInterface    $adapter
42
     * @param OwnerProviderInterface     $owner_provider
43
     * @param TypesManager               $typesManager
44
     */
45 13
    public function __construct(
46
        CollectionOptionsInterface $options,
47
        StorageAdapterInterface $adapter,
48
        OwnerProviderInterface $owner_provider,
49
        TypesManager $typesManager
50
    ) {
51 13
        $this->adapter       = $adapter;
52 13
        $this->ownerProvider = $owner_provider;
53 13
        $this->options       = $options;
54 13
        $this->typesManager  = $typesManager;
55 13
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3
    public function setValue($name, $value)
61
    {
62
        // check if setting is configured
63 3
        $this->getSettingOptions($name);
64
65 2
        $this->adapter->set($this, $this->ownerProvider->getIdentifier(), $name, $value);
66 2
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 5
    public function getValue($name)
72
    {
73 5
        $settingOptions = $this->getSettingOptions($name);
74
75 4
        $setting = $this->adapter->get($this, $this->ownerProvider->getIdentifier(), $name);
76
77 4
        if ($setting instanceof SettingInterface) {
78 1
            return $setting->getValue();
79
        }
80
81 3
        if (array_key_exists('default_value', $settingOptions)) {
82 2
            return $settingOptions['default_value'];
83
        }
84
85
        // if setting is configured without default value return null
86 1
        return null;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 2
    public function getList()
93
    {
94 2
        $settings = [];
95 2
        $storedSettings = $this->adapter->getList($this, $this->ownerProvider->getIdentifier());
96
97
        /** @var SettingInterface $setting */
98 2
        foreach ($storedSettings as $setting) {
99 1
            $settings[$setting->getName()] = $setting->getValue();
100 2
        }
101
102 2
        $notSet = array_diff_key($this->options->getSettings(), $settings);
103
104 2
        foreach ($notSet as $name => $setting) {
105 2
            if (isset($setting['default_value'])) {
106 2
                $settings[$name] = $setting['default_value'];
107 2
            } else {
108 1
                $settings[$name] = null;
109
            }
110 2
        }
111
112 2
        return $settings;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 4
    public function isValid($name, $value)
119
    {
120 4
        $setting = $this->getSettingOptions($name);
121
122 3
        if (!isset($setting['type'])) {
123 1
            throw new RuntimeException('Missing "type" option in setting configuration');
124
        }
125
126 2
        if (!$this->typesManager->has($setting['type'])) {
127 1
            throw new RuntimeException(sprintf(
128 1
                "SettingType '%s' does not exist",
129 1
                is_string($setting['type']) ? $setting['type'] : gettype($setting['type'])
130 1
            ));
131
        }
132
133 1
        $type = $this->typesManager->get($setting['type']);
134
135
        if (!empty($setting['options']) && is_array($setting)) {
136
            $type->setOptions($setting['options']);
137
        }
138
139
        return $type->isValid($value);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 11
    public function getOptions()
146
    {
147 11
        return $this->options;
148
    }
149
150
    /**
151
     * @param $name
152
     *
153
     * @return array
154
     */
155 11
    private function getSettingOptions($name)
156
    {
157 11
        $settings = $this->options->getSettings();
158
159 11
        if (!array_key_exists($name, $settings)) {
160 2
            throw new SettingDoesNotExistException(sprintf(
161 2
                "Collection '%s' doesn't have '%s' setting.",
162 2
                $this->options->getName(),
163
                $name
164 2
            ));
165
        }
166
167 9
        if (!is_array($settings[$name])) {
168 1
            throw new RuntimeException(sprintf(
169 1
                "Failed to read '%s' setting configuration in '%s' collection.",
170 1
                $name,
171 1
                $this->options->getName()
172 1
            ));
173
        }
174
175 8
        return $settings[$name];
176
    }
177
}
178