Failed Conditions
Pull Request — master (#56)
by Bernhard
11:07
created

BindingDescriptor::getLoadErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
1
<?php
2
3
/*
4
 * This file is part of the puli/manager 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\Manager\Api\Discovery;
13
14
use Exception;
15
use Puli\Discovery\Api\Binding\Binding;
16
use Puli\Discovery\Binding\ResourceBinding;
17
use Puli\Manager\Api\AlreadyLoadedException;
18
use Puli\Manager\Api\Module\Module;
19
use Puli\Manager\Api\Module\RootModule;
20
use Puli\Manager\Api\NotLoadedException;
21
use Rhumsaa\Uuid\Uuid;
22
23
/**
24
 * Describes a resource binding.
25
 *
26
 * This class contains a high-level model of {@link ResourceBinding} as it is
27
 * used in this module.
28
 *
29
 * @since  1.0
30
 *
31
 * @author Bernhard Schussek <[email protected]>
32
 *
33
 * @see    ResourceBinding
34
 */
35
class BindingDescriptor
36
{
37
    /**
38
     * @var Binding
39
     */
40
    private $binding;
41
42
    /**
43
     * @var int
44
     */
45
    private $state;
46
47
    /**
48
     * @var Module
49
     */
50
    private $containingModule;
51
52
    /**
53
     * @var BindingTypeDescriptor
54
     */
55
    private $typeDescriptor;
56
57
    /**
58
     * @var Exception[]
59
     */
60
    private $loadErrors;
61
62
    /**
63
     * Creates a new binding descriptor.
64
     *
65
     * @param Binding $binding The described binding.
66
     */
67 64
    public function __construct(Binding $binding)
68
    {
69 64
        $this->binding = $binding;
70 64
    }
71
72
    /**
73
     * Loads the binding descriptor.
74
     *
75
     * @param Module                     $containingModule The module that
76
     *                                                     contains the
77
     *                                                     descriptor.
78
     * @param BindingTypeDescriptor|null $typeDescriptor   The type descriptor.
79
     *
80
     * @throws AlreadyLoadedException If the descriptor is already loaded.
81
     */
82 30
    public function load(Module $containingModule, BindingTypeDescriptor $typeDescriptor = null)
83
    {
84 30
        if (null !== $this->state) {
85
            throw new AlreadyLoadedException('The binding descriptor is already loaded.');
86
        }
87
88 30
        $this->loadErrors = array();
89
90 30
        if ($typeDescriptor && $typeDescriptor->isLoaded() && $typeDescriptor->isEnabled()) {
91
            try {
92 20
                $this->binding->initialize($typeDescriptor->getType());
93 4
            } catch (Exception $e) {
94 4
                $this->loadErrors[] = $e;
95
            }
96
        }
97
98 30
        $this->containingModule = $containingModule;
99 30
        $this->typeDescriptor = $typeDescriptor;
100
101 30
        $this->refreshState();
102 30
    }
103
104
    /**
105
     * Unloads the binding descriptor.
106
     *
107
     * All memory allocated during {@link load()} is freed.
108
     *
109
     * @throws NotLoadedException If the descriptor is not loaded.
110
     */
111 3
    public function unload()
112
    {
113 3
        if (null === $this->state) {
114
            throw new NotLoadedException('The binding descriptor is not loaded.');
115
        }
116
117 3
        $this->containingModule = null;
118 3
        $this->typeDescriptor = null;
119 3
        $this->loadErrors = array();
120 3
        $this->state = null;
121 3
    }
122
123
    /**
124
     * Returns whether the descriptor is loaded.
125
     *
126
     * @return bool Returns `true` if the descriptor is loaded.
127
     */
128 24
    public function isLoaded()
129
    {
130 24
        return null !== $this->state;
131
    }
132
133
    /**
134
     * Returns the name of the bound type.
135
     *
136
     * @return string The type name.
137
     */
138 28
    public function getTypeName()
139
    {
140 28
        return $this->binding->getTypeName();
141
    }
142
143
    /**
144
     * Returns the described binding.
145
     *
146
     * @return Binding The binding.
147
     */
148 53
    public function getBinding()
149
    {
150 53
        return $this->binding;
151
    }
152
153
    /**
154
     * Returns the errors that happened during loading.
155
     *
156
     * The method {@link load()} needs to be called before calling this method,
157
     * otherwise an exception is thrown.
158
     *
159
     * @return Exception[] The load errors.
160
     *
161
     * @throws NotLoadedException If the descriptor is not loaded.
162
     */
163 6
    public function getLoadErrors()
164
    {
165 6
        if (null === $this->loadErrors) {
166
            throw new NotLoadedException('The binding descriptor is not loaded.');
167
        }
168
169 6
        return $this->loadErrors;
170
    }
171
172
    /**
173
     * Returns the module that contains the descriptor.
174
     *
175
     * The method {@link load()} needs to be called before calling this method,
176
     * otherwise an exception is thrown.
177
     *
178
     * @return Module The containing module.
179
     *
180
     * @throws NotLoadedException If the descriptor is not loaded.
181
     */
182 24
    public function getContainingModule()
183
    {
184 24
        if (null === $this->containingModule) {
185 3
            throw new NotLoadedException('The binding descriptor is not loaded.');
186
        }
187
188 24
        return $this->containingModule;
189
    }
190
191
    /**
192
     * Returns the type descriptor.
193
     *
194
     * The method {@link load()} needs to be called before calling this method,
195
     * otherwise an exception is thrown.
196
     *
197
     * @return BindingTypeDescriptor|null The type descriptor or null, if no
198
     *                                    type descriptor exists for the
199
     *                                    binding's type name.
200
     *
201
     * @throws NotLoadedException If the binding descriptor is not loaded.
202
     */
203
    public function getTypeDescriptor()
204
    {
205
        // Check containing module, as the type descriptor may be null
206
        if (null === $this->containingModule) {
207
            throw new NotLoadedException('The binding descriptor is not loaded.');
208
        }
209
210
        return $this->typeDescriptor;
211
    }
212
213
    /**
214
     * Returns the state of the binding.
215
     *
216
     * The method {@link load()} needs to be called before calling this method,
217
     * otherwise an exception is thrown.
218
     *
219
     * @return int One of the {@link BindingState} constants.
220
     *
221
     * @throws NotLoadedException If the descriptor is not loaded.
222
     */
223 5
    public function getState()
224
    {
225 5
        if (null === $this->state) {
226
            throw new NotLoadedException('The binding descriptor is not loaded.');
227
        }
228
229 5
        return $this->state;
230
    }
231
232
    /**
233
     * Returns whether the binding is enabled.
234
     *
235
     * The method {@link load()} needs to be called before calling this method,
236
     * otherwise an exception is thrown.
237
     *
238
     * @return bool Returns `true` if the state is {@link BindingState::ENABLED}.
239
     *
240
     * @throws NotLoadedException If the descriptor is not loaded.
241
     *
242
     * @see BindingState::ENABLED
243
     */
244 5 View Code Duplication
    public function isEnabled()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
245
    {
246 5
        if (null === $this->state) {
247
            throw new NotLoadedException('The binding descriptor is not loaded.');
248
        }
249
250 5
        return BindingState::ENABLED === $this->state;
251
    }
252
253
    /**
254
     * Returns whether the type of the binding does not exist.
255
     *
256
     * The method {@link load()} needs to be called before calling this method,
257
     * otherwise an exception is thrown.
258
     *
259
     * @return bool Returns `true` if the state is {@link BindingState::TYPE_NOT_FOUND}.
260
     *
261
     * @throws NotLoadedException If the descriptor is not loaded.
262
     *
263
     * @see BindingState::TYPE_NOT_FOUND
264
     */
265 6 View Code Duplication
    public function isTypeNotFound()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
266
    {
267 6
        if (null === $this->state) {
268
            throw new NotLoadedException('The binding descriptor is not loaded.');
269
        }
270
271 6
        return BindingState::TYPE_NOT_FOUND === $this->state;
272
    }
273
274
    /**
275
     * Returns whether the type of the binding is not enabled.
276
     *
277
     * The method {@link load()} needs to be called before calling this method,
278
     * otherwise an exception is thrown.
279
     *
280
     * @return bool Returns `true` if the state is {@link BindingState::TYPE_NOT_ENABLED}.
281
     *
282
     * @throws NotLoadedException If the descriptor is not loaded.
283
     *
284
     * @see BindingState::TYPE_NOT_ENABLED
285
     */
286 5 View Code Duplication
    public function isTypeNotEnabled()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
    {
288 5
        if (null === $this->state) {
289
            throw new NotLoadedException('The binding descriptor is not loaded.');
290
        }
291
292 5
        return BindingState::TYPE_NOT_ENABLED === $this->state;
293
    }
294
295
    /**
296
     * Returns whether the binding is invalid.
297
     *
298
     * The method {@link load()} needs to be called before calling this method,
299
     * otherwise an exception is thrown.
300
     *
301
     * @return bool Returns `true` if the state is {@link BindingState::INVALID}.
302
     *
303
     * @throws NotLoadedException If the descriptor is not loaded.
304
     *
305
     * @see BindingState::INVALID
306
     */
307 View Code Duplication
    public function isInvalid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
308
    {
309
        if (null === $this->state) {
310
            throw new NotLoadedException('The binding descriptor is not loaded.');
311
        }
312
313
        return BindingState::INVALID === $this->state;
314
    }
315
316 30
    private function refreshState()
317
    {
318 30
        if (null === $this->typeDescriptor || !$this->typeDescriptor->isLoaded()) {
319 6
            $this->state = BindingState::TYPE_NOT_FOUND;
320 25
        } elseif (!$this->typeDescriptor->isEnabled()) {
321 5
            $this->state = BindingState::TYPE_NOT_ENABLED;
322 20
        } elseif (count($this->loadErrors) > 0) {
323 4
            $this->state = BindingState::INVALID;
324 16
        } elseif ($this->containingModule instanceof RootModule) {
325 14
            $this->state = BindingState::ENABLED;
326
        } else {
327 5
            $this->state = BindingState::ENABLED;
328
        }
329 30
    }
330
}
331