Completed
Push — master ( 982f0c...377cb6 )
by Kamil
05:13 queued 02:16
created

InMemorySettingRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Galileo\SettingBundle\Lib\Infrastructure\Internal;
4
5
use Galileo\SettingBundle\Lib\Model\Setting;
6
use Galileo\SettingBundle\Lib\Model\SettingRepositoryInterface;
7
use Galileo\SettingBundle\Lib\Model\ValueObject\Key;
8
use Galileo\SettingBundle\Lib\Model\ValueObject\Section;
9
use Galileo\SettingBundle\Lib\Model\ValueObject\Value;
10
11
class InMemorySettingRepository implements SettingRepositoryInterface
12
{
13
    private $values = [];
14
15
16
    public function __construct($settingPlainRows)
17
    {
18
        foreach ($settingPlainRows as $key) {
19
            $name = $key[0];
20
            $value = $key[1];
21
            $section = $key[2];
22
            $this->set($name, $value, $section);
23
        }
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function findWithinSection(Key $settingKey, Section $section)
30
    {
31
        $key = $settingKey->key();
32
        $sectionName = $section->name();
33
34
        if (!isset($this->values[$key][$sectionName])) {
35
            return null;
36
        }
37
38
        return Setting::issue(
39
            $settingKey,
40
            $this->values[$key][$sectionName],
41
            new Section($sectionName)
42
        );
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function save(Setting $setting)
49
    {
50
        $this->set($setting->name(), $setting->value(), $setting->section());
51
    }
52
53
    /**
54
     * @param $name
55
     * @param $value
56
     * @param $section
57
     */
58
    private function set($name, $value, $section)
59
    {
60
        $section = $section ? $section : Section::EMPTY_SECTION;
61
62
        $this->values[$name][$section] = new Value($value);
63
    }
64
}
65