summaryrefslogtreecommitdiffstats
path: root/modules/setup/library/Setup/Requirement.php
blob: 1df02ef27321a7d5f3d94df269770fd7b4e84801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup;

use LogicException;

abstract class Requirement
{
    /**
     * The state of this requirement
     *
     * @var bool
     */
    protected $state;

    /**
     * A descriptive text representing the current state of this requirement
     *
     * @var string
     */
    protected $stateText;

    /**
     * The descriptions of this requirement
     *
     * @var array
     */
    protected $descriptions;

    /**
     * The title of this requirement
     *
     * @var string
     */
    protected $title;

    /**
     * The condition of this requirement
     *
     * @var mixed
     */
    protected $condition;

    /**
     * Whether this requirement is optional
     *
     * @var bool
     */
    protected $optional;

    /**
     * The alias to display the condition with in a human readable way
     *
     * @var string
     */
    protected $alias;

    /**
     * The text to display if the given requirement is fulfilled
     *
     * @var string
     */
    protected $textAvailable;

    /**
     * The text to display if the given requirement is not fulfilled
     *
     * @var string
     */
    protected $textMissing;

    /**
     * Create a new requirement
     *
     * @param   array   $options
     *
     * @throws  LogicException  In case there exists no setter for an option's key
     */
    public function __construct(array $options = array())
    {
        $this->optional = false;
        $this->descriptions = array();

        foreach ($options as $key => $value) {
            $setMethod = 'set' . ucfirst($key);
            $addMethod = 'add' . ucfirst($key);
            if (method_exists($this, $setMethod)) {
                $this->$setMethod($value);
            } elseif (method_exists($this, $addMethod)) {
                $this->$addMethod($value);
            } else {
                throw new LogicException('No setter found for option key: ' . $key);
            }
        }
    }

    /**
     * Set the state of this requirement
     *
     * @param   bool    $state
     *
     * @return  Requirement
     */
    public function setState($state)
    {
        $this->state = (bool) $state;
        return $this;
    }

    /**
     * Return the state of this requirement
     *
     * Evaluates the requirement in case there is no state set yet.
     *
     * @return  int
     */
    public function getState()
    {
        if ($this->state === null) {
            $this->state = $this->evaluate();
        }

        return $this->state;
    }

    /**
     * Set a descriptive text for this requirement's current state
     *
     * @param   string  $text
     *
     * @return  Requirement
     */
    public function setStateText($text)
    {
        $this->stateText = $text;
        return $this;
    }

    /**
     * Return a descriptive text for this requirement's current state
     *
     * @return  string
     */
    public function getStateText()
    {
        $state = $this->getState();
        if ($this->stateText === null) {
            return $state ? $this->getTextAvailable() : $this->getTextMissing();
        }
        return $this->stateText;
    }

    /**
     * Add a description for this requirement
     *
     * @param   string  $description
     *
     * @return  Requirement
     */
    public function addDescription($description)
    {
        $this->descriptions[] = $description;
        return $this;
    }

    /**
     * Return the descriptions of this wizard
     *
     * @return  array
     */
    public function getDescriptions()
    {
        return $this->descriptions;
    }

    /**
     * Set the title for this requirement
     *
     * @param   string  $title
     *
     * @return  Requirement
     */
    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }

    /**
     * Return the title of this requirement
     *
     * In case there is no title set the alias is returned instead.
     *
     * @return  string
     */
    public function getTitle()
    {
        if ($this->title === null) {
            return $this->getAlias();
        }

        return $this->title;
    }

    /**
     * Set the condition for this requirement
     *
     * @param   mixed   $condition
     *
     * @return  Requirement
     */
    public function setCondition($condition)
    {
        $this->condition = $condition;
        return $this;
    }

    /**
     * Return the condition of this requirement
     *
     * @return  mixed
     */
    public function getCondition()
    {
        return $this->condition;
    }

    /**
     * Set whether this requirement is optional
     *
     * @param   bool    $state
     *
     * @return  Requirement
     */
    public function setOptional($state = true)
    {
        $this->optional = (bool) $state;
        return $this;
    }

    /**
     * Return whether this requirement is optional
     *
     * @return  bool
     */
    public function isOptional()
    {
        return $this->optional;
    }

    /**
     * Set the alias to display the condition with in a human readable way
     *
     * @param   string  $alias
     *
     * @return  Requirement
     */
    public function setAlias($alias)
    {
        $this->alias = $alias;
        return $this;
    }

    /**
     * Return the alias to display the condition with in a human readable way
     *
     * @return  string
     */
    public function getAlias()
    {
        return $this->alias;
    }

    /**
     * Set the text to display if the given requirement is fulfilled
     *
     * @param   string  $textAvailable
     *
     * @return  Requirement
     */
    public function setTextAvailable($textAvailable)
    {
        $this->textAvailable = $textAvailable;
        return $this;
    }

    /**
     * Get the text to display if the given requirement is fulfilled
     *
     * @return  string
     */
    public function getTextAvailable()
    {
        return $this->textAvailable;
    }

    /**
     * Set the text to display if the given requirement is not fulfilled
     *
     * @param   string  $textMissing
     *
     * @return  Requirement
     */
    public function setTextMissing($textMissing)
    {
        $this->textMissing = $textMissing;
        return $this;
    }

    /**
     * Get the text to display if the given requirement is not fulfilled
     *
     * @return  string
     */
    public function getTextMissing()
    {
        return $this->textMissing;
    }

    /**
     * Evaluate this requirement and return whether it is fulfilled
     *
     * @return  bool
     */
    abstract protected function evaluate();

    /**
     * Return whether the given requirement equals this one
     *
     * @param   Requirement     $requirement
     *
     * @return  bool
     */
    public function equals(Requirement $requirement)
    {
        if ($requirement instanceof static) {
            return $this->getCondition() === $requirement->getCondition();
        }

        return false;
    }
}