|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine\Aent\Event\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Safe\Exceptions\StringsException; |
|
6
|
|
|
use TheAentMachine\Aent\Context\Context; |
|
7
|
|
|
use TheAentMachine\Aent\Event\AbstractEvent; |
|
8
|
|
|
use TheAentMachine\Aent\Event\Service\Model\Environments; |
|
9
|
|
|
use TheAentMachine\Aent\Event\Service\Model\ServiceState; |
|
10
|
|
|
use TheAentMachine\Aenthill\Aenthill; |
|
11
|
|
|
use TheAentMachine\Service\Service; |
|
12
|
|
|
use function Safe\sprintf; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractServiceAddEvent extends AbstractEvent |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param Environments $environments |
|
18
|
|
|
* @return ServiceState[] |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract protected function createServices(Environments $environments): array; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function getEventName(): string |
|
26
|
|
|
{ |
|
27
|
|
|
return 'ADD'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function shouldRegisterEvents(): bool |
|
34
|
|
|
{ |
|
35
|
|
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return void |
|
40
|
|
|
* @throws StringsException |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function beforeExecute(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->output->writeln(sprintf( |
|
45
|
|
|
"š Hello! I'm the aent <info>%s</info> and I'm going to setup myself!", |
|
46
|
|
|
$this->getAentName() |
|
47
|
|
|
)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param null|string $payload |
|
52
|
|
|
* @return null|string |
|
53
|
|
|
* @throws StringsException |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function executeEvent(?string $payload): ?string |
|
56
|
|
|
{ |
|
57
|
|
|
$environments = $this->fetchEnvironments(); |
|
58
|
|
|
$this->prompt->printAltBlock(sprintf("%s: configuring service(s)...", $this->getAentName())); |
|
59
|
|
|
$servicesStates = $this->createServices($environments); |
|
60
|
|
|
foreach ($servicesStates as $serviceState) { |
|
61
|
|
|
$this->dispatch($environments->getDevelopmentEnvironments(), $serviceState->getDevelopmentVersion()); |
|
62
|
|
|
$this->dispatch($environments->getTestEnvironments(), $serviceState->getTestVersion()); |
|
63
|
|
|
$this->dispatch($environments->getProductionEnvironments(), $serviceState->getProductionVersion()); |
|
64
|
|
|
} |
|
65
|
|
|
$this->prompt->printBlock("Dispatch done."); |
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return void |
|
71
|
|
|
* @throws StringsException |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function afterExecute(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->output->writeln(sprintf("\nš Hello again! This is the aent <info>%s</info> and we have finished my setup.", $this->getAentName())); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Environments |
|
80
|
|
|
* @throws StringsException |
|
81
|
|
|
*/ |
|
82
|
|
|
private function fetchEnvironments(): Environments |
|
83
|
|
|
{ |
|
84
|
|
|
$this->prompt->printAltBlock(sprintf("%s: fetching environments...", $this->getAentName())); |
|
85
|
|
|
$responses = Aenthill::dispatchJson('CHOOSE_ENVIRONMENT', []); |
|
86
|
|
|
if (empty($responses)) { |
|
87
|
|
|
$this->byebye(); |
|
88
|
|
|
} |
|
89
|
|
|
$environments = new Environments(); |
|
90
|
|
|
foreach ($responses as $response) { |
|
91
|
|
|
if (!empty($response)) { |
|
92
|
|
|
$context = Context::fromArray($response); |
|
93
|
|
|
$environments->add($context); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
if ($environments->isEmpty()) { |
|
97
|
|
|
$this->byebye(); |
|
98
|
|
|
} |
|
99
|
|
|
return $environments; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return void |
|
104
|
|
|
* @throws StringsException |
|
105
|
|
|
*/ |
|
106
|
|
|
private function byebye(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$this->output->writeln(sprintf("\nš Hello again! This is the aent <info>%s</info> and you have not selected any environment! Bye!", $this->getAentName())); |
|
109
|
|
|
exit(0); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param Context[] $environments |
|
114
|
|
|
* @param null|Service $service |
|
115
|
|
|
* @throws StringsException |
|
116
|
|
|
*/ |
|
117
|
|
|
private function dispatch(array $environments, ?Service $service): void |
|
118
|
|
|
{ |
|
119
|
|
|
if (empty($environments || empty($service))) { |
|
|
|
|
|
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
foreach ($environments as $environment) { |
|
123
|
|
|
$this->prompt->printBlock(sprintf( |
|
124
|
|
|
"Dispatching service %s for %s environment %s.", |
|
125
|
|
|
$service->getServiceName(), |
|
|
|
|
|
|
126
|
|
|
$environment->getEnvironmentType(), |
|
127
|
|
|
$environment->getEnvironmentName() |
|
128
|
|
|
)); |
|
129
|
|
|
Aenthill::dispatchJson('NEW_SERVICE', $service, sprintf('"ENVIRONMENT_NAME" in Metadata and Metadata["ENVIRONMENT_NAME"] == "%s"', $environment->getEnvironmentName())); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.