Completed
Pull Request — master (#1)
by Jindun
04:17
created

NewDeployDockerComposeJobEventCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 47
dl 0
loc 103
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A askForDeployType() 0 17 3
A createDeployOnRemoteServerJob() 0 21 1
A executeEvent() 0 27 2
A getEventName() 0 3 1
1
<?php
2
3
namespace TheAentMachine\AentGitLabCI\Command;
4
5
use TheAentMachine\AentGitLabCI\Aenthill\Metadata;
6
use TheAentMachine\AentGitLabCI\Exception\GitLabCIFileException;
7
use TheAentMachine\AentGitLabCI\Exception\JobException;
8
use TheAentMachine\AentGitLabCI\Exception\PayloadException;
9
use TheAentMachine\AentGitLabCI\GitLabCI\GitLabCIFile;
10
use TheAentMachine\AentGitLabCI\GitLabCI\Job\DeployDockerComposeJob;
11
use TheAentMachine\AentGitLabCI\Question\GitLabCICommonQuestions;
12
use TheAentMachine\Aenthill\CommonEvents;
13
use TheAentMachine\Aenthill\CommonMetadata;
14
use TheAentMachine\Aenthill\Manifest;
15
use TheAentMachine\Command\AbstractEventCommand;
16
use TheAentMachine\Exception\ManifestException;
17
use TheAentMachine\Exception\MissingEnvironmentVariableException;
18
19
final class NewDeployDockerComposeJobEventCommand extends AbstractEventCommand
20
{
21
    /** @var string */
22
    private $envName;
23
24
    /** @var string */
25
    private $registryDomainName;
26
27
    /** @var string */
28
    private $dockerComposeFilename;
29
30
    protected function getEventName(): string
31
    {
32
        return CommonEvents::NEW_DEPLOY_DOCKER_COMPOSE_JOB_EVENT;
33
    }
34
35
    /**
36
     * @param null|string $payload
37
     * @return null|string
38
     * @throws GitLabCIFileException
39
     * @throws JobException
40
     * @throws ManifestException
41
     * @throws MissingEnvironmentVariableException
42
     * @throws PayloadException
43
     */
44
    protected function executeEvent(?string $payload): ?string
45
    {
46
        $aentHelper = $this->getAentHelper();
47
48
        $aentHelper->title('GitLab CI: adding a deploy stage');
49
50
        if (empty($payload)) {
51
            throw PayloadException::missingDockerComposeFilename();
52
        }
53
54
55
        $this->envName = Manifest::mustGetMetadata(CommonMetadata::ENV_NAME_KEY);
56
        $this->registryDomainName = Manifest::mustGetMetadata(Metadata::REGISTRY_DOMAIN_NAME_KEY);
57
        $this->dockerComposeFilename = $payload;
58
59
        $this->output->writeln("🦊 Docker Compose file: <info>$this->dockerComposeFilename</info>");
60
        $aentHelper->spacer();
61
62
        $job = $this->askForDeployType();
63
64
        $file = new GitLabCIFile();
65
        $file->findOrCreate();
66
        $file->addDeploy($job);
67
68
        $this->output->writeln('🦊 <info>' . GitLabCIFile::DEFAULT_FILENAME . '</info> has been successfully updated!');
69
70
        return null;
71
    }
72
73
    /**
74
     * @return DeployDockerComposeJob
75
     * @throws JobException
76
     */
77
    private function askForDeployType(): DeployDockerComposeJob
78
    {
79
        $deployType = Manifest::getMetadata(Metadata::DEPLOY_TYPE_KEY);
80
81
        if (null === $deployType) {
82
            $deployType = $this->getAentHelper()
83
                ->choiceQuestion('Select on which provider you want to deploy your stack', [
84
                    Metadata::DEPLOY_TYPE_REMOTE_SERVER
85
                ])
86
                ->ask();
87
        }
88
89
        switch ($deployType) {
90
            case Metadata::DEPLOY_TYPE_REMOTE_SERVER:
91
                return $this->createDeployOnRemoteServerJob();
92
            default:
93
                throw JobException::unknownDeployType($deployType);
94
        }
95
    }
96
97
    /**
98
     * @return DeployDockerComposeJob
99
     * @throws JobException
100
     */
101
    private function createDeployOnRemoteServerJob(): DeployDockerComposeJob
102
    {
103
        Manifest::addMetadata(Metadata::DEPLOY_TYPE_KEY, Metadata::DEPLOY_TYPE_REMOTE_SERVER);
104
105
        $gitlabCICommonQuestions = new GitLabCICommonQuestions($this->getAentHelper());
106
107
        $remoteIP = $gitlabCICommonQuestions->askForRemoteIP();
108
        $remoteUser = $gitlabCICommonQuestions->askForRemoteUser();
109
        $remoteBasePath = $gitlabCICommonQuestions->askForRemoteBasePath();
110
        $branches = $gitlabCICommonQuestions->askForBranches(false);
111
        $isManual = $gitlabCICommonQuestions->askForManual();
112
113
        return DeployDockerComposeJob::newDeployOnRemoteServer(
114
            $this->envName,
115
            $this->registryDomainName,
116
            $this->dockerComposeFilename,
117
            $remoteIP,
118
            $remoteUser,
119
            $remoteBasePath,
120
            $branches,
121
            $isManual
122
        );
123
    }
124
}
125