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

Config   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 38
eloc 90
c 3
b 0
f 0
dl 0
loc 206
rs 9.36

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getCertificatePassphrase() 0 3 1
A getDelay() 0 3 1
A getCertificatePath() 0 3 2
A getExpectationsPath() 0 3 2
A getPhiremockPath() 0 3 1
A initServerFactory() 0 10 3
A getCertificateKeyPath() 0 3 2
A getPort() 0 3 1
A initExpectationsPath() 0 3 2
A getServerFactory() 0 3 1
A getInterface() 0 3 1
A getExtraInstances() 0 3 1
A getLogsPath() 0 3 1
A initCertificatePath() 0 3 2
A isDebugMode() 0 3 1
A initCertificateKeyPath() 0 3 2
A getDefaultLogsPath() 0 3 1
A getStartDelay() 0 3 1
A initInterfaceAndPort() 0 6 3
A initExtraInstances() 0 8 4
A initDelay() 0 10 3
A getSuites() 0 3 1
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
63
    public function __construct(array $config)
64
    {
65
        $this->initInterfaceAndPort($config);
66
        $this->initExpectationsPath($config);
67
        $this->initServerFactory($config);
68
        $this->delay = (int) $config['start_delay'];
69
        $this->phiremockPath = new Path($config['bin_path']);
70
        $this->logsPath = new Path($config['logs_path']);
71
        $this->debug = (bool) $config['debug'];
72
        $this->initCertificatePath($config);
73
        $this->initCertificateKeyPath($config);
74
        $this->certificatePassphrase = $config['cert_passphrase'];
75
        $this->initExtraInstances($config);
76
        $this->suites = $config['suites'];
77
    }
78
79
    public function getSuites(): array
80
    {
81
        return $this->suites;
82
    }
83
84
    public function getInterface(): string
85
    {
86
        return $this->interface;
87
    }
88
89
    public function getPort(): string
90
    {
91
        return $this->port;
92
    }
93
94
    public function isDebugMode(): bool
95
    {
96
        return $this->debug;
97
    }
98
99
    public function getStartDelay(): string
100
    {
101
        return $this->delay;
102
    }
103
104
    public function getPhiremockPath(): string
105
    {
106
        return $this->phiremockPath->absoluteOrRelativeToCodeceptionDir();
107
    }
108
109
    public function getExpectationsPath(): ?string
110
    {
111
        return $this->expectationsPath ? $this->expectationsPath->absoluteOrRelativeToCodeceptionDir() : null;
112
    }
113
114
    public function getCertificatePath(): ?string
115
    {
116
        return $this->certificate ? $this->certificate->absoluteOrRelativeToCodeceptionDir() : null;
117
    }
118
119
    public function getCertificateKeyPath(): ?string
120
    {
121
        return $this->certificateKey ? $this->certificateKey->absoluteOrRelativeToCodeceptionDir() : null;
122
    }
123
124
    public function getCertificatePassphrase(): ?string
125
    {
126
        return $this->certificatePassphrase;
127
    }
128
129
    public function getLogsPath(): string
130
    {
131
        return $this->logsPath->absoluteOrRelativeToCodeceptionDir();
132
    }
133
134
    public function getServerFactory(): ?string
135
    {
136
        return $this->serverFactory;
137
    }
138
139
    public function getDelay(): int
140
    {
141
        return $this->delay;
142
    }
143
144
    public function getExtraInstances(): array
145
    {
146
        return $this->extraInstances;
147
    }
148
149
    public static function getDefaultLogsPath(): string
150
    {
151
        return Configuration::logDir();
152
    }
153
154
    private function initInterfaceAndPort(array $config): void
155
    {
156
        if (isset($config['listen'])) {
157
            $parts = explode(':', $config['listen']);
158
            $this->interface = $parts[0];
159
            $this->port = (int) isset($parts[1]) ? $parts[1] : self::DEFAULT_PORT;
1 ignored issue
show
Documentation Bug introduced by
It seems like (int)IssetNode ? $parts[1] : self::DEFAULT_PORT can also be of type string. However, the property $port is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
160
        }
161
    }
162
163
    private function initExpectationsPath(array $config): void
164
    {
165
        $this->expectationsPath = isset($config['expectations_path']) ? new Path($config['expectations_path']) : null;
166
    }
167
168
    private function initServerFactory(array $config): void
169
    {
170
        $factory = null;
171
        if (isset($config['server_factory'])) {
172
            $factoryClassConfig = $config['server_factory'];
173
            if ($factoryClassConfig !== 'default') {
174
                $factory = $config['server_factory'];
175
            }
176
        }
177
        $this->serverFactory = $factory;
178
    }
179
180
    private function initDelay(array $config): void
0 ignored issues
show
Unused Code introduced by
The method initDelay() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
181
    {
182
        if (isset($config['startDelay'])) {
183
            $this->writeln('PHIREMOCK/DEPRECATION: startDelay option is deprecated and will be removed. Please use start_delay');
0 ignored issues
show
Bug introduced by
The method writeln() does not exist on Mcustiel\Phiremock\Codeception\Extension\Config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
            $this->/** @scrutinizer ignore-call */ 
184
                   writeln('PHIREMOCK/DEPRECATION: startDelay option is deprecated and will be removed. Please use start_delay');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
184
            $this->delay = $config['startDelay'];
185
            return;
186
        }
187
188
        if ($config['start_delay']) {
189
            $this->delay = $config['start_delay'];
190
        }
191
    }
192
193
    private function initExtraInstances(array $config): void
194
    {
195
        $this->extraInstances = self::DEFAULT_EXTRA_INSTANCES;
196
        if (isset($config['extra_instances']) && is_array($config['extra_instances'])) {
197
            foreach ($config['extra_instances'] as $extraInstance) {
198
                $instanceConfig = $extraInstance + self::DEFAULT_CONFIG + ['logs_path' => Config::getDefaultLogsPath()];
199
                unset($instanceConfig['extra_instances']);
200
                $this->extraInstances[] = new self($instanceConfig);
201
            }
202
        }
203
    }
204
205
    private function initCertificateKeyPath($config): void
206
    {
207
        $this->certificateKey = $config['certificate_key'] ? new Path($config['certificate_key']) : null;
208
    }
209
210
    private function initCertificatePath($config): void
211
    {
212
        $this->certificate = $config['certificate'] ? new Path($config['certificate']) : null;
213
    }
214
}
215