Passed
Pull Request — master (#39)
by Mariano
02:50
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_SERVER_FACTORY = 'default';
16
    public const DEFAULT_EXTRA_INSTANCES = [];
17
18
    public const DEFAULT_CONFIG = [
19
        'listen'            => self::DEFAULT_INTERFACE . ':' . self::DEFAULT_PORT,
20
        'debug'             => self::DEFAULT_DEBUG_MODE,
21
        'start_delay'       => self::DEFAULT_DELAY,
22
        'bin_path'          => self::DEFAULT_PHIREMOCK_PATH,
23
        'expectations_path' => self::DEFAULT_EXPECTATIONS_PATH,
24
        'server_factory'    => self::DEFAULT_SERVER_FACTORY,
25
        'extra_instances'   => self::DEFAULT_EXTRA_INSTANCES,
26
    ];
27
28
    /** @var string */
29
    private $interface;
30
    /** @var int */
31
    private $port;
32
    /** @var int */
33
    private $delay;
34
    /** @var bool */
35
    private $debug;
36
    /** @var Path */
37
    private $phiremockPath;
38
    /** @var Path */
39
    private $expectationsPath;
40
    /** @var Path */
41
    private $logsPath;
42
    /** @var string */
43
    private $serverFactory;
44
    /** @var array */
45
    private $extraInstances;
46
47
    public function __construct(array $config)
48
    {
49
        $this->initInterfaceAndPort($config);
50
        $this->initExpectationsPath($config);
51
        $this->initServerFactory($config);
52
        $this->delay = (int) $config['start_delay'];
53
        $this->phiremockPath = new Path($config['bin_path']);
54
        $this->logsPath = new Path($config['logs_path']);
55
        $this->debug = (bool) $config['debug'];
56
        $this->initExtraInstances($config);
57
    }
58
59
    public function getInterface(): string
60
    {
61
        return $this->interface;
62
    }
63
64
    public function getPort(): string
65
    {
66
        return $this->port;
67
    }
68
69
    public function isDebugMode(): bool
70
    {
71
        return $this->debug;
72
    }
73
74
    public function getStartDelay(): string
75
    {
76
        return $this->delay;
77
    }
78
79
    public function getPhiremockPath(): string
80
    {
81
        return $this->phiremockPath->absoluteOrRelativeToCodeceptionDir();
82
    }
83
84
    public function getExpectationsPath(): ?string
85
    {
86
        return $this->expectationsPath ? $this->expectationsPath->absoluteOrRelativeToCodeceptionDir() : null;
87
    }
88
89
    public function getLogsPath(): string
90
    {
91
        return $this->logsPath->absoluteOrRelativeToCodeceptionDir();
92
    }
93
94
    public function getServerFactory(): ?string
95
    {
96
        return $this->serverFactory;
97
    }
98
99
    public function getDelay(): int
100
    {
101
        return $this->delay;
102
    }
103
104
    public function getExtraInstances(): array
105
    {
106
        return $this->extraInstances;
107
    }
108
109
    public static function getDefaultLogsPath(): string
110
    {
111
        return Configuration::logDir();
112
    }
113
114
    private function initInterfaceAndPort(array $config): void
115
    {
116
        if (isset($config['listen'])) {
117
            $parts = explode(':', $config['listen']);
118
            $this->interface = $parts[0];
119
            $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...
120
        }
121
    }
122
123
    private function initExpectationsPath(array $config): void
124
    {
125
        $this->expectationsPath = isset($config['expectations_path']) ? new Path($config['expectations_path']) : null;
126
    }
127
128
    private function initServerFactory(array $config): void
129
    {
130
        $factory = null;
131
        if (isset($config['server_factory'])) {
132
            $factoryClassConfig = $config['server_factory'];
133
            if ($factoryClassConfig !== 'default') {
134
                $factory = $config['server_factory'];
135
            }
136
        }
137
        $this->serverFactory = $factory;
138
    }
139
140
    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...
141
    {
142
        if (isset($config['startDelay'])) {
143
            $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

143
            $this->/** @scrutinizer ignore-call */ 
144
                   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...
144
            $this->delay = $config['startDelay'];
145
            return;
146
        }
147
148
        if ($config['start_delay']) {
149
            $this->delay = $config['start_delay'];
150
        }
151
    }
152
153
    private function initExtraInstances(array $config): void
154
    {
155
        if (isset($config['extra_instances']) && is_array($config['extra_instances'])) {
156
            foreach ($config['extra_instances'] as $extraInstance) {
157
                $instanceConfig = $extraInstance + self::DEFAULT_CONFIG + ['logs_path' => Config::getDefaultLogsPath()];
158
                unset($instanceConfig['extra_instances']);
159
                $this->extraInstances[] = new self($instanceConfig);
160
            }
161
        }
162
    }
163
}
164