Completed
Push — master ( 4fe608...5ceeaf )
by Jindun
12s
created

createDeployOnRemoteServerJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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