GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Server::setPrefix()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * This file is part of WebHelper Parser.
5
 *
6
 * (c) James <[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 WebHelper\Parser\Server;
13
14
use WebHelper\Parser\Parser\Checker;
15
16
/**
17
 * A web server instance.
18
 *
19
 * @author James <[email protected]>
20
 */
21
class Server implements ServerInterface
22
{
23
    /**
24
     * a Checker instance.
25
     *
26
     * @var \WebHelper\Parser\Parser\Checker
27
     */
28
    private $checker;
29
30
    /**
31
     * The filesystem path where the web server is installed.
32
     *
33
     * It has to be an absolute path.
34
     *
35
     * Apache httpd server does not accept a relative prefix path at compilation.
36
     * Nginx does, but this is a very risky practice...
37
     * So relative prefix path in nginx configuration will not be considered
38
     *
39
     * @var string
40
     */
41
    private $prefix = '';
42
43
    /**
44
     * The string to match as a starting multi-line directive.
45
     *
46
     * @var string
47
     */
48
    private $startMultiLine = '';
49
50
    /**
51
     * The string to match as an ending multi-line directive.
52
     *
53
     * @var string
54
     */
55
    private $endMultiLine = '';
56
57
    /**
58
     * The string to match a simple directive.
59
     *
60
     * @var string
61
     */
62
    private $simpleDirective = '';
63
64
    /**
65
     * binaries that can be used to control the webserver.
66
     *
67
     * @var array
68
     */
69
    private $binaries = [];
70
71
    /**
72
     * the parameter string to use to detect version and config file.
73
     *
74
     * @var string
75
     */
76
    private $detectionParameter = '';
77
78
    /**
79
     * The ordered list of methods to apply before convertion.
80
     *
81
     * @var array
82
     */
83
    private $beforeMethods = [];
84
85
    /**
86
     * The ordered list of methods to apply after convertion.
87
     *
88
     * @var array
89
     */
90
    private $afterMethods = [];
91
92
    /**
93
     * The simple directive syntax when dumped.
94
     *
95
     * @var string
96
     */
97
    private $dumperSimpleDirective = '';
98
99
    /**
100
     * The starting block directive syntax when dumped.
101
     *
102
     * @var string
103
     */
104
    private $dumperStartDirective = '';
105
106
    /**
107
     * The ending block directive syntax when dumped.
108
     *
109
     * @var string
110
     */
111
    private $dumperEndDirective = '';
112
113
    /**
114
     * The known directives of the server
115
     *
116
     * @var array
117
     */
118
    private $knownDirectives = [];
119
120
    /**
121
     * Sets the Checker instance.
122
     *
123
     * @param Checker $checker a Checker instance
124
     */
125 30
    public function setChecker(Checker $checker)
126
    {
127 30
        $this->checker = $checker;
128
129 30
        return $this;
130
    }
131
132
    /**
133
     * Confirms if the server instance has valid parameters.
134
     *
135
     * @return bool true if all parameters are initialized, false otherwise
136
     */
137 2
    public function isValid()
138
    {
139 2
        $valid = $this->prefix != '';
140 2
        $valid = $valid && $this->startMultiLine != '';
141 2
        $valid = $valid && $this->endMultiLine != '';
142 2
        $valid = $valid && $this->simpleDirective != '';
143
144 2
        return $valid;
145
    }
146
147
    /**
148
     * Getter for the prefix.
149
     *
150
     * @return string the filesystem path where the web server is installed
151
     */
152 4
    public function getPrefix()
153
    {
154 4
        return $this->prefix;
155
    }
156
157
    /**
158
     * Sets the prefix of a server instance.
159
     *
160
     *
161
     * @param string $prefix the filesystem path where the web server is installed
162
     *
163
     * @throws ServerException if the prefix is invalid
164
     */
165 11
    public function setPrefix($prefix)
166
    {
167 11
        if (!$this->checker->setString($prefix)->getString()) {
168 1
            throw ServerException::forInvalidPrefix($prefix, 'The path is expected to be a string. Got: %s');
169
        }
170
171 10
        if (!$this->checker->isValidAbsolutePath()) {
172 2
            throw ServerException::forInvalidPrefix(
173 2
                $prefix,
174
                'The path is expected to be absolute and an existing directory. Got: %s'
175 2
            );
176
        }
177
178 8
        $this->prefix = $prefix;
179
180 8
        return $this;
181
    }
182
183
    /**
184
     * Gets the regexp that will match the starting block directives.
185
     *
186
     * @return string the regexp that will match the starting block directives
187
     */
188 8
    public function getStartMultiLine()
189
    {
190 8
        return $this->startMultiLine;
191
    }
192
193
    /**
194
     * Sets the regexp that will match the starting block directives.
195
     *
196
     * @param string $startMultiLine the regexp that will match the starting block directives
197
     */
198 13
    public function setStartMultiLine($startMultiLine)
199
    {
200 13
        if ($this->isValidDirective(
201 13
            $startMultiLine,
202 13
            'The starting block directive matcher is expected to be a string. Got: %s',
203
            'The starting block directive matcher is expected to be a regexp '.
204
            'containing named subpatterns "key" and "value". Got: %s'
205 13
        )) {
206 8
            $this->startMultiLine = $startMultiLine;
207 8
        }
208
209 8
        return $this;
210
    }
211
212
    /**
213
     * Gets the regexp that will match the ending block directives.
214
     *
215
     * @return string the regexp that will match the ending block directives
216
     */
217 8
    public function getEndMultiLine()
218
    {
219 8
        return $this->endMultiLine;
220
    }
221
222
    /**
223
     * Sets the regexp that will match the ending block directives.
224
     *
225
     * @param string $endMultiLine the regexp that will match the ending block directives
226
     */
227 10
    public function setEndMultiLine($endMultiLine)
228
    {
229 10
        $this->endMultiLine = $this->setRegexDirective(
230 10
            $endMultiLine,
231 10
            'The ending block directive matcher is expected to be a string. Got: %s',
232
            'The ending block directive matcher is expected to be a regexp.'
233 10
        );
234
235 8
        return $this;
236
    }
237
238
    /**
239
     * Gets the regexp that will match the simple directives.
240
     *
241
     * @return string the regexp that will match the simple directives
242
     */
243 8
    public function getSimpleDirective()
244
    {
245 8
        return $this->simpleDirective;
246
    }
247
248
    /**
249
     * Sets the regexp that will match the simple directives.
250
     *
251
     * @param string $simpleDirective the regexp that will match the simple directives
252
     */
253 13
    public function setSimpleDirective($simpleDirective)
254
    {
255 13
        if ($this->isValidDirective(
256 13
            $simpleDirective,
257 13
            'The simple directive matcher is expected to be a string. Got: %s',
258
            'The simple directive matcher is expected to be a regexp '.
259
            'containing named subpatterns "key" and "value". Got: %s'
260 13
        )) {
261 8
            $this->simpleDirective = $simpleDirective;
262 8
        }
263
264 8
        return $this;
265
    }
266
267
    /**
268
     * Sets the regular expression directive.
269
     *
270
     * @param string $directive the directive string
271
     * @param string $message1  message exception if the matcher is not a string
272
     * @param string $message2  message exception if the matcher is not a valid regex
273
     *
274
     * @throws ServerException if the directive matcher is invalid
275
     *
276
     * @return string the regular expression directive
277
     */
278 10 View Code Duplication
    private function setRegexDirective($directive, $message1, $message2)
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...
279
    {
280 10
        if (!$this->checker->setString($directive)->getString()) {
281 1
            throw ServerException::forInvalidMatcher(
282 1
                $directive,
283
                $message1
284 1
            );
285
        }
286
287 9
        if (!$this->checker->isValidRegex()) {
288 1
            throw ServerException::forInvalidMatcher(
289 1
                $directive,
290
                $message2
291 1
            );
292
        }
293
294 8
        return $directive;
295
    }
296
297
    /**
298
     * Confirms if a directive matcher is a valid regex.
299
     *
300
     * @param string $directive the directive matcher to check
301
     * @param string $message1  message exception if the matcher is not a string
302
     * @param string $message2  message exception if the matcher is not a valid regex
303
     *
304
     * @throws ServerException if the directive matcher is invalid
305
     *
306
     * @return bool true if the directive matcher is valid
307
     */
308 19 View Code Duplication
    private function isValidDirective($directive, $message1, $message2)
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...
309
    {
310 19
        if (!$this->checker->setString($directive)->getString()) {
311 2
            throw ServerException::forInvalidMatcher(
312 2
                $directive,
313
                $message1
314 2
            );
315
        }
316
317 17
        if (!$this->checker->hasKeyAndValueSubPattern()) {
318 8
            throw ServerException::forInvalidMatcher(
319 8
                $directive,
320
                $message2
321 8
            );
322
        }
323
324 9
        return true;
325
    }
326
327
    /**
328
     * Gets the list of binaries that can be run to analyze.
329
     *
330
     * @return array the list of binaries that can be run
331
     */
332 1
    public function getBinaries()
333
    {
334 1
        return $this->binaries;
335
    }
336
337
    /**
338
     * Sets the list of binaries that can be run to analyze.
339
     *
340
     * @param array $binaries list of controlers
341
     */
342 8
    public function setBinaries(array $binaries)
343
    {
344 8
        $this->binaries = $binaries;
345
346 8
        return $this;
347
    }
348
349
    /**
350
     * Gets the parameter string to use to detect version and config file.
351
     *
352
     * @return string parameter string
353
     */
354 1
    public function getDetectionParameter()
355
    {
356 1
        return $this->detectionParameter;
357
    }
358
359
    /**
360
     * Sets the parameter string to use to detect version and config file.
361
     *
362
     * @param string $parameter parameter string
363
     */
364 8
    public function setDetectionParameter($parameter = '')
365
    {
366 8
        $this->detectionParameter = $parameter;
367
368 8
        return $this;
369
    }
370
371
    /**
372
     * Gets the ordered list of methods to apply before the config file turns into an array.
373
     *
374
     * @return array the ordered list of methods to apply before convertion
375
     */
376 14
    public function getBeforeMethods()
377
    {
378 14
        return $this->beforeMethods;
379
    }
380
381
    /**
382
     * Sets the ordered list of methods to apply before the config file turns into an array.
383
     *
384
     * @param array $methods the ordered list of methods to apply before convertion
385
     */
386 13
    public function setBeforeMethods(array $methods)
387
    {
388 13
        $this->beforeMethods = $methods;
389
390 13
        return $this;
391
    }
392
393
    /**
394
     * Gets the ordered list of methods to apply after the config file has turned into an array.
395
     *
396
     * @return array the ordered list of methods to apply after convertion
397
     */
398 14
    public function getAfterMethods()
399
    {
400 14
        return $this->afterMethods;
401
    }
402
403
    /**
404
     * Sets the ordered list of methods to apply after the config file has turned into an array.
405
     *
406
     * @param array $methods the ordered list of methods to apply after convertion
407
     */
408 13
    public function setAfterMethods(array $methods)
409
    {
410 13
        $this->afterMethods = $methods;
411
412 13
        return $this;
413
    }
414
415
    /**
416
     * Gets the simple directive syntax when dumped.
417
     *
418
     * @return string the simple directive syntax when dumped
419
     */
420 1
    public function getDumperSimpleDirective()
421
    {
422 1
        return $this->dumperSimpleDirective;
423
    }
424
425
    /**
426
     * Gets the starting block directive syntax when dumped.
427
     *
428
     * @return string the starting block directive syntax when dumped
429
     */
430 1
    public function getDumperStartDirective()
431
    {
432 1
        return $this->dumperStartDirective;
433
    }
434
435
    /**
436
     * Gets the ending block directive syntax when dumped.
437
     *
438
     * @return string the ending block directive syntax when dumped
439
     */
440 1
    public function getDumperEndDirective()
441
    {
442 1
        return $this->dumperEndDirective;
443
    }
444
445
    /**
446
     * Sets the simple directive syntax when dumped.
447
     *
448
     * @param string $simpleDirective the simple directive syntax when dumped
449
     */
450 8
    public function setDumperSimpleDirective($simpleDirective)
451
    {
452 8
        $this->dumperSimpleDirective = $simpleDirective;
453
454 8
        return $this;
455
    }
456
457
    /**
458
     * Sets the starting block directive syntax when dumped.
459
     *
460
     * @param string $startMultiLine the starting block directive syntax when dumped
461
     */
462 8
    public function setDumperStartDirective($startMultiLine)
463
    {
464 8
        $this->dumperStartDirective = $startMultiLine;
465
466 8
        return $this;
467
    }
468
469
    /**
470
     * Sets the ending block directive syntax when dumped.
471
     *
472
     * @param string $endMultiLine the ending block directive syntax when dumped
473
     */
474 8
    public function setDumperEndDirective($endMultiLine)
475
    {
476 8
        $this->dumperEndDirective = $endMultiLine;
477
478 8
        return $this;
479
    }
480
481
    /**
482
     * Gets the known directives of the server.
483
     *
484
     * @return array the known directives of the server
485
     */
486 9
    public function getKnownDirectives()
487
    {
488 9
        return $this->knownDirectives;
489
    }
490
491
    /**
492
     * Sets the known directives of the server.
493
     *
494
     * @param array $knownDirectives the known directives of the server
495
     */
496 7
    public function setKnownDirectives(array $knownDirectives)
497
    {
498 7
        $this->knownDirectives = $knownDirectives;
499
500 7
        return $this;
501
    }
502
}
503