InMemorySettingRepository::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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 7
    public function __construct($settingPlainRows)
17
    {
18 7
        foreach ($settingPlainRows as $key) {
19 7
            $name = $key[0];
20 7
            $value = $key[1];
21 7
            $section = $key[2];
22 7
            $this->set($name, $value, $section);
23
        }
24 7
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 6
    public function findWithinSection(Key $settingKey, Section $section)
30
    {
31 6
        $key = $settingKey->key();
32 6
        $sectionName = $section->name();
33
34 6
        if (!isset($this->values[$key][$sectionName])) {
35 3
            return null;
36
        }
37
38 4
        return Setting::issue(
39
            $settingKey,
40 4
            $this->values[$key][$sectionName],
41 4
            new Section($sectionName)
42
        );
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function save(Setting $setting)
49
    {
50 2
        $this->set($setting->name(), $setting->value(), $setting->section());
51 2
    }
52
53
    /**
54
     * @param $name
55
     * @param $value
56
     * @param $section
57
     */
58 7
    private function set($name, $value, $section)
59
    {
60 7
        $section = $section ? $section : Section::EMPTY_SECTION;
61
62 7
        $this->values[$name][$section] = new Value($value);
63 7
    }
64
}
65