SectionQuery::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Galileo\SettingBundle\Lib\Model;
4
5
use Galileo\SettingBundle\Lib\Model\ValueObject\Key;
6
use Galileo\SettingBundle\Lib\Model\ValueObject\Section;
7
use Galileo\SettingBundle\Lib\Model\ValueObject\SettingValueChooser;
8
use Galileo\SettingBundle\Lib\Model\ValueObject\Value;
9
10
class SectionQuery implements SettingBagInterface
11
{
12
    /**
13
     * @var SettingRepositoryInterface
14
     */
15
    private $settingRepository;
16
17
    /**
18
     * @var Section
19
     */
20
    private $section;
21
22 6
    public function __construct(SettingRepositoryInterface $settingRepository, Section $section)
23
    {
24 6
        $this->settingRepository = $settingRepository;
25 6
        $this->section = $section;
26 6
    }
27
28 6
    public function get($key, $default = null)
29
    {
30 6
        $chooseValue = SettingValueChooser::choose($this->find(new Key($key)), $default);
31
32 6
        return $chooseValue->value();
33
    }
34
35 2
    public function set($key, $value)
36
    {
37 2
        $aKey = Key::fromString($key);
38 2
        $aValue = Value::fromString($value);
39
40 2
        $setting = $this->find($aKey);
41
42 2
        if (null === $setting) {
43 1
            $setting = Setting::issue($aKey, $aValue, $this->section);
44
        }
45
46 2
        $setting->changeValue($aValue);
47
48 2
        $this->settingRepository->save($setting);
49 2
    }
50
51
    /**
52
     * @param Key $key
53
     * @return Setting|null
54
     */
55 6
    private function find(Key $key)
56
    {
57 6
        return $this->settingRepository->findWithinSection($key, $this->section);
58
    }
59
}
60