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

NewDeployKubernetesJobEventCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 57
dl 0
loc 124
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getEventName() 0 3 1
A createDeployOnGCloud() 0 15 1
A executeEvent() 0 28 2
A createCleanupOnGCloud() 0 16 1
A askForDeployType() 0 17 3
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\CleanupKubernetesJob;
11
use TheAentMachine\AentGitLabCI\GitLabCI\Job\DeployKubernetesJob;
12
use TheAentMachine\AentGitLabCI\GitLabCI\Job\Model\BranchesModel;
13
use TheAentMachine\AentGitLabCI\Question\GitLabCICommonQuestions;
14
use TheAentMachine\Aenthill\CommonEvents;
15
use TheAentMachine\Aenthill\CommonMetadata;
16
use TheAentMachine\Aenthill\Manifest;
17
use TheAentMachine\Command\AbstractEventCommand;
18
use TheAentMachine\Exception\ManifestException;
19
use TheAentMachine\Exception\MissingEnvironmentVariableException;
20
21
final class NewDeployKubernetesJobEventCommand extends AbstractEventCommand
22
{
23
    /** @var string */
24
    private $envName;
25
26
    /** @var string */
27
    private $registryDomainName;
28
29
    /** @var string */
30
    private $k8sDirname;
31
32
    protected function getEventName(): string
33
    {
34
        return CommonEvents::NEW_DEPLOY_KUBERNETES_JOB_EVENT;
35
    }
36
37
    /**
38
     * @param null|string $payload
39
     * @return null|string
40
     * @throws GitLabCIFileException
41
     * @throws JobException
42
     * @throws ManifestException
43
     * @throws MissingEnvironmentVariableException
44
     * @throws PayloadException
45
     */
46
    protected function executeEvent(?string $payload): ?string
47
    {
48
        $aentHelper = $this->getAentHelper();
49
50
        $aentHelper->title('GitLab CI: adding a deploy stage');
51
52
        if (null === $payload) {
53
            throw PayloadException::missingKubernetesPathname();
54
        }
55
56
        $this->envName = Manifest::mustGetMetadata(CommonMetadata::ENV_NAME_KEY);
57
        $this->registryDomainName = Manifest::mustGetMetadata(Metadata::REGISTRY_DOMAIN_NAME_KEY);
58
        $this->k8sDirname = $payload;
59
60
        $this->output->writeln("🦊×☸️ Kubernetes folder: <info>$this->k8sDirname</info>");
61
        $aentHelper->spacer();
62
63
        $deployJob = $this->askForDeployType();
64
        $cleanUpJob = $this->createCleanupOnGCloud();
65
66
        $file = new GitLabCIFile();
67
        $file->findOrCreate();
68
        $file->addDeploy($deployJob);
69
        $file->addCleanUp($cleanUpJob);
70
71
        $this->output->writeln('🦊 <info>' . GitLabCIFile::DEFAULT_FILENAME . '</info> has been successfully updated!');
72
73
        return null;
74
    }
75
76
    /**
77
     * @return DeployKubernetesJob
78
     * @throws JobException
79
     * @throws ManifestException
80
     */
81
    private function askForDeployType(): DeployKubernetesJob
82
    {
83
        $deployType = Manifest::getMetadata(Metadata::DEPLOY_TYPE_KEY);
84
85
        if (null === $deployType) {
86
            $deployType = $this->getAentHelper()
87
                ->choiceQuestion('Select on which provider you want to deploy your stack', [
88
                    Metadata::DEPLOY_TYPE_GCLOUD
89
                ])
90
                ->ask();
91
        }
92
93
        switch ($deployType) {
94
            case Metadata::DEPLOY_TYPE_GCLOUD:
95
                return $this->createDeployOnGCloud();
96
            default:
97
                throw JobException::unknownDeployType($deployType);
98
        }
99
    }
100
101
    /**
102
     * @return DeployKubernetesJob
103
     * @throws JobException
104
     * @throws ManifestException
105
     */
106
    private function createDeployOnGCloud(): DeployKubernetesJob
107
    {
108
        Manifest::addMetadata(Metadata::DEPLOY_TYPE_KEY, Metadata::DEPLOY_TYPE_GCLOUD);
109
110
        $gitlabCICommonQuestions = new GitLabCICommonQuestions($this->getAentHelper());
111
        $remoteBasePath = $gitlabCICommonQuestions->askForRemoteBasePath();
112
        $kubernetesDirPath = $remoteBasePath . '/' . $this->k8sDirname;
113
        $branchesModel = BranchesModel::newFromMetadata();
114
        $isManual = $gitlabCICommonQuestions->askForManual();
115
116
        return DeployKubernetesJob::newDeployOnGCloud(
117
            $this->envName,
118
            $kubernetesDirPath,
119
            $branchesModel,
120
            $isManual
121
        );
122
    }
123
124
    /**
125
     * @return CleanupKubernetesJob
126
     * @throws JobException
127
     * @throws ManifestException
128
     */
129
    private function createCleanupOnGCloud(): CleanupKubernetesJob
130
    {
131
        $gitlabCICommonQuestions = new GitLabCICommonQuestions($this->getAentHelper());
132
        $registryDomainName = Manifest::mustGetMetadata(Metadata::REGISTRY_DOMAIN_NAME_KEY);
133
        $projectGroup = Manifest::mustGetMetadata(Metadata::PROJECT_GROUP_KEY);
134
        $projectName = Manifest::mustGetMetadata(Metadata::PROJECT_NAME_KEY);
135
        $branchesModel = BranchesModel::newFromMetadata();
136
        $isManual = $gitlabCICommonQuestions->askForManual();
137
138
        return CleanupKubernetesJob::newCleanup(
139
            $this->envName,
140
            $registryDomainName,
141
            $projectGroup,
142
            $projectName,
143
            $branchesModel,
144
            $isManual
145
        );
146
    }
147
}
148