Completed
Push — master ( 392874...829d42 )
by Albert
05:23
created

SettingRepository::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
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 \'%\' 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 = [];
48
49
        array_walk($this->findAll(), function (Setting $setting) use (&$settings) {
0 ignored issues
show
Bug introduced by
$this->findAll() cannot be passed to array_walk() as the parameter $array expects a reference.
Loading history...
50
            $settings[$setting->getName()] = $setting;
51
        });
52
53
        $this->settings = $settings;
54
    }
55
}
56