NewServiceEvent::createDotEnv()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\AentDockerCompose\Event;
4
5
use Safe\Exceptions\FilesystemException;
6
use Safe\Exceptions\PcreException;
7
use Safe\Exceptions\StringsException;
8
use TheAentMachine\Aent\Event\Orchestrator\AbstractOrchestratorNewServiceEvent;
9
use TheAentMachine\Aent\Payload\ReverseProxy\ReverseProxyNewVirtualHostPayload;
10
use TheAentMachine\AentDockerCompose\Context\DockerComposeContext;
11
use TheAentMachine\AentDockerCompose\Helper\DockerComposeHelper;
12
use TheAentMachine\AentDockerCompose\Helper\EnvFileHelper;
13
use TheAentMachine\Aenthill\Aenthill;
14
use TheAentMachine\Exception\MissingEnvironmentVariableException;
15
use TheAentMachine\Service\Environment\SharedEnvVariable;
16
use TheAentMachine\Service\Exception\ServiceException;
17
use TheAentMachine\Service\Service;
18
19
final class NewServiceEvent extends AbstractOrchestratorNewServiceEvent
20
{
21
    /** @var DockerComposeContext */
22
    private $context;
23
24
    /**
25
     * @param Service $service
26
     * @throws FilesystemException
27
     * @throws PcreException
28
     * @throws ServiceException
29
     * @throws StringsException
30
     * @throws MissingEnvironmentVariableException
31
     */
32
    protected function finalizeService(Service $service): void
33
    {
34
        $this->context = DockerComposeContext::fromMetadata();
35
        if (!empty($service->getVirtualHosts())) {
36
            $this->prompt->printAltBlock('Docker Compose: adding virtual host');
37
            $service = $this->addVirtualHost($service);
38
        }
39
        if (!empty(DockerComposeHelper::getEnvironmentVariablesForDotEnv($service))) {
40
            $this->prompt->printAltBlock('Docker Compose: creating dot env file...');
41
            $serialized = $this->createDotEnv($service);
42
        } else {
43
            $serialized= DockerComposeHelper::dockerComposeServiceSerialize($service);
44
        }
45
        DockerComposeHelper::mergeContentInDockerComposeFile($serialized, $this->context->getDockerComposeFilePath(), true);
46
    }
47
48
    /**
49
     * @param Service $service
50
     * @return Service
51
     * @throws ServiceException
52
     * @throws FilesystemException
53
     */
54
    private function addVirtualHost(Service $service): Service
55
    {
56
        $payload = new ReverseProxyNewVirtualHostPayload($this->context->getBaseVirtualHost(), $service);
57
        $response = Aenthill::runJson(DockerComposeContext::REVERSE_PROXY_SERVICE_DEPENDENCY_KEY, 'NEW_VIRTUAL_HOST', $payload->toArray());
58
        $assoc = \GuzzleHttp\json_decode($response[0], true);
59
        $service = Service::parsePayload($assoc);
60
        return $service;
61
    }
62
63
    /**
64
     * @param Service $service
65
     * @return array|mixed
66
     * @throws FilesystemException
67
     * @throws PcreException
68
     */
69
    private function createDotEnv(Service $service)
70
    {
71
        $envMapDotEnvFile = DockerComposeHelper::getEnvironmentVariablesForDotEnv($service);
72
        $envFilePaths = [];
73
        /**
74
         * @var string $key
75
         * @var SharedEnvVariable $sharedEnvVariable
76
         */
77
        foreach ($envMapDotEnvFile as $key => $sharedEnvVariable) {
78
            $envFilePath = '.' . $sharedEnvVariable->getContainerId() . $this->context->getEnvironmentName() . '.env';
79
            $envFilePaths[] = $envFilePath;
80
            $dotEnvFile = new EnvFileHelper($this->context->getProjectDir() . '/' . $envFilePath);
81
            $dotEnvFile->set($key, $sharedEnvVariable->getValue(), $sharedEnvVariable->getComment());
82
        }
83
        $envFilePaths = array_unique($envFilePaths);
84
        return DockerComposeHelper::dockerComposeServiceSerialize($service, $envFilePaths);
85
    }
86
}
87