BootstrapCurator::applyToListeners()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
namespace Consolidation\Bootstrap;
3
4
/**
5
 * Keep bootstrap-aware object safe until such a time as
6
 * a bootstrap object is selected and set.  Once the bootstrap
7
 * object is known, is is passed along to any object that was
8
 * initialized before it became available.
9
 *
10
 * The purpose of this class is for use as a Dependency Injection
11
 * inflector. Typically, the bootstrap object will not be known
12
 * at the time the container is configured; however, adding to the
13
 * bootstrap object to the container later is not desirable either.
14
 * It is more efficient to take advantage of the lazy-instantiation
15
 * capabilities of the container, although doing so means that it
16
 * is not always obvious at what point a given object might be
17
 * instantiated.
18
 *
19
 * It is presumed that bootstrap-aware objects in general will not
20
 * be used until after bootstrap-selection has taken place. If they
21
 * are, they must be prepared to deal with an unset boot object.
22
 *
23
 * TODO: Alternately, we could create an "empty" or "no selection"
24
 * bootstrap object, and provide that temporarily to all listeners
25
 * until the real object is available. However, this design has the
26
 * disadvantage the listeners would then need to re-do any work once
27
 * their setBootstrap method was called. This clearly would not be
28
 * possible for work that was returned as a function result, and would
29
 * probably be very hard to debug. With the current design, the
30
 * unset boot object is, at least, easy to detect.
31
 */
32
class BootstrapCurator
33
{
34
    /**
35
     * @var BootInterface
36
     */
37
    protected $bootstrap;
38
39
    /**
40
     * @var BootstrapAwareInterface[]
41
     */
42
    protected $listeners = [];
43
44
    public function register(BootstrapAwareInterface $listener)
45
    {
46
        if (isset($this->bootstrap)) {
47
            $this->apply($listener, $this->bootstrap);
48
            return;
49
        }
50
        $this->listeners[] = $listener;
51
    }
52
53
    public function setBootstrap(BootInterface $bootstrap)
54
    {
55
        $this->bootstrap = $bootstrap;
56
        $this->applyToListeners($this->listeners, $bootstrap);
57
        $this->listeners = [];
58
    }
59
60
    public function getBootstrap()
61
    {
62
        return $this->bootstrap;
63
    }
64
65
    public function hasBootstrap()
66
    {
67
        return isset($this->bootstrap);
68
    }
69
70
    protected function apply(BootstrapAwareInterface $listener, BootInterface $bootstrap)
0 ignored issues
show
Unused Code introduced by
The parameter $bootstrap is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        $listener->setBootstrap($this->bootstrap);
73
    }
74
75
    protected function applyToListeners($listeners, BootInterface $bootstrap)
76
    {
77
        foreach ($listeners as $listener) {
78
            $this->apply($listener, $bootstrap);
79
        }
80
    }
81
}
82