DaemonOptions::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 4
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PHPFastCGI\FastCGIDaemon;
6
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
10
/**
11
 * The default configuration object.
12
 */
13
final class DaemonOptions implements DaemonOptionsInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    private $options;
19
20
    /**
21
     * Constructor.
22
     *
23
     * The value of the LOGGER option must implement the PSR-3 LoggerInterface.
24
     *
25
     * For the REQUEST_LIMIT, MEMORY_LIMIT and TIME_LIMIT options, NO_LIMIT can
26
     * be used to specify that these metrics should not cause the daemon to
27
     * shutdown.
28
     *
29
     * @param array $options The options to configure the daemon with
30
     *
31
     * @throws \InvalidArgumentException On unrecognised option
32
     */
33
    public function __construct(array $options = [])
34
    {
35
        // Set defaults
36
        $this->options = [
37
            self::LOGGER        => new NullLogger(),
38
            self::REQUEST_LIMIT => self::NO_LIMIT,
39
            self::MEMORY_LIMIT  => self::NO_LIMIT,
40
            self::TIME_LIMIT    => self::NO_LIMIT,
41
            self::AUTO_SHUTDOWN => false,
42
        ];
43
44
        foreach ($options as $option => $value) {
45
            if (!isset($this->options[$option])) {
46
                throw new \InvalidArgumentException('Unknown option: '.$option);
47
            }
48
49
            $this->options[$option] = $value;
50
        }
51
52
        if (!$this->options[self::LOGGER] instanceof LoggerInterface) {
53
            throw new \InvalidArgumentException('Logger must implement LoggerInterface');
54
        }
55
    }
56
57
    public function getOption(string $option)
58
    {
59
        if (!isset($this->options[$option])) {
60
            throw new \InvalidArgumentException('Unknown option: '.$option);
61
        }
62
63
        return $this->options[$option];
64
    }
65
}
66