Passed
Push — master ( e66d23...e16cc9 )
by Mariano
02:32 queued 01:03
created

Config::initInterfaceAndPort()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Mcustiel\Phiremock\Codeception\Extension;
4
5
use Codeception\Configuration;
6
use Codeception\Exception\ConfigurationException;
7
8
class Config
9
{
10
    public const DEFAULT_INTERFACE = '0.0.0.0';
11
    public const DEFAULT_PORT = 8086;
12
    public const DEFAULT_PHIREMOCK_PATH = 'vendor/bin/phiremock';
13
    public const DEFAULT_DELAY = 0;
14
    public const DEFAULT_DEBUG_MODE = false;
15
    public const DEFAULT_EXPECTATIONS_PATH = null;
16
    public const DEFAULT_CERTIFICATE = null;
17
    public const DEFAULT_CERTIFICATE_KEY = null;
18
    public const DEFAULT_CERTIFICATE_PASSPHRASE = null;
19
    public const DEFAULT_SERVER_FACTORY = 'default';
20
    public const DEFAULT_EXTRA_INSTANCES = [];
21
    public const DEFAULT_SUITES = [];
22
23
    public const DEFAULT_CONFIG = [
24
        'listen'            => self::DEFAULT_INTERFACE . ':' . self::DEFAULT_PORT,
25
        'debug'             => self::DEFAULT_DEBUG_MODE,
26
        'start_delay'       => self::DEFAULT_DELAY,
27
        'bin_path'          => self::DEFAULT_PHIREMOCK_PATH,
28
        'expectations_path' => self::DEFAULT_EXPECTATIONS_PATH,
29
        'server_factory'    => self::DEFAULT_SERVER_FACTORY,
30
        'certificate'       => self::DEFAULT_CERTIFICATE,
31
        'certificate_key'   => self::DEFAULT_CERTIFICATE_KEY,
32
        'cert_passphrase'   => self::DEFAULT_CERTIFICATE_PASSPHRASE,
33
        'extra_instances'   => self::DEFAULT_EXTRA_INSTANCES,
34
        'suites'            => self::DEFAULT_SUITES,
35
    ];
36
37
    /** @var string */
38
    private $interface;
39
    /** @var int */
40
    private $port;
41
    /** @var int */
42
    private $delay;
43
    /** @var bool */
44
    private $debug;
45
    /** @var Path */
46
    private $phiremockPath;
47
    /** @var Path */
48
    private $expectationsPath;
49
    /** @var Path */
50
    private $logsPath;
51
    /** @var string */
52
    private $serverFactory;
53
    /** @var Path */
54
    private $certificate;
55
    /** @var Path */
56
    private $certificateKey;
57
    /** @var string */
58
    private $certificatePassphrase;
59
    /** @var Config[] */
60
    private $extraInstances;
61
    /** @var string[] */
62
    private $suites;
63
    /** @var callable */
64
    private $output;
65
66
    /** @throws ConfigurationException */
67
    public function __construct(array $config, callable $output)
68
    {
69
        $this->output = $output;
70
        $this->initInterfaceAndPort($config);
71
        $this->initExpectationsPath($config);
72
        $this->initServerFactory($config);
73
        $this->initDelay($config);
74
        $this->phiremockPath = new Path($config['bin_path']);
75
        $this->logsPath = new Path($config['logs_path']);
76
        $this->debug = (bool) $config['debug'];
77
        $this->initCertificatePath($config);
78
        $this->initCertificateKeyPath($config);
79
        $this->certificatePassphrase = $config['cert_passphrase'];
80
        $this->initExtraInstances($config);
81
        $this->suites = $config['suites'];
82
    }
83
84
    public function getSuites(): array
85
    {
86
        return $this->suites;
87
    }
88
89
    public function getInterface(): string
90
    {
91
        return $this->interface;
92
    }
93
94
    public function getPort(): string
95
    {
96
        return $this->port;
97
    }
98
99
    public function isDebugMode(): bool
100
    {
101
        return $this->debug;
102
    }
103
104
    public function getStartDelay(): string
105
    {
106
        return $this->delay;
107
    }
108
109
    public function getPhiremockPath(): string
110
    {
111
        return $this->phiremockPath->absoluteOrRelativeToCodeceptionDir();
112
    }
113
114
    public function getExpectationsPath(): ?string
115
    {
116
        return $this->expectationsPath ? $this->expectationsPath->absoluteOrRelativeToCodeceptionDir() : null;
117
    }
118
119
    public function getCertificatePath(): ?string
120
    {
121
        return $this->certificate ? $this->certificate->absoluteOrRelativeToCodeceptionDir() : null;
122
    }
123
124
    public function getCertificateKeyPath(): ?string
125
    {
126
        return $this->certificateKey ? $this->certificateKey->absoluteOrRelativeToCodeceptionDir() : null;
127
    }
128
129
    public function getCertificatePassphrase(): ?string
130
    {
131
        return $this->certificatePassphrase;
132
    }
133
134
    public function getLogsPath(): string
135
    {
136
        return $this->logsPath->absoluteOrRelativeToCodeceptionDir();
137
    }
138
139
    public function getServerFactory(): ?string
140
    {
141
        return $this->serverFactory;
142
    }
143
144
    public function getDelay(): int
145
    {
146
        return $this->delay;
147
    }
148
149
    public function getExtraInstances(): array
150
    {
151
        return $this->extraInstances;
152
    }
153
154
    /** @throws ConfigurationException */
155
    public static function getDefaultLogsPath(): string
156
    {
157
        return Configuration::logDir();
158
    }
159
160
    private function initInterfaceAndPort(array $config): void
161
    {
162
        if (isset($config['listen'])) {
163
            $parts = explode(':', $config['listen']);
164
            $this->interface = $parts[0];
165
            $this->port = (int) (isset($parts[1]) ? $parts[1] : self::DEFAULT_PORT);
166
        }
167
    }
168
169
    private function initExpectationsPath(array $config): void
170
    {
171
        $this->expectationsPath = isset($config['expectations_path']) ? new Path($config['expectations_path']) : null;
172
    }
173
174
    private function initServerFactory(array $config): void
175
    {
176
        $factory = null;
177
        if (isset($config['server_factory'])) {
178
            $factoryClassConfig = $config['server_factory'];
179
            if ($factoryClassConfig !== 'default') {
180
                $factory = $config['server_factory'];
181
            }
182
        }
183
        $this->serverFactory = $factory;
184
    }
185
186
    private function initDelay(array $config): void
187
    {
188
        if (isset($config['startDelay'])) {
189
            call_user_func($this->output, 'PHIREMOCK/DEPRECATION: startDelay option is deprecated and will be removed. Please use start_delay');
190
            $this->delay = (int) $config['startDelay'];
191
            return;
192
        }
193
194
        if ($config['start_delay']) {
195
            $this->delay = (int) $config['start_delay'];
196
        }
197
    }
198
199
    /** @throws ConfigurationException */
200
    private function initExtraInstances(array $config): void
201
    {
202
        $this->extraInstances = self::DEFAULT_EXTRA_INSTANCES;
203
        if (isset($config['extra_instances']) && is_array($config['extra_instances'])) {
204
            foreach ($config['extra_instances'] as $extraInstance) {
205
                $instanceConfig = $extraInstance + self::DEFAULT_CONFIG + ['logs_path' => Config::getDefaultLogsPath()];
206
                unset($instanceConfig['extra_instances']);
207
                $this->extraInstances[] = new self($instanceConfig, $this->output);
208
            }
209
        }
210
    }
211
212
    private function initCertificateKeyPath($config): void
213
    {
214
        $this->certificateKey = $config['certificate_key'] ? new Path($config['certificate_key']) : null;
215
    }
216
217
    private function initCertificatePath($config): void
218
    {
219
        $this->certificate = $config['certificate'] ? new Path($config['certificate']) : null;
220
    }
221
}
222