/
MockServerEnvConfig.php
86 lines (72 loc) · 2.48 KB
/
MockServerEnvConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace PhpPact\Standalone\MockService;
use PhpPact\Standalone\Exception\MissingEnvVariableException;
/**
* An environment variable based mock server configuration.
* Class MockServerEnvConfig.
*/
class MockServerEnvConfig extends MockServerConfig
{
const DEFAULT_SPECIFICATION_VERSION = '2.0.0';
/**
* MockServerEnvConfig constructor.
*
* @throws MissingEnvVariableException
*/
public function __construct()
{
$this->setHost($this->parseEnv('PACT_MOCK_SERVER_HOST'));
$this->setPort((int) $this->parseEnv('PACT_MOCK_SERVER_PORT'));
$this->setConsumer($this->parseEnv('PACT_CONSUMER_NAME'));
$this->setProvider($this->parseEnv('PACT_PROVIDER_NAME'));
$this->setPactDir($this->parseEnv('PACT_OUTPUT_DIR', false));
$this->setCors($this->parseEnv('PACT_CORS', false));
if ($logDir = $this->parseEnv('PACT_LOG', false)) {
$this->setLog($logDir);
}
if ($logLevel = $this->parseEnv('PACT_LOGLEVEL', false)) {
$this->setLogLevel($logLevel);
}
$timeout = $this->parseEnv('PACT_MOCK_SERVER_HEALTH_CHECK_TIMEOUT', false);
if (!$timeout) {
$timeout = 10;
}
$this->setHealthCheckTimeout($timeout);
$seconds = $this->parseEnv('PACT_MOCK_SERVER_HEALTH_CHECK_RETRY_SEC', false);
if (!$seconds) {
$seconds = 1;
}
$this->setHealthCheckRetrySec($seconds);
$version = $this->parseEnv('PACT_SPECIFICATION_VERSION', false);
if (!$version) {
$version = static::DEFAULT_SPECIFICATION_VERSION;
}
$this->setPactSpecificationVersion($version);
}
/**
* Parse environmental variables to be either null if not required or throw an error if required.
*
* @param string $variableName
* @param bool $required
*
* @throws MissingEnvVariableException
*
* @return null|string
*/
private function parseEnv(string $variableName, bool $required = true)
{
$result = null;
if (\getenv($variableName) === 'false') {
$result = false;
} elseif (\getenv($variableName) === 'true') {
$result = true;
}
if (\getenv($variableName) !== false) {
$result = \getenv($variableName);
}
if ($required === true && $result === null) {
throw new MissingEnvVariableException($variableName);
}
return $result;
}
}