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

AddEventCommand::executeEvent()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 51
rs 9.328
c 0
b 0
f 0
cc 4
nc 8
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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