-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalConfig.php
More file actions
165 lines (149 loc) · 4.13 KB
/
LocalConfig.php
File metadata and controls
165 lines (149 loc) · 4.13 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php namespace MyENA\PHPIPAMAPI\Config;
use MyENA\PHPIPAMAPI\Config;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
/**
* Class LocalConfig
* @package MyENA\PHPIPAMAPI\Config
*/
class LocalConfig implements Config {
/** @var string */
protected $host;
/** @var int */
protected $port;
/** @var bool */
protected $https = true;
/** @var string */
protected $username;
/** @var string */
protected $password;
/** @var string */
protected $appID;
/** @var string */
protected $appCode;
/** @var bool */
protected $silent;
/** @var \GuzzleHttp\ClientInterface */
protected $client;
/**
* Config constructor.
* @param array $config
* @param \GuzzleHttp\ClientInterface|null $client
*/
public function __construct(array $config, ?ClientInterface $client = null) {
$this->processConfig($config);
$this->validate();
$this->client = $client;
}
/**
* Returns configured php-ipam service host
*
* @return string
*/
public function getHost(): string {
return $this->host;
}
/**
* Returns configured php-ipam service host
*
* @return int
*/
public function getPort(): int {
return $this->port;
}
/**
* Returns whether communication will happen over https or not
*
* @return bool
*/
public function useHTTPS(): bool {
return $this->https;
}
/**
* Returns configured php-ipam user username
*
* @return string
*/
public function getUsername(): string {
return $this->username;
}
/**
* Returns configured php-ipam user password
*
* @return string
*/
public function getPassword(): string {
return $this->password;
}
/**
* Returns configured api app id
*
* @return string
*/
public function getAppID(): string {
return $this->appID;
}
/**
* Returns configured api app code
*
* @return string
*/
public function getAppCode(): string {
return $this->appCode;
}
/**
* Returns GuzzleHttp-compatible http client, optionally constructing a new one if one was not provided at construction
*
* @return \GuzzleHttp\ClientInterface
*/
public function getClient(): ClientInterface {
if (!isset($this->client)) {
$this->client = new Client();
}
return $this->client;
}
/**
* @return bool
*/
public function silent(): bool {
return $this->silent;
}
/**
* Takes provided config input and defines local vars, optionally setting empty or default values if fields are omitted.
*
* @param array $config
*/
protected function processConfig(array $config): void {
$this->host = trim((string)($config['host'] ?? ''));
$this->port = (int)($config['port'] ?? '');
$this->https = (bool)($config['https'] ?? true);
$this->username = trim((string)($config['username'] ?? ''));
$this->password = trim((string)($config['password'] ?? ''));
$this->appID = trim((string)($config['appid'] ?? ''));
$this->appCode = trim((string)($config['appcode'] ?? ''));
$this->silent = (bool)($config['silent'] ?? false);
}
/**
* Perform post-config processing validation
*/
protected function validate(): void {
if ('' === $this->host) {
throw new \InvalidArgumentException('host cannot be empty');
}
if (0 > $this->port) {
throw new \InvalidArgumentException('port must be > 0');
}
if ('' === $this->username) {
throw new \InvalidArgumentException('username cannot be empty');
}
if ('' === $this->password) {
throw new \InvalidArgumentException('password cannot be empty');
}
if ('' === $this->appID) {
throw new \InvalidArgumentException('appid cannot be empty');
}
if ('' === $this->appCode) {
throw new \InvalidArgumentException('appcode cannot be empty');
}
}
}