Passed
Push — master ( 0b7dea...b9611f )
by Mariano
04:34
created

Config::isSecure()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Mcustiel\Phiremock\Server\Utils\Config;
4
5
use Exception;
6
use Mcustiel\Phiremock\Server\Cli\Options\CertificateKeyPath;
7
use Mcustiel\Phiremock\Server\Cli\Options\CertificatePath;
8
use Mcustiel\Phiremock\Server\Cli\Options\ExpectationsDirectory;
9
use Mcustiel\Phiremock\Server\Cli\Options\HostInterface;
10
use Mcustiel\Phiremock\Server\Cli\Options\Passphrase;
11
use Mcustiel\Phiremock\Server\Cli\Options\PhpFactoryFqcn;
12
use Mcustiel\Phiremock\Server\Cli\Options\Port;
13
use Mcustiel\Phiremock\Server\Cli\Options\SecureOptions;
14
15
class Config
16
{
17
    public const IP = 'ip';
18
    public const PORT = 'port';
19
    public const DEBUG = 'debug';
20
    public const EXPECTATIONS_DIR = 'expectations-dir';
21
    public const FACTORY_CLASS = 'factory-class';
22
    public const CERTIFICATE = 'certificate';
23
    public const CERTIFICATE_KEY = 'certificate-key';
24
    public const CERT_PASSPHRASE = 'cert-passphrase';
25
26
    public const CONFIG_OPTIONS = [
27
        self::IP,
28
        self::PORT,
29
        self::DEBUG,
30
        self::EXPECTATIONS_DIR,
31
        self::FACTORY_CLASS,
32
        self::CERTIFICATE,
33
        self::CERTIFICATE_KEY,
34
        self::CERT_PASSPHRASE,
35
    ];
36
37
    /** @var array */
38
    private $configurationArray;
39
40
    public function __construct(array $configurationArray)
41
    {
42
        $this->configurationArray = $configurationArray;
43
    }
44
45
    public function getInterfaceIp(): HostInterface
46
    {
47
        return new HostInterface($this->configurationArray[self::IP]);
48
    }
49
50
    public function getPort(): Port
51
    {
52
        return new Port((int) $this->configurationArray[self::PORT]);
53
    }
54
55
    public function isDebugMode(): bool
56
    {
57
        return $this->configurationArray[self::DEBUG];
58
    }
59
60
    public function getExpectationsPath(): ExpectationsDirectory
61
    {
62
        return new ExpectationsDirectory($this->configurationArray[self::EXPECTATIONS_DIR]);
63
    }
64
65
    public function getFactoryClassName(): PhpFactoryFqcn
66
    {
67
        return new PhpFactoryFqcn($this->configurationArray[self::FACTORY_CLASS]);
68
    }
69
70
    public function isSecure(): bool
71
    {
72
        return isset($this->configurationArray[self::CERTIFICATE])
73
            && isset($this->configurationArray[self::CERTIFICATE_KEY]);
74
    }
75
76
    /** @throws Exception */
77
    public function getSecureOptions(): ?SecureOptions
78
    {
79
        if (!$this->isSecure()) {
80
            return null;
81
        }
82
83
        return new SecureOptions(
84
            new CertificatePath($this->configurationArray[self::CERTIFICATE]),
85
            new CertificateKeyPath($this->configurationArray[self::CERTIFICATE_KEY]),
86
            isset($this->configurationArray[self::CERT_PASSPHRASE])
87
                ? new Passphrase($this->configurationArray[self::CERT_PASSPHRASE])
88
                : null
89
        );
90
    }
91
}
92