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

SectionQuery::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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
    public function __construct(SettingRepositoryInterface $settingRepository, Section $section)
23
    {
24
        $this->settingRepository = $settingRepository;
25
        $this->section = $section;
26
    }
27
28
    public function get($key, $default = null)
29
    {
30
        $chooseValue = SettingValueChooser::choose($this->find(new Key($key)), $default);
31
32
        return $chooseValue->value();
33
    }
34
35
    public function set($key, $value)
36
    {
37
        $aKey = Key::fromString($key);
38
        $aValue = Value::fromString($value);
39
40
        $setting = $this->find($aKey);
41
42
        if (null === $setting) {
43
            $setting = Setting::issue($aKey, $aValue, $this->section);
44
        }
45
46
        $setting->changeValue($aValue);
47
48
        $this->settingRepository->save($setting);
49
    }
50
51
    /**
52
     * @param Key $key
53
     * @return Setting|null
54
     */
55
    private function find(Key $key)
56
    {
57
        return $this->settingRepository->findWithinSection($key, $this->section);
58
    }
59
}
60