SettingRepository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 7
c 2
b 1
f 1
lcom 1
cbo 2
dl 0
loc 47
ccs 0
cts 29
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 0 8 2
A offsetGet() 0 8 2
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A loadSettings() 0 11 1
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