SettingRepository::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Albert221\Blog\Repository\Database;
4
5
use Albert221\Blog\Entity\Setting;
6
use Albert221\Blog\Repository\SettingRepositoryInterface;
7
use Doctrine\ORM\EntityRepository;
8
use InvalidArgumentException;
9
10
class SettingRepository extends EntityRepository implements SettingRepositoryInterface
11
{
12
    /**
13
     * @var array Settings
14
     */
15
    protected $settings;
16
17
    public function offsetExists($offset)
18
    {
19
        if (is_null($this->settings)) {
20
            $this->loadSettings();
21
        }
22
        
23
        return isset($this->settings[$offset]);
24
    }
25
26
    public function offsetGet($offset)
27
    {
28
        if (!$this->offsetExists($offset)) {
29
            throw new InvalidArgumentException(sprintf('Setting \'%s\' cannot be found.', $offset));
30
        }
31
        
32
        return $this->settings[$offset];
33
    }
34
35
    public function offsetSet($offset, $value)
36
    {
37
        // Do nothing
38
    }
39
40
    public function offsetUnset($offset)
41
    {
42
        // Do nothing
43
    }
44
45
    protected function loadSettings()
46
    {
47
        $settings = $this->findAll();
48
        $newSettings = [];
49
50
        array_walk($settings, function (Setting $setting) use (&$newSettings) {
51
            $newSettings[$setting->getName()] = $setting;
52
        });
53
54
        $this->settings = $newSettings;
55
    }
56
}
57