Passed
Pull Request — master (#40)
by Mariano
01:15
created

Config   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 37
eloc 84
c 1
b 0
f 0
dl 0
loc 195
rs 9.44

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 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 7 4
A initDelay() 0 10 3
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
21
    public const DEFAULT_CONFIG = [
22
        'listen'            => self::DEFAULT_INTERFACE . ':' . self::DEFAULT_PORT,
23
        'debug'             => self::DEFAULT_DEBUG_MODE,
24
        'start_delay'       => self::DEFAULT_DELAY,
25
        'bin_path'          => self::DEFAULT_PHIREMOCK_PATH,
26
        'expectations_path' => self::DEFAULT_EXPECTATIONS_PATH,
27
        'server_factory'    => self::DEFAULT_SERVER_FACTORY,
28
        'certificate'       => self::DEFAULT_CERTIFICATE,
29
        'certificate_key'   => self::DEFAULT_CERTIFICATE_KEY,
30
        'cert_passphrase'   => self::DEFAULT_CERTIFICATE_PASSPHRASE,
31
        'extra_instances'   => self::DEFAULT_EXTRA_INSTANCES,
32
    ];
33
34
    /** @var string */
35
    private $interface;
36
    /** @var int */
37
    private $port;
38
    /** @var int */
39
    private $delay;
40
    /** @var bool */
41
    private $debug;
42
    /** @var Path */
43
    private $phiremockPath;
44
    /** @var Path */
45
    private $expectationsPath;
46
    /** @var Path */
47
    private $logsPath;
48
    /** @var string */
49
    private $serverFactory;
50
    /** @var Path */
51
    private $certificate;
52
    /** @var Path */
53
    private $certificateKey;
54
    /** @var string */
55
    private $certificatePassphrase;
56
    /** @var array */
57
    private $extraInstances;
58
59
    public function __construct(array $config)
60
    {
61
        $this->initInterfaceAndPort($config);
62
        $this->initExpectationsPath($config);
63
        $this->initServerFactory($config);
64
        $this->delay = (int) $config['start_delay'];
65
        $this->phiremockPath = new Path($config['bin_path']);
66
        $this->logsPath = new Path($config['logs_path']);
67
        $this->debug = (bool) $config['debug'];
68
        $this->initCertificatePath($config);
69
        $this->initCertificateKeyPath($config);
70
        $this->certificatePassphrase = $config['cert_passphrase'];
71
        $this->initExtraInstances($config);
72
    }
73
74
    public function getInterface(): string
75
    {
76
        return $this->interface;
77
    }
78
79
    public function getPort(): string
80
    {
81
        return $this->port;
82
    }
83
84
    public function isDebugMode(): bool
85
    {
86
        return $this->debug;
87
    }
88
89
    public function getStartDelay(): string
90
    {
91
        return $this->delay;
92
    }
93
94
    public function getPhiremockPath(): string
95
    {
96
        return $this->phiremockPath->absoluteOrRelativeToCodeceptionDir();
97
    }
98
99
    public function getExpectationsPath(): ?string
100
    {
101
        return $this->expectationsPath ? $this->expectationsPath->absoluteOrRelativeToCodeceptionDir() : null;
102
    }
103
104
    public function getCertificatePath(): ?string
105
    {
106
        return $this->certificate ? $this->certificate->absoluteOrRelativeToCodeceptionDir() : null;
107
    }
108
109
    public function getCertificateKeyPath(): ?string
110
    {
111
        return $this->certificateKey ? $this->certificateKey->absoluteOrRelativeToCodeceptionDir() : null;
112
    }
113
114
    public function getCertificatePassphrase(): ?string
115
    {
116
        return $this->certificatePassphrase;
117
    }
118
119
    public function getLogsPath(): string
120
    {
121
        return $this->logsPath->absoluteOrRelativeToCodeceptionDir();
122
    }
123
124
    public function getServerFactory(): ?string
125
    {
126
        return $this->serverFactory;
127
    }
128
129
    public function getDelay(): int
130
    {
131
        return $this->delay;
132
    }
133
134
    public function getExtraInstances(): array
135
    {
136
        return $this->extraInstances;
137
    }
138
139
    public static function getDefaultLogsPath(): string
140
    {
141
        return Configuration::logDir();
142
    }
143
144
    private function initInterfaceAndPort(array $config): void
145
    {
146
        if (isset($config['listen'])) {
147
            $parts = explode(':', $config['listen']);
148
            $this->interface = $parts[0];
149
            $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...
150
        }
151
    }
152
153
    private function initExpectationsPath(array $config): void
154
    {
155
        $this->expectationsPath = isset($config['expectations_path']) ? new Path($config['expectations_path']) : null;
156
    }
157
158
    private function initServerFactory(array $config): void
159
    {
160
        $factory = null;
161
        if (isset($config['server_factory'])) {
162
            $factoryClassConfig = $config['server_factory'];
163
            if ($factoryClassConfig !== 'default') {
164
                $factory = $config['server_factory'];
165
            }
166
        }
167
        $this->serverFactory = $factory;
168
    }
169
170
    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...
171
    {
172
        if (isset($config['startDelay'])) {
173
            $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

173
            $this->/** @scrutinizer ignore-call */ 
174
                   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...
174
            $this->delay = $config['startDelay'];
175
            return;
176
        }
177
178
        if ($config['start_delay']) {
179
            $this->delay = $config['start_delay'];
180
        }
181
    }
182
183
    private function initExtraInstances(array $config): void
184
    {
185
        if (isset($config['extra_instances']) && is_array($config['extra_instances'])) {
186
            foreach ($config['extra_instances'] as $extraInstance) {
187
                $instanceConfig = $extraInstance + self::DEFAULT_CONFIG + ['logs_path' => Config::getDefaultLogsPath()];
188
                unset($instanceConfig['extra_instances']);
189
                $this->extraInstances[] = new self($instanceConfig);
190
            }
191
        }
192
    }
193
194
    private function initCertificateKeyPath($config)
195
    {
196
        $this->certificateKey = $config['certificate_key'] ? new Path($config['certificate_key']) : null;
197
    }
198
199
    private function initCertificatePath($config)
200
    {
201
        $this->certificate = $config['certificate'] ? new Path($config['certificate']) : null;
202
    }
203
}
204