Passed
Pull Request — master (#9)
by Shinji
10:53
created

DaemonSettings   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromConsoleInput() 0 19 4
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[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
declare(strict_types=1);
13
14
namespace PhpProfiler\Inspector\Settings;
15
16
use PhpProfiler\Inspector\Settings\InspectorSettingsException;
17
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
final class DaemonSettings
20
{
21
    private const TARGET_REGEX_DEFAULT = '^php-fpm';
22
23
    public string $target_regex;
24
    public int $threads;
25
26
    /**
27
     * DaemonSettings constructor.
28
     * @param string $target_regex
29
     * @param int $threads
30
     */
31
    public function __construct(string $target_regex, int $threads)
32
    {
33
        $this->target_regex = $target_regex;
34
        $this->threads = $threads;
35
    }
36
37
    /**
38
     * @param InputInterface $input
39
     * @return self
40
     * @throws InspectorSettingsException
41
     */
42
    public static function fromConsoleInput(InputInterface $input): self
43
    {
44
        $threads = $input->getOption('threads');
45
        if (is_null($threads)) {
46
            $threads = 8;
47
        }
48
        $threads = filter_var($threads, FILTER_VALIDATE_INT);
49
        if ($threads === false) {
50
            throw DaemonInspectorSettingsException::create(DaemonInspectorSettingsException::THREADS_IS_NOT_INTEGER);
51
        }
52
53
        $target_regex = $input->getOption('target-regex') ?? self::TARGET_REGEX_DEFAULT;
54
        if (!is_string($target_regex)) {
55
            throw DaemonInspectorSettingsException::create(
56
                DaemonInspectorSettingsException::TARGET_REGEX_IS_NOT_STRING
57
            );
58
        }
59
60
        return new self('{' . $target_regex . '}', $threads);
61
    }
62
}
63