Feature::dependsOnAtLeastOne()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace PSB\Core\Feature;
3
4
5
use PSB\Core\BusContextInterface;
6
use PSB\Core\ObjectBuilder\BuilderInterface;
7
use PSB\Core\Pipeline\PipelineModifications;
8
use PSB\Core\Util\Guard;
9
use PSB\Core\Util\Settings;
10
11
abstract class Feature
12
{
13
    /**
14
     * @var array
15
     */
16
    private $dependencies = [];
17
18
    /**
19
     * @var bool
20
     */
21
    private $isEnabledByDefault = false;
22
23
    /**
24
     * @var bool
25
     */
26
    private $isActive = false;
27
28
    /**
29
     * @var callable[]
30
     */
31
    private $registeredDefaults = [];
32
33
    /**
34
     * @var Prerequisite[]
35
     */
36
    private $registeredPrerequisites = [];
37
38
    /**
39
     * @var FeatureStartupTaskController[]
40
     */
41
    private $registeredStartupTasksControllers = [];
42
43
    /**
44
     * @var FeatureInstallTaskController[]
45
     */
46
    private $registeredInstallTasksControllers = [];
47
48
    /**
49
     * Method will always be executed and should be used to determine whether to enable or disable the feature,
50
     * configure default settings, configure dependencies, configure prerequisites and register startup tasks.
51
     */
52
    abstract public function describe();
53
54
    /**
55
     * Method is called if all defined conditions are met and the feature is marked as enabled.
56
     * Use this method to configure and initialize all required components for the feature like
57
     * the steps in the pipeline or the instances/factories in the container.
58
     *
59
     * @param Settings              $settings
60
     * @param BuilderInterface      $builder
61
     * @param PipelineModifications $pipelineModifications
62
     */
63
    abstract public function setup(
64
        Settings $settings,
65
        BuilderInterface $builder,
66
        PipelineModifications $pipelineModifications
67
    );
68
69
    /**
70
     * @param Settings              $settings
71
     * @param BuilderInterface      $builder
72
     * @param PipelineModifications $pipelineModifications
73
     *
74
     * @return bool
75
     */
76 3
    public function checkPrerequisites(
77
        Settings $settings,
78
        BuilderInterface $builder,
79
        PipelineModifications $pipelineModifications
80
    ) {
81 3
        foreach ($this->registeredPrerequisites as $prerequisite) {
82 3
            $condition = $prerequisite->getCondition();
83 3
            if (!$condition($settings, $builder, $pipelineModifications)) {
84 2
                return false;
85
            }
86
        }
87
88 1
        return true;
89
    }
90
91
    /**
92
     * @param Settings              $settings
93
     * @param BuilderInterface      $builder
94
     * @param PipelineModifications $pipelineModifications
95
     */
96 1
    public function activate(
97
        Settings $settings,
98
        BuilderInterface $builder,
99
        PipelineModifications $pipelineModifications
100
    ) {
101 1
        $this->setup($settings, $builder, $pipelineModifications);
102 1
        $this->isActive = true;
103 1
    }
104
105
    /**
106
     * @param BuilderInterface    $builder
107
     * @param BusContextInterface $busContext
108
     */
109 1
    public function start(BuilderInterface $builder, BusContextInterface $busContext)
110
    {
111 1
        foreach ($this->registeredStartupTasksControllers as $controller) {
112 1
            $controller->start($builder, $busContext);
113
        }
114 1
    }
115
116
    /**
117
     * @param BuilderInterface $builder
118
     */
119 1
    public function install(BuilderInterface $builder)
120
    {
121 1
        foreach ($this->registeredInstallTasksControllers as $controller) {
122 1
            $controller->install($builder);
123
        }
124 1
    }
125
126
    /**
127
     * @param Settings $settings
128
     */
129 1
    public function configureDefaults(Settings $settings)
130
    {
131 1
        foreach ($this->registeredDefaults as $registeredDefault) {
132 1
            $registeredDefault($settings);
133
        }
134 1
    }
135
136
    /**
137
     * @param callable $defaultInitializer
138
     */
139 2
    protected function registerDefault(callable $defaultInitializer)
140
    {
141 2
        $this->registeredDefaults[] = $defaultInitializer;
142 2
    }
143
144
    /**
145
     * @param callable $prerequisiteSpecification
146
     * @param string   $description
147
     */
148 8
    protected function registerPrerequisite(callable $prerequisiteSpecification, $description)
149
    {
150 8
        Guard::againstNullAndEmpty('description', $description);
151 8
        $this->registeredPrerequisites[] = new Prerequisite($prerequisiteSpecification, $description);
152 8
    }
153
154
    /**
155
     * @param callable $taskFactory
156
     */
157 1
    protected function registerStartupTask(callable $taskFactory)
158
    {
159 1
        $this->registeredStartupTasksControllers[] = new FeatureStartupTaskController($taskFactory);
160 1
    }
161
162
    /**
163
     * @param callable $taskFactory
164
     */
165 2
    protected function registerInstallTask(callable $taskFactory)
166
    {
167 2
        $this->registeredInstallTasksControllers[] = new FeatureInstallTaskController($taskFactory);
168 2
    }
169
170 15
    protected function enableByDefault()
171
    {
172 15
        $this->isEnabledByDefault = true;
173 15
    }
174
175
    /**
176
     * @param string $featureFqcn
177
     */
178 6
    protected function dependsOn($featureFqcn)
179
    {
180 6
        $this->dependencies[] = [$featureFqcn];
181 6
    }
182
183
    /**
184
     * @param array $featureFqcns
185
     */
186 1
    protected function dependsOnAtLeastOne(array $featureFqcns)
187
    {
188 1
        $this->dependencies[] = $featureFqcns;
189 1
    }
190
191
    /**
192
     * @param string $featureFqcn
193
     */
194 1
    protected function dependsOnOptionally($featureFqcn)
195
    {
196 1
        $this->dependsOnAtLeastOne([RootFeature::class, $featureFqcn]);
197 1
    }
198
199
    /**
200
     * @return string
201
     */
202 1
    public function getName()
203
    {
204 1
        return static::class;
205
    }
206
207
    /**
208
     * @return bool
209
     */
210 15
    public function isEnabledByDefault()
211
    {
212 15
        return $this->isEnabledByDefault;
213
    }
214
215
    /**
216
     * @return bool
217
     */
218 1
    public function isActive()
219
    {
220 1
        return $this->isActive;
221
    }
222
223
    /**
224
     * @return array
225
     */
226 6
    public function getDependencies()
227
    {
228 6
        return $this->dependencies;
229
    }
230
}
231