Completed
Push — master ( ea4e28...f4a6ba )
by Eric
08:48
created

LazyRegistry::offsetSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\RegistryBundle\Model;
13
14
use Lug\Bundle\RegistryBundle\Exception\LazyServiceAlreadyExistsException;
15
use Lug\Bundle\RegistryBundle\Exception\LazyServiceNotFoundException;
16
use Lug\Component\Registry\Model\RegistryInterface;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class LazyRegistry implements LazyRegistryInterface
23
{
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
29
    /**
30
     * @var RegistryInterface
31
     */
32
    private $registry;
33
34
    /**
35
     * @var string[]
36
     */
37
    private $services = [];
38
39
    /**
40
     * @param ContainerInterface $container
41
     * @param RegistryInterface  $registry
42
     * @param string[]           $services
43
     */
44 16
    public function __construct(ContainerInterface $container, RegistryInterface $registry, array $services = [])
45
    {
46 16
        $this->container = $container;
47 16
        $this->registry = $registry;
48
49 16
        foreach ($services as $type => $service) {
50 16
            $this->setLazy($type, $service);
51 16
        }
52 16
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 16
    public function hasLazy($type)
58
    {
59 16
        return array_key_exists($type, $this->services) && $this->container->has($this->services[$type]);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 10
    public function getLazy($type)
66
    {
67 10
        if (!$this->hasLazy($type)) {
68 2
            throw new LazyServiceNotFoundException(sprintf('The lazy service "%s" could not be found.', $type));
69
        }
70
71 8
        return $this->services[$type];
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 16
    public function setLazy($type, $service)
78
    {
79 16
        if ($this->hasLazy($type)) {
80 1
            throw new LazyServiceAlreadyExistsException(sprintf('The lazy service "%s" already exists.', $type));
81
        }
82
83 16
        $this->services[$type] = $service;
84 16
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 3
    public function removeLazy($type)
90
    {
91 3
        if (!$this->hasLazy($type)) {
92 1
            throw new LazyServiceNotFoundException(sprintf('The lazy service "%s" could not be found.', $type));
93
        }
94
95 2
        unset($this->services[$type]);
96
97 2
        if (isset($this->registry[$type])) {
98 1
            unset($this->registry[$type]);
99 1
        }
100 2
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 2
    public function offsetExists($offset)
106
    {
107 2
        return $this->hasLazy($offset) || isset($this->registry[$offset]);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 3
    public function offsetGet($offset)
114
    {
115 3
        $this->lazyLoad($offset);
116
117 2
        return $this->registry[$offset];
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 2
    public function offsetSet($offset, $value)
124
    {
125 2
        if ($this->hasLazy($offset)) {
126 1
            $this->lazyLoad($offset);
127 1
        }
128
129 2
        $this->registry[$offset] = $value;
130 1
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 1
    public function offsetUnset($offset)
136
    {
137 1
        if ($this->hasLazy($offset)) {
138 1
            $this->lazyLoad($offset);
139 1
        }
140
141 1
        unset($this->registry[$offset]);
142
143 1
        if ($this->hasLazy($offset)) {
144 1
            $this->removeLazy($offset);
145 1
        }
146 1
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 6
    public function count()
152
    {
153 6
        $this->load();
154
155 6
        return count($this->registry);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 6
    public function getIterator()
162
    {
163 6
        $this->load();
164
165 6
        return $this->registry->getIterator();
166
    }
167
168 6
    private function load()
169
    {
170 6
        foreach (array_keys($this->services) as $type) {
171 4
            $this->lazyLoad($type);
172 6
        }
173 6
    }
174
175
    /**
176
     * @param string $type
177
     */
178 8
    private function lazyLoad($type)
179
    {
180 8
        if (!isset($this->registry[$type])) {
181 8
            $this->registry[$type] = $this->container->get($this->getLazy($type));
182 7
        }
183 7
    }
184
}
185