Completed
Push — master ( 7c4652...919922 )
by Jindun
10s
created

AddEventCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 39
dl 0
loc 65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEventName() 0 3 1
A executeEvent() 0 51 4
1
<?php
2
3
namespace TheAentMachine\AentDockerCompose\Command;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use TheAentMachine\Aenthill\Aenthill;
7
use TheAentMachine\Aenthill\CommonEvents;
8
use TheAentMachine\Aenthill\CommonMetadata;
9
use TheAentMachine\Aenthill\Manifest;
10
use TheAentMachine\Aenthill\Pheromone;
11
use TheAentMachine\Command\AbstractEventCommand;
12
13
class AddEventCommand extends AbstractEventCommand
14
{
15
    protected function getEventName(): string
16
    {
17
        return CommonEvents::ADD_EVENT;
18
    }
19
20
    /**
21
     * @param null|string $payload
22
     * @return null|string
23
     * @throws \TheAentMachine\Exception\CommonAentsException
24
     * @throws \TheAentMachine\Exception\ManifestException
25
     * @throws \TheAentMachine\Exception\MissingEnvironmentVariableException
26
     */
27
    protected function executeEvent(?string $payload): ?string
28
    {
29
        $aentHelper = $this->getAentHelper();
30
        $aentHelper->title('Installing a Docker Compose orchestrator');
31
        $envType = $aentHelper->getCommonQuestions()->askForEnvType();
32
        $envName = $aentHelper->getCommonQuestions()->askForEnvName($envType);
33
34
        $projectDir = Pheromone::getContainerProjectDirectory();
35
        $fileNameChoices = [];
36
        if (!file_exists("$projectDir/docker-compose.yml")) {
37
            $fileNameChoices[] = 'docker-compose.yml';
38
        }
39
40
        $i = 0;
41
        $tmpFileName = "docker-compose.$envName.yml";
42
        while (file_exists("$projectDir/$tmpFileName")) {
43
            $i++;
44
            $tmpFileName = "docker-compose.$envName$i.yml";
45
        }
46
        $fileNameChoices[] = $tmpFileName;
47
48
        $fileName = $aentHelper->choiceQuestion('Select your docker-compose file name', $fileNameChoices)
49
            ->setDefault('0')
50
            ->ask();
51
52
        $versionChoices = ['3.7', '3.6', '3.5', '3.4', '3.3', '3.2'];
53
        $version = $aentHelper->choiceQuestion('Select your docker-compose version', $versionChoices)
54
            ->setDefault('3.3')
55
            ->ask();
56
57
        $fileSystem = new Filesystem();
58
        $dockerComposePath = "$projectDir/$fileName";
59
        $fileSystem->dumpFile($dockerComposePath, "version: '$version'");
60
        $dirInfo = new \SplFileInfo(\dirname($dockerComposePath));
61
        chown($dockerComposePath, $dirInfo->getOwner());
62
        chgrp($dockerComposePath, $dirInfo->getGroup());
63
        Manifest::addMetadata(CommonMetadata::DOCKER_COMPOSE_FILENAME_KEY, $fileName);
64
65
        $CIAentID = $aentHelper->getCommonQuestions()->askForCI();
66
        if (null !== $CIAentID) {
67
            Aenthill::run($CIAentID, CommonEvents::ADD_EVENT, '1'); // 1 for single environment (single branch)
68
            Aenthill::run($CIAentID, CommonEvents::NEW_DEPLOY_DOCKER_COMPOSE_JOB_EVENT, $fileName);
69
            $aentHelper->spacer();
70
        }
71
72
        $aentHelper->getCommonQuestions()->askForImageBuilder();
73
74
        $this->output->writeln("Docker Compose file <info>$fileName</info> has been successfully created!");
75
        $aentHelper->spacer();
76
77
        return null;
78
    }
79
}
80