AbstractEditableDiscovery   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Test Coverage

Coverage 97.3%

Importance

Changes 15
Bugs 0 Features 6
Metric Value
wmc 16
c 15
b 0
f 6
lcom 3
cbo 3
dl 0
loc 176
ccs 36
cts 37
cp 0.973
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
removeAllBindings() 0 1 ?
removeBindingsThatMatch() 0 1 ?
removeBindingsWithTypeName() 0 1 ?
removeBindingsWithTypeNameThatMatch() 0 1 ?
hasBindingsWithTypeNameThatMatch() 0 1 ?
A removeBindings() 0 16 4
A hasBindings() 0 18 4
B initializeBinding() 0 22 5
A initializeBindings() 0 6 2
hasAnyBinding() 0 1 ?
hasBindingsThatMatch() 0 1 ?
hasBindingsWithTypeName() 0 1 ?
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\Binding\Initializer\BindingInitializer;
16
use Puli\Discovery\Api\EditableDiscovery;
17
use Puli\Discovery\Api\Type\BindingNotAcceptedException;
18
use Webmozart\Assert\Assert;
19
use Webmozart\Expression\Expression;
20
21
/**
22
 * Base class for editable discoveries.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
abstract class AbstractEditableDiscovery implements EditableDiscovery
29
{
30
    /**
31
     * @var BindingInitializer[]
32
     */
33
    private $initializers;
34
35
    /**
36
     * @var BindingInitializer[][]
37
     */
38
    private $initializersByBindingClass = array();
39
40
    /**
41
     * Creates a new discovery.
42
     *
43
     * @param BindingInitializer[] $initializers The binding initializers to
44
     *                                           apply to newly created or
45
     *                                           unserialized bindings.
46
     */
47 228
    public function __construct(array $initializers = array())
48
    {
49 228
        $this->initializers = $initializers;
50 228
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 56
    public function removeBindings($typeName = null, Expression $expr = null)
56
    {
57 56
        Assert::nullOrStringNotEmpty($typeName, 'The type name must be a non-empty string. Got: %s');
58
59 51
        if (null !== $typeName) {
60 34
            if (null !== $expr) {
61 17
                $this->removeBindingsWithTypeNameThatMatch($typeName, $expr);
62
            } else {
63 34
                $this->removeBindingsWithTypeName($typeName);
64
            }
65 17
        } elseif (null !== $expr) {
66 5
            $this->removeBindingsThatMatch($expr);
67
        } else {
68 12
            $this->removeAllBindings();
69
        }
70 51
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 40
    public function hasBindings($typeName = null, Expression $expr = null)
76
    {
77 40
        Assert::nullOrStringNotEmpty($typeName, 'The type class must be a non-empty string. Got: %s');
78
79 30
        if (null !== $typeName) {
80 20
            if (null !== $expr) {
81 10
                return $this->hasBindingsWithTypeNameThatMatch($typeName, $expr);
82
            }
83
84 10
            return $this->hasBindingsWithTypeName($typeName);
85
        }
86
87 10
        if (null !== $expr) {
88
            return $this->hasBindingsThatMatch($expr);
89
        }
90
91 10
        return $this->hasAnyBinding();
92
    }
93
94
    /**
95
     * Removes all bindings from the discovery.
96
     */
97
    abstract protected function removeAllBindings();
98
99
    /**
100
     * Removes all bindings from the discovery that match an expression.
101
     *
102
     * @param Expression $expr The expression to filter by.
103
     */
104
    abstract protected function removeBindingsThatMatch(Expression $expr);
105
106
    /**
107
     * Removes all bindings bound to the given binding type.
108
     *
109
     * @param string $typeName The name of the binding type.
110
     */
111
    abstract protected function removeBindingsWithTypeName($typeName);
112
113
    /**
114
     * Removes all bindings bound to the given binding type that match an expression.
115
     *
116
     * @param string     $typeName The name of the binding type.
117
     * @param Expression $expr     The expression to filter by.
118
     */
119
    abstract protected function removeBindingsWithTypeNameThatMatch($typeName, Expression $expr);
120
121
    /**
122
     * Returns whether the discovery contains bindings.
123
     *
124
     * @return bool Returns `true` if the discovery has bindings and `false`
125
     *              otherwise.
126
     */
127
    abstract protected function hasAnyBinding();
128
129
    /**
130
     * Returns whether the discovery contains bindings that match an expression.
131
     *
132
     * @param Expression $expr The expression to filter by.
133
     *
134
     * @return bool Returns `true` if the discovery has bindings and `false`
135
     *              otherwise.
136
     */
137
    abstract protected function hasBindingsThatMatch(Expression $expr);
138
139
    /**
140
     * Returns whether the discovery contains bindings for the given type.
141
     *
142
     * @param string $typeName The name of the binding type.
143
     *
144
     * @return bool Returns `true` if bindings bound to the given binding type
145
     *              are found and `false` otherwise.
146
     */
147
    abstract protected function hasBindingsWithTypeName($typeName);
148
149
    /**
150
     * Returns whether the discovery contains bindings for the given type that
151
     * match an expression.
152
     *
153
     * @param string     $typeName The name of the binding type.
154
     * @param Expression $expr     The expression to filter by.
155
     *
156
     * @return bool Returns `true` if bindings bound to the given binding type
157
     *              are found and `false` otherwise.
158
     */
159
    abstract protected function hasBindingsWithTypeNameThatMatch($typeName, Expression $expr);
160
161
    /**
162
     * Initializes a binding.
163
     *
164
     * @param Binding $binding The binding to initialize.
165
     *
166
     * @throws BindingNotAcceptedException If the loaded type does not accept
167
     *                                     the binding.
168
     */
169 89
    protected function initializeBinding(Binding $binding)
170
    {
171 89
        $binding->initialize($this->getBindingType($binding->getTypeName()));
172
173 83
        $bindingClass = get_class($binding);
174
175 83
        if (!isset($this->initializersByBindingClass[$bindingClass])) {
176 83
            $this->initializersByBindingClass[$bindingClass] = array();
177
178
            // Find out which initializers accept the binding
179 83
            foreach ($this->initializers as $initializer) {
180 3
                if ($initializer->acceptsBinding($bindingClass)) {
181 3
                    $this->initializersByBindingClass[$bindingClass][] = $initializer;
182
                }
183
            }
184
        }
185
186
        // Apply all initializers that we found
187 83
        foreach ($this->initializersByBindingClass[$bindingClass] as $initializer) {
188 3
            $initializer->initializeBinding($binding);
189
        }
190 83
    }
191
192
    /**
193
     * Initializes multiple bindings.
194
     *
195
     * @param Binding[] $bindings The bindings to initialize.
196
     */
197 81
    protected function initializeBindings(array $bindings)
198
    {
199 81
        foreach ($bindings as $binding) {
200 25
            $this->initializeBinding($binding);
201
        }
202 81
    }
203
}
204