Completed
Push — master ( 328402...1cd18c )
by Julien
587:13 queued 584:54
created

AbstractOrchestratorAddEvent::finish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\Aent\Event\Orchestrator;
4
5
use Safe\Exceptions\StringsException;
6
use TheAentMachine\Aent\Context\BaseOrchestratorContext;
7
use TheAentMachine\Aent\Context\Context;
8
use TheAentMachine\Aent\Context\ContextInterface;
9
use TheAentMachine\Aent\Event\AbstractJsonEvent;
10
use TheAentMachine\Aent\Payload\Bootstrap\BootstrapPayload;
11
use TheAentMachine\Aent\Registry\ColonyRegistry;
12
use TheAentMachine\Aent\Registry\Exception\ColonyRegistryException;
13
use TheAentMachine\Aenthill\Aenthill;
14
use function Safe\sprintf;
15
16
abstract class AbstractOrchestratorAddEvent extends AbstractJsonEvent
17
{
18
    /**
19
     * @return ContextInterface
20
     */
21
    abstract protected function setup(): ContextInterface;
22
23
    /**
24
     * @param ContextInterface $context
25
     * @return ContextInterface
26
     */
27
    abstract protected function addDeployJobInCI(ContextInterface $context): ContextInterface;
28
29
    /**
30
     * @return string
31
     */
32
    protected function getEventName(): string
33
    {
34
        return 'ADD_ORCHESTRATOR';
35
    }
36
37
    /**
38
     * @return bool
39
     */
40
    protected function shouldRegisterEvents(): bool
41
    {
42
        return true;
43
    }
44
45
    /**
46
     * @return void
47
     * @throws StringsException
48
     */
49
    protected function beforeExecute(): void
50
    {
51
        /** @var Context $context */
52
        $context = Context::fromMetadata();
53
        $this->output->writeln(sprintf(
54
            "\n👋 Hello! I'm the aent <info>%s</info> and I'm going to setup myself as orchestrator for <info>%s</info> environment <info>%s</info>.",
55
            $this->getAentName(),
56
            $context->getEnvironmentType(),
57
            $context->getEnvironmentName()
58
        ));
59
    }
60
61
    /**
62
     * @param mixed[] $payload
63
     * @return mixed[]|null
64
     * @throws ColonyRegistryException
65
     * @throws StringsException
66
     */
67
    protected function executeJsonEvent(array $payload): ?array
68
    {
69
        $orchestratorContext = $this->setup();
70
        $this->registerDockerfileBuilder();
71
        /** @var Context $context */
72
        $context = Context::fromMetadata();
73
        if (!$context->isDevelopment() && !empty($payload)) {
74
            $payload = BootstrapPayload::fromArray($payload);
75
            $this->registerCI($payload);
76
            $orchestratorContext = $this->addDeployJobInCI($orchestratorContext);
77
        }
78
        $this->finish($orchestratorContext);
79
        return null;
80
    }
81
82
    /**
83
     * @return void
84
     * @throws ColonyRegistryException
85
     * @throws StringsException
86
     */
87
    private function registerDockerfileBuilder(): void
88
    {
89
        $this->prompt->printAltBlock(sprintf("%s: registering Dockerfile builder...", $this->getAentName()));
90
        $registry = ColonyRegistry::builderRegistry();
91
        $aent = $registry->getAent(ColonyRegistry::DOCKERFILE);
92
        /** @var Context $context */
93
        $context = Context::fromMetadata();
94
        Aenthill::register($aent->getImage(), BaseOrchestratorContext::BUILDER_DEPENDENCY_KEY, $context->toArray());
95
    }
96
97
    /**
98
     * @param BootstrapPayload $payload
99
     * @throws StringsException
100
     */
101
    private function registerCI(BootstrapPayload $payload): void
102
    {
103
        $this->prompt->printAltBlock(sprintf("%s: registering CI/CD provider...", $this->getAentName()));
104
        $aent = $payload->getCIAent();
105
        /** @var Context $context */
106
        $context = Context::fromMetadata();
107
        $metadata = \array_merge($context->toArray(), $payload->getCIMetadata());
108
        Aenthill::register($aent->getImage(), BaseOrchestratorContext::CI_DEPENDENCY_KEY, $metadata);
109
    }
110
111
    /**
112
     * @param ContextInterface $context
113
     * @throws StringsException
114
     */
115
    private function finish(ContextInterface $context): void
116
    {
117
        $this->prompt->printAltBlock(sprintf("%s: finishing...", $this->getAentName()));
118
        $context->toMetadata();
119
    }
120
121
    /**
122
     * @return void
123
     * @throws StringsException
124
     */
125
    protected function afterExecute(): void
126
    {
127
        /** @var Context $context */
128
        $context = Context::fromMetadata();
129
        $this->output->writeln(
130
            sprintf(
131
                "\nI've finished my setup for your <info>%s</info> environment <info>%s</info>. See you later!",
132
                $context->getEnvironmentType(),
133
                $context->getEnvironmentName()
134
            )
135
        );
136
    }
137
}
138