Passed
Pull Request — master (#41)
by Mariano
01:30
created

Config::getExpectationsPath()   A

Complexity

Conditions 2
Paths 2

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