Config   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
c 2
b 0
f 0
dl 0
loc 74
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getFactoryClassName() 0 3 1
A isDebugMode() 0 3 1
A isSecure() 0 4 2
A getInterfaceIp() 0 3 1
A getSecureOptions() 0 12 3
A __construct() 0 3 1
A getExpectationsPath() 0 3 1
A getPort() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of phiremock-codeception-extension.
5
 *
6
 * phiremock-codeception-extension is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * phiremock-codeception-extension is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with phiremock-codeception-extension.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace Mcustiel\Phiremock\Server\Utils\Config;
21
22
use Exception;
23
use Mcustiel\Phiremock\Server\Cli\Options\CertificateKeyPath;
24
use Mcustiel\Phiremock\Server\Cli\Options\CertificatePath;
25
use Mcustiel\Phiremock\Server\Cli\Options\ExpectationsDirectory;
26
use Mcustiel\Phiremock\Server\Cli\Options\HostInterface;
27
use Mcustiel\Phiremock\Server\Cli\Options\Passphrase;
28
use Mcustiel\Phiremock\Server\Cli\Options\PhpFactoryFqcn;
29
use Mcustiel\Phiremock\Server\Cli\Options\Port;
30
use Mcustiel\Phiremock\Server\Cli\Options\SecureOptions;
31
32
class Config
33
{
34
    public const IP = 'ip';
35
    public const PORT = 'port';
36
    public const DEBUG = 'debug';
37
    public const EXPECTATIONS_DIR = 'expectations-dir';
38
    public const FACTORY_CLASS = 'factory-class';
39
    public const CERTIFICATE = 'certificate';
40
    public const CERTIFICATE_KEY = 'certificate-key';
41
    public const CERT_PASSPHRASE = 'cert-passphrase';
42
43
    public const CONFIG_OPTIONS = [
44
        self::IP,
45
        self::PORT,
46
        self::DEBUG,
47
        self::EXPECTATIONS_DIR,
48
        self::FACTORY_CLASS,
49
        self::CERTIFICATE,
50
        self::CERTIFICATE_KEY,
51
        self::CERT_PASSPHRASE,
52
    ];
53
54
    /** @var array */
55
    private $configurationArray;
56
57
    public function __construct(array $configurationArray)
58
    {
59
        $this->configurationArray = $configurationArray;
60
    }
61
62
    public function getInterfaceIp(): HostInterface
63
    {
64
        return new HostInterface($this->configurationArray[self::IP]);
65
    }
66
67
    public function getPort(): Port
68
    {
69
        return new Port((int) $this->configurationArray[self::PORT]);
70
    }
71
72
    public function isDebugMode(): bool
73
    {
74
        return $this->configurationArray[self::DEBUG];
75
    }
76
77
    public function getExpectationsPath(): ExpectationsDirectory
78
    {
79
        return new ExpectationsDirectory($this->configurationArray[self::EXPECTATIONS_DIR]);
80
    }
81
82
    public function getFactoryClassName(): PhpFactoryFqcn
83
    {
84
        return new PhpFactoryFqcn($this->configurationArray[self::FACTORY_CLASS]);
85
    }
86
87
    public function isSecure(): bool
88
    {
89
        return isset($this->configurationArray[self::CERTIFICATE])
90
            && isset($this->configurationArray[self::CERTIFICATE_KEY]);
91
    }
92
93
    /** @throws Exception */
94
    public function getSecureOptions(): ?SecureOptions
95
    {
96
        if (!$this->isSecure()) {
97
            return null;
98
        }
99
100
        return new SecureOptions(
101
            new CertificatePath($this->configurationArray[self::CERTIFICATE]),
102
            new CertificateKeyPath($this->configurationArray[self::CERTIFICATE_KEY]),
103
            isset($this->configurationArray[self::CERT_PASSPHRASE])
104
                ? new Passphrase($this->configurationArray[self::CERT_PASSPHRASE])
105
                : null
106
        );
107
    }
108
}
109