Passed
Pull Request — master (#40)
by Mariano
02:38
created

Config::getInterface()   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
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->setDefaults();
62
        $this->initInterfaceAndPort($config);
63
        $this->initExpectationsPath($config);
64
        $this->initServerFactory($config);
65
        $this->delay = (int) $config['start_delay'];
66
        $this->phiremockPath = new Path($config['bin_path']);
67
        $this->logsPath = new Path($config['logs_path']);
68
        $this->debug = (bool) $config['debug'];
69
        $this->initCertificatePath($config);
70
        $this->initCertificateKeyPath($config);
71
        $this->certificatePassphrase = $config['cert_passphrase'];
72
        $this->initExtraInstances($config);
73
    }
74
75
    public function getInterface(): string
76
    {
77
        return $this->interface;
78
    }
79
80
    public function getPort(): string
81
    {
82
        return $this->port;
83
    }
84
85
    public function isDebugMode(): bool
86
    {
87
        return $this->debug;
88
    }
89
90
    public function getStartDelay(): string
91
    {
92
        return $this->delay;
93
    }
94
95
    public function getPhiremockPath(): string
96
    {
97
        return $this->phiremockPath->absoluteOrRelativeToCodeceptionDir();
98
    }
99
100
    public function getExpectationsPath(): ?string
101
    {
102
        return $this->expectationsPath ? $this->expectationsPath->absoluteOrRelativeToCodeceptionDir() : null;
103
    }
104
105
    public function getCertificatePath(): ?string
106
    {
107
        return $this->certificate ? $this->certificate->absoluteOrRelativeToCodeceptionDir() : null;
108
    }
109
110
    public function getCertificateKeyPath(): ?string
111
    {
112
        return $this->certificateKey ? $this->certificateKey->absoluteOrRelativeToCodeceptionDir() : null;
113
    }
114
115
    public function getCertificatePassphrase(): ?string
116
    {
117
        return $this->certificatePassphrase;
118
    }
119
120
    public function getLogsPath(): string
121
    {
122
        return $this->logsPath->absoluteOrRelativeToCodeceptionDir();
123
    }
124
125
    public function getServerFactory(): ?string
126
    {
127
        return $this->serverFactory;
128
    }
129
130
    public function getDelay(): int
131
    {
132
        return $this->delay;
133
    }
134
135
    public function getExtraInstances(): array
136
    {
137
        return $this->extraInstances;
138
    }
139
140
    public static function getDefaultLogsPath(): string
141
    {
142
        return Configuration::logDir();
143
    }
144
145
    private function initInterfaceAndPort(array $config): void
146
    {
147
        if (isset($config['listen'])) {
148
            $parts = explode(':', $config['listen']);
149
            $this->interface = $parts[0];
150
            $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...
151
        }
152
    }
153
154
    private function initExpectationsPath(array $config): void
155
    {
156
        $this->expectationsPath = isset($config['expectations_path']) ? new Path($config['expectations_path']) : null;
157
    }
158
159
    private function initServerFactory(array $config): void
160
    {
161
        $factory = null;
162
        if (isset($config['server_factory'])) {
163
            $factoryClassConfig = $config['server_factory'];
164
            if ($factoryClassConfig !== 'default') {
165
                $factory = $config['server_factory'];
166
            }
167
        }
168
        $this->serverFactory = $factory;
169
    }
170
171
    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...
172
    {
173
        if (isset($config['startDelay'])) {
174
            $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

174
            $this->/** @scrutinizer ignore-call */ 
175
                   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...
175
            $this->delay = $config['startDelay'];
176
            return;
177
        }
178
179
        if ($config['start_delay']) {
180
            $this->delay = $config['start_delay'];
181
        }
182
    }
183
184
    private function initExtraInstances(array $config): void
185
    {
186
        if (isset($config['extra_instances']) && is_array($config['extra_instances'])) {
187
            foreach ($config['extra_instances'] as $extraInstance) {
188
                $instanceConfig = $extraInstance + self::DEFAULT_CONFIG + ['logs_path' => Config::getDefaultLogsPath()];
189
                unset($instanceConfig['extra_instances']);
190
                $this->extraInstances[] = new self($instanceConfig);
191
            }
192
        }
193
    }
194
195
    private function initCertificateKeyPath($config): void
196
    {
197
        $this->certificateKey = $config['certificate_key'] ? new Path($config['certificate_key']) : null;
198
    }
199
200
    private function initCertificatePath($config): void
201
    {
202
        $this->certificate = $config['certificate'] ? new Path($config['certificate']) : null;
203
    }
204
205
    private function setDefaults(): void
206
    {
207
        $this->extraInstances = self::DEFAULT_EXTRA_INSTANCES;
208
        $this->delay = self::DEFAULT_DELAY;
209
        $this->debug = self::DEFAULT_DEBUG_MODE;
210
        $this->certificate = self::DEFAULT_CERTIFICATE;
211
        $this->certificateKey = self::DEFAULT_CERTIFICATE_KEY;
212
        $this->certificatePassphrase = self::DEFAULT_CERTIFICATE_PASSPHRASE;
213
        $this->logs = self::getDefaultLogsPath();
0 ignored issues
show
Bug Best Practice introduced by
The property logs does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
214
        $this->serverFactory = self::DEFAULT_SERVER_FACTORY;
215
        $this->phiremockPath = self::DEFAULT_PHIREMOCK_PATH;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::DEFAULT_PHIREMOCK_PATH of type string is incompatible with the declared type Mcustiel\Phiremock\Codeception\Extension\Path of property $phiremockPath.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
216
    }
217
}
218