Passed
Pull Request — master (#48)
by
unknown
20:54
created

Config::getDefaultLogsPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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