Completed
Pull Request — master (#32)
by Jindun
02:19
created

NewServiceEventCommand::executeJsonEvent()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 13
nop 1
dl 0
loc 39
rs 8.9617
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\AentDockerCompose\Command;
4
5
use TheAentMachine\AentDockerCompose\DockerCompose\DockerComposeService;
6
use TheAentMachine\Aenthill\Aenthill;
7
use TheAentMachine\Aenthill\CommonDependencies;
8
use TheAentMachine\Aenthill\CommonEvents;
9
use TheAentMachine\Aenthill\CommonMetadata;
10
use TheAentMachine\Aenthill\Manifest;
11
use TheAentMachine\Aenthill\Pheromone;
12
use TheAentMachine\Command\AbstractJsonEventCommand;
13
use TheAentMachine\Exception\ManifestException;
14
use TheAentMachine\Exception\MissingEnvironmentVariableException;
15
use TheAentMachine\Service\Service;
16
17
class NewServiceEventCommand extends AbstractJsonEventCommand
18
{
19
    protected function getEventName(): string
20
    {
21
        return 'NEW_SERVICE';
22
    }
23
24
    /**
25
     * @param array $payload
26
     * @return array|null
27
     * @throws ManifestException
28
     * @throws MissingEnvironmentVariableException
29
     * @throws \TheAentMachine\Service\Exception\ServiceException
30
     * @throws \TheAentMachine\Exception\CommonAentsException
31
     */
32
    protected function executeJsonEvent(array $payload): ?array
33
    {
34
        $service = Service::parsePayload($payload);
35
        if (!$service->isForMyEnvType()) {
36
            return null;
37
        }
38
39
        $fileName = Manifest::mustGetMetadata(CommonMetadata::DOCKER_COMPOSE_FILENAME_KEY);
40
        $this->getAentHelper()->title($fileName);
41
42
        $serviceName = $service->getServiceName();
43
        $formattedPayload = DockerComposeService::dockerComposeServiceSerialize($service);
44
        $this->log->debug(\GuzzleHttp\json_encode($formattedPayload, JSON_PRETTY_PRINT));
45
46
        // docker-compose
47
        $dockerComposePath = Pheromone::getContainerProjectDirectory() . '/' . $fileName;
48
        DockerComposeService::mergeContentInDockerComposeFile($formattedPayload, $dockerComposePath, true);
49
50
        // Virtual Host
51
        if ($service->getNeedVirtualHost()) {
52
            if (null === Manifest::getDependency(CommonDependencies::REVERSE_PROXY_KEY)) {
53
                $this->getAentHelper()->getCommonQuestions()->askForReverseProxy();
54
                $this->runAddReverseProxy($dockerComposePath);
55
            }
56
            $this->newVirtualHost($dockerComposePath, $serviceName);
57
        }
58
        $this->output->writeln("Service <info>$serviceName</info> has been successfully added in <info>$fileName</info>!");
59
60
        // Build
61
        if ($service->getNeedBuild()) {
62
            Aenthill::runJson(Manifest::mustGetDependency(CommonDependencies::CI_KEY), CommonEvents::NEW_BUILD_IMAGE_JOB_EVENT, $payload);
63
        }
64
65
        // Deploy
66
        if ($service->getNeedDeploy()) {
67
            Aenthill::run(Manifest::mustGetDependency(CommonDependencies::CI_KEY), CommonEvents::NEW_DEPLOY_DOCKER_COMPOSE_JOB_EVENT, $fileName);
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * @throws ManifestException
75
     * @throws \TheAentMachine\Service\Exception\ServiceException
76
     */
77
    private function runAddReverseProxy(string $dockerComposePath): void
78
    {
79
        $reverseProxyKey = Manifest::mustGetDependency(CommonDependencies::REVERSE_PROXY_KEY);
80
        $repliedPayloads = Aenthill::runJson($reverseProxyKey, CommonEvents::ADD_EVENT, []);
81
        $payload = \GuzzleHttp\json_decode($repliedPayloads[0], true);
82
        $service = Service::parsePayload($payload);
83
        $formattedPayload = DockerComposeService::dockerComposeServiceSerialize($service);
84
        DockerComposeService::mergeContentInDockerComposeFile($formattedPayload, $dockerComposePath, true);
85
86
        $serviceName = $service->getServiceName();
87
        $fileName = Manifest::mustGetMetadata(CommonMetadata::DOCKER_COMPOSE_FILENAME_KEY);
88
        $this->output->writeln("Reverse proxy <info>$serviceName</info> has been successfully added in <info>$fileName</info>!");
89
    }
90
91
    /**
92
     * @throws ManifestException
93
     * @throws \TheAentMachine\Service\Exception\ServiceException
94
     */
95
    private function newVirtualHost(string $dockerComposePath, string $serviceName, int $virtualPort = 80, string $virtualHost = null): void
96
    {
97
        $message = [
98
            'service' => $serviceName,
99
            'virtualPort' => $virtualPort
100
        ];
101
        if ($virtualHost !== null) {
102
            $message['virtualHost'] = $virtualHost;
103
        }
104
        $reverseProxyKey = Manifest::mustGetDependency(CommonDependencies::REVERSE_PROXY_KEY);
105
        $repliedPayloads = Aenthill::runJson($reverseProxyKey, CommonEvents::NEW_VIRTUAL_HOST_EVENT, $message);
106
        $payload = \GuzzleHttp\json_decode($repliedPayloads[0], true);
107
        $service = Service::parsePayload($payload);
108
        $formattedPayload = DockerComposeService::dockerComposeServiceSerialize($service);
109
        DockerComposeService::mergeContentInDockerComposeFile($formattedPayload, $dockerComposePath, true);
110
111
        $serviceName = $service->getServiceName();
112
        $fileName = Manifest::mustGetMetadata(CommonMetadata::DOCKER_COMPOSE_FILENAME_KEY);
113
        $this->output->writeln("A new virtual host has been successfully added for <info>$serviceName</info> in <info>$fileName</info>!");
114
    }
115
}
116