This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 240
/
UpdatePartiallyPlatformInformation.php
332 lines (304 loc) · 12 KB
/
UpdatePartiallyPlatformInformation.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/*
* Copyright 2005 - 2021 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);
namespace Centreon\Domain\PlatformInformation\UseCase\V20;
use Centreon\Domain\Proxy\Proxy;
use Centreon\Domain\RemoteServer\RemoteServerException;
use Centreon\Domain\PlatformInformation\Model\Information;
use Centreon\Domain\Proxy\Interfaces\ProxyServiceInterface;
use Centreon\Domain\PlatformInformation\Model\InformationFactory;
use Centreon\Domain\PlatformInformation\Model\PlatformInformation;
use Centreon\Domain\PlatformInformation\Interfaces\DtoValidatorInterface;
use Centreon\Domain\PlatformInformation\Model\PlatformInformationFactory;
use Centreon\Domain\RemoteServer\Interfaces\RemoteServerServiceInterface;
use Centreon\Domain\PlatformInformation\Exception\PlatformInformationException;
use Centreon\Domain\PlatformTopology\Interfaces\PlatformTopologyServiceInterface;
use Centreon\Domain\PlatformInformation\Interfaces\PlatformInformationReadRepositoryInterface;
use Centreon\Domain\PlatformInformation\Interfaces\PlatformInformationWriteRepositoryInterface;
class UpdatePartiallyPlatformInformation
{
/**
* @var PlatformInformationWriteRepositoryInterface
*/
private $writeRepository;
/**
* @var PlatformInformationReadRepositoryInterface
*/
private $readRepository;
/**
* @var ProxyServiceInterface
*/
private $proxyService;
/**
* @var RemoteServerServiceInterface
*/
private $remoteServerService;
/**
* @var PlatformTopologyServiceInterface
*/
private $platformTopologyService;
/**
* Array of all available validators for this use case.
*
* @var array<DtoValidatorInterface>
*/
private $validators = [];
/**
* @var string|null
*/
private $encryptionFirstKey;
public function __construct(
PlatformInformationWriteRepositoryInterface $writeRepository,
PlatformInformationReadRepositoryInterface $readRepository,
ProxyServiceInterface $proxyService,
RemoteServerServiceInterface $remoteServerService,
PlatformTopologyServiceInterface $platformTopologyService
) {
$this->writeRepository = $writeRepository;
$this->readRepository = $readRepository;
$this->proxyService = $proxyService;
$this->remoteServerService = $remoteServerService;
$this->platformTopologyService = $platformTopologyService;
}
public function setEncryptionFirstKey(?string $encryptionFirstKey): void
{
$this->encryptionFirstKey = $encryptionFirstKey;
}
/**
* @param array<DtoValidatorInterface> $validators
*/
public function addValidators(array $validators): void
{
foreach ($validators as $validator) {
$this->addValidator($validator);
}
}
/**
* @param DtoValidatorInterface $dtoValidator
*/
private function addValidator(DtoValidatorInterface $dtoValidator): void
{
$this->validators[] = $dtoValidator;
}
/**
* Execute the use case for which this class was designed.
*
* @param array<string,mixed> $request
* @throws \Throwable
*/
public function execute(array $request): void
{
foreach ($this->validators as $validator) {
$validator->validateOrFail($request);
}
/**
* Create Information from Factory to be able to access them independently
* and validate the length of each value.
*/
$informationList = InformationFactory::createFromDto($request);
$currentPlatformInformation = $this->readRepository->findPlatformInformation();
$platformInformationFactory = new PlatformInformationFactory($this->encryptionFirstKey);
/**
* Take account if the isRemote request key is provided to create the PlatformInformation and trigger
* the conversion.
* If this key is not provided in the request, we dont want to convert the platform
* and just update the informations.
*/
if (
(isset($request["isRemote"]) && $request["isRemote"] === true)
|| (!isset($request["isRemote"]) && $currentPlatformInformation->isRemote() === true)
) {
$platformInformationToUpdate = $platformInformationFactory->createRemoteInformation($request);
} else {
$platformInformationToUpdate = $platformInformationFactory->createCentralInformation();
}
if (isset($request["isRemote"])) {
$this->updateRemoteOrCentralType($platformInformationToUpdate, $currentPlatformInformation);
}
foreach ($informationList as $information) {
if ($information->getKey() === "proxy") {
$this->updateProxyOptions($information, $platformInformationToUpdate->getCentralServerAddress());
}
}
$this->writeRepository->updatePlatformInformation($platformInformationToUpdate);
}
/**
* Update Proxy Options.
*
* @param Information $proxyInformation
* @param string|null $centralServerAddress
* @throws \InvalidArgumentException
*/
private function updateProxyOptions(Information $proxyInformation, ?string $centralServerAddress): void
{
/**
* Verify that proxy address and central address are different before continue the update.
*/
$proxyOptions = $proxyInformation->getValue();
if ($centralServerAddress !== null && isset($proxyOptions['host'])) {
$this->validateCentralServerAddressOrFail(
$centralServerAddress,
$proxyOptions['host']
);
}
$proxy = new Proxy();
if (isset($proxyOptions['host'])) {
$proxy->setUrl($proxyOptions['host']);
}
if (isset($proxyOptions['scheme'])) {
$proxy->setProtocol($proxyOptions['scheme']);
}
if (isset($proxyOptions['port'])) {
$proxy->setPort($proxyOptions['port']);
}
if (isset($proxyOptions['user'])) {
$proxy->setUser($proxyOptions['user']);
if (isset($proxyOptions['password'])) {
$proxy->setPassword($proxyOptions['password']);
}
}
$this->proxyService->updateProxy($proxy);
}
/**
* Update the Platform Type
*
* @param PlatformInformation $platformInformationToUpdate
* @param PlatformInformation $currentPlatformInformation
* @throws RemoteServerException
*/
private function updateRemoteOrCentralType(
PlatformInformation $platformInformationToUpdate,
PlatformInformation $currentPlatformInformation
): void {
if ($platformInformationToUpdate->isRemote()) {
$this->convertCentralToRemote($platformInformationToUpdate, $currentPlatformInformation);
} elseif ($platformInformationToUpdate->isRemote() === false && $currentPlatformInformation->isRemote()) {
/**
* Use the current information
* as they contains all the information required to remove the Remote to its Parent
*/
$this->remoteServerService->convertRemoteToCentral($currentPlatformInformation);
}
}
/**
* This method verify the existing and updated type before sending information to Remote Server Service.
*
* @param PlatformInformation $platformInformationToUpdate
* @param PlatformInformation $currentPlatformInformation
* @throws RemoteServerException
* @return void
*/
private function convertCentralToRemote(
PlatformInformation $platformInformationToUpdate,
PlatformInformation $currentPlatformInformation
): void {
/**
* If some parameters required fort registering the Remote Server are missing,
* populate them with existing values.
*/
$platformInformationToUpdate = $this->populateMissingInformationValues(
$platformInformationToUpdate,
$currentPlatformInformation
);
$this->remoteServerService->convertCentralToRemote(
$platformInformationToUpdate
);
}
/**
* Validate that central server address is not already in used by another platform or by the proxy.
*
* @param string $centralServerAddress
* @param string|null $proxyAddress
* @throws PlatformInformationException
*/
private function validateCentralServerAddressOrFail(
string $centralServerAddress,
?string $proxyAddress = null
): void {
$platforms = $this->platformTopologyService->getPlatformTopology();
foreach ($platforms as $platform) {
if ($centralServerAddress === $platform->getAddress()) {
throw new PlatformInformationException(
sprintf(
_('the address %s is already used in the topology and can\'t ' .
'be provided as Central Server Address'),
$centralServerAddress
)
);
}
}
if ($centralServerAddress === $proxyAddress) {
throw new PlatformInformationException(
sprintf(
_('the address %s is already used has proxy address and can\'t ' .
'be provided as Central Server Address'),
$centralServerAddress
)
);
}
}
/**
* Populate the PlatformInformation missing values.
* This method is useful if some properties are already existing in data storage.
*
* @param PlatformInformation $platformInformationToUpdate
* @param PlatformInformation $currentPlatformInformation
* @return PlatformInformation
*/
private function populateMissingInformationValues(
PlatformInformation $platformInformationToUpdate,
PlatformInformation $currentPlatformInformation
): PlatformInformation {
if ($platformInformationToUpdate->getCentralServerAddress() !== null) {
$this->validateCentralServerAddressOrFail($platformInformationToUpdate->getCentralServerAddress());
}
if ($platformInformationToUpdate->getCentralServerAddress() === null) {
$platformInformationToUpdate->setCentralServerAddress(
$currentPlatformInformation->getCentralServerAddress()
);
}
if ($platformInformationToUpdate->getApiCredentials() === null) {
$platformInformationToUpdate->setApiCredentials(
$currentPlatformInformation->getApiCredentials()
);
}
if ($platformInformationToUpdate->getApiUsername() === null) {
$platformInformationToUpdate->setApiUsername(
$currentPlatformInformation->getApiUsername()
);
}
if ($platformInformationToUpdate->getApiPath() === null) {
$platformInformationToUpdate->setApiPath(
$currentPlatformInformation->getApiPath()
);
}
if ($platformInformationToUpdate->getApiScheme() === null) {
$platformInformationToUpdate->setApiScheme(
$currentPlatformInformation->getApiScheme()
);
}
if ($platformInformationToUpdate->getApiPort() === null) {
$platformInformationToUpdate->setApiPort(
$currentPlatformInformation->getApiPort()
);
}
return $platformInformationToUpdate;
}
}