InMemoryDiscovery   B
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 89.66%

Importance

Changes 13
Bugs 2 Features 6
Metric Value
c 13
b 2
f 6
dl 0
loc 241
wmc 39
lcom 1
cbo 8
ccs 78
cts 87
cp 0.8966
rs 8.2857

18 Methods

Rating   Name   Duplication   Size   Complexity  
A addBindingType() 0 8 2
A removeBindingType() 0 8 1
A removeBindingTypes() 0 5 1
A hasBindingType() 0 6 1
A getBindingType() 0 10 2
A hasBindingTypes() 0 4 1
A getBindingTypes() 0 4 1
A addBinding() 0 8 1
A findBindings() 0 16 3
A getBindings() 0 12 3
A removeAllBindings() 0 4 1
B removeBindingsThatMatch() 0 14 5
A removeBindingsWithTypeName() 0 8 2
B removeBindingsWithTypeNameThatMatch() 0 16 5
A hasAnyBinding() 0 4 1
A hasBindingsThatMatch() 0 12 4
A hasBindingsWithTypeName() 0 4 1
A hasBindingsWithTypeNameThatMatch() 0 14 4
1
<?php
2
3
/*
4
 * This file is part of the puli/discovery package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Discovery;
13
14
use Puli\Discovery\Api\Binding\Binding;
15
use Puli\Discovery\Api\Type\BindingType;
16
use Puli\Discovery\Api\Type\DuplicateTypeException;
17
use Puli\Discovery\Api\Type\NoSuchTypeException;
18
use Webmozart\Assert\Assert;
19
use Webmozart\Expression\Expr;
20
use Webmozart\Expression\Expression;
21
22
/**
23
 * A discovery that holds the bindings in memory.
24
 *
25
 * @since  1.0
26
 *
27
 * @author Bernhard Schussek <[email protected]>
28
 */
29
class InMemoryDiscovery extends AbstractEditableDiscovery
30
{
31
    /**
32
     * @var BindingType[]
33
     */
34
    private $types = array();
35
36
    /**
37
     * @var Binding[][]
38
     */
39
    private $bindingsByTypeName = array();
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 27
    public function addBindingType(BindingType $type)
45
    {
46 27
        if (isset($this->types[$type->getName()])) {
47 1
            throw DuplicateTypeException::forTypeName($type->getName());
48
        }
49
50 27
        $this->types[$type->getName()] = $type;
51 27
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 4
    public function removeBindingType($typeName)
57
    {
58 4
        Assert::stringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s');
59
60 3
        unset($this->types[$typeName]);
61
62 3
        $this->removeBindingsWithTypeName($typeName);
63 3
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 2
    public function removeBindingTypes()
69
    {
70 2
        $this->types = array();
71 2
        $this->bindingsByTypeName = array();
72 2
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 5
    public function hasBindingType($typeName)
78
    {
79 5
        Assert::stringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s');
80
81 4
        return isset($this->types[$typeName]);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 21
    public function getBindingType($typeName)
88
    {
89 21
        Assert::stringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s');
90
91 20
        if (!isset($this->types[$typeName])) {
92 2
            throw NoSuchTypeException::forTypeName($typeName);
93
        }
94
95 18
        return $this->types[$typeName];
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function hasBindingTypes()
102
    {
103
        return count($this->types) > 0;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 3
    public function getBindingTypes()
110
    {
111 3
        return array_values($this->types);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 16
    public function addBinding(Binding $binding)
118
    {
119 16
        $typeName = $binding->getTypeName();
120
121 16
        $this->initializeBinding($binding);
122
123 14
        $this->bindingsByTypeName[$typeName][] = $binding;
124 14
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 10
    public function findBindings($typeName, Expression $expr = null)
130
    {
131 10
        Assert::stringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s');
132
133 9
        if (!isset($this->bindingsByTypeName[$typeName])) {
134 3
            return array();
135
        }
136
137 7
        $bindings = $this->bindingsByTypeName[$typeName];
138
139 7
        if (null !== $expr) {
140 1
            $bindings = Expr::filter($bindings, $expr);
141
        }
142
143 7
        return array_values($bindings);
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 15
    public function getBindings()
150
    {
151 15
        $bindings = array();
152
153 15
        foreach ($this->bindingsByTypeName as $bindingsOfType) {
154 7
            foreach ($bindingsOfType as $binding) {
155 7
                $bindings[] = $binding;
156
            }
157
        }
158
159 15
        return $bindings;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 2
    protected function removeAllBindings()
166
    {
167 2
        $this->bindingsByTypeName = array();
168 2
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173 1
    protected function removeBindingsThatMatch(Expression $expr)
174
    {
175 1
        foreach ($this->bindingsByTypeName as $typeName => $bindings) {
176 1
            foreach ($bindings as $key => $binding) {
177 1
                if ($expr->evaluate($binding)) {
178 1
                    unset($this->bindingsByTypeName[$typeName][$key]);
179
                }
180
            }
181
182 1
            if (0 === count($this->bindingsByTypeName[$typeName])) {
183 1
                unset($this->bindingsByTypeName[$typeName]);
184
            }
185
        }
186 1
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 6
    protected function removeBindingsWithTypeName($typeName)
192
    {
193 6
        if (!isset($this->bindingsByTypeName[$typeName])) {
194 4
            return;
195
        }
196
197 2
        unset($this->bindingsByTypeName[$typeName]);
198 2
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203 3
    protected function removeBindingsWithTypeNameThatMatch($typeName, Expression $expr)
204
    {
205 3
        if (!isset($this->bindingsByTypeName[$typeName])) {
206 2
            return;
207
        }
208
209 1
        foreach ($this->bindingsByTypeName[$typeName] as $key => $binding) {
210 1
            if ($expr->evaluate($binding)) {
211 1
                unset($this->bindingsByTypeName[$typeName][$key]);
212
            }
213
        }
214
215 1
        if (0 === count($this->bindingsByTypeName[$typeName])) {
216
            unset($this->bindingsByTypeName[$typeName]);
217
        }
218 1
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223 2
    protected function hasAnyBinding()
224
    {
225 2
        return count($this->bindingsByTypeName) > 0;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    protected function hasBindingsThatMatch(Expression $expr)
232
    {
233
        foreach ($this->bindingsByTypeName as $typeName => $bindings) {
234
            foreach ($bindings as $key => $binding) {
235
                if ($expr->evaluate($binding)) {
236
                    return true;
237
                }
238
            }
239
        }
240
241
        return false;
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247 2
    protected function hasBindingsWithTypeName($typeName)
248
    {
249 2
        return !empty($this->bindingsByTypeName[$typeName]);
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255 2
    protected function hasBindingsWithTypeNameThatMatch($typeName, Expression $expr)
256
    {
257 2
        if (!isset($this->bindingsByTypeName[$typeName])) {
258 1
            return false;
259
        }
260
261 1
        foreach ($this->bindingsByTypeName[$typeName] as $binding) {
262 1
            if ($expr->evaluate($binding)) {
263 1
                return true;
264
            }
265
        }
266
267 1
        return false;
268
    }
269
}
270