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

DeployDockerComposeJob::newDeployOnRemoteServer()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 37
rs 9.472
c 0
b 0
f 0
cc 3
nc 4
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
4
namespace TheAentMachine\AentGitLabCI\GitLabCI\Job;
5
6
use TheAentMachine\AentGitLabCI\Exception\JobException;
7
use TheAentMachine\AentGitLabCI\GitLabCI\Job\Model\BranchesModel;
8
9
final class DeployDockerComposeJob extends AbstractDeployJob
10
{
11
12
    /**
13
     * @param string $identifier
14
     * @param string $registryDomainName
15
     * @param string $dockerComposeFilename
16
     * @param string $remoteIP
17
     * @param string $remoteUser
18
     * @param string $remoteBasePath
19
     * @param BranchesModel $branchesModel
20
     * @param bool $isManual
21
     * @return DeployDockerComposeJob
22
     * @throws JobException
23
     */
24
    public static function newDeployOnRemoteServer(string $identifier, string $registryDomainName, string $dockerComposeFilename, string $remoteIP, string $remoteUser, string $remoteBasePath, BranchesModel $branchesModel, bool $isManual): self
25
    {
26
        $self = new self($identifier);
27
28
        $self->image = 'kroniak/ssh-client:3.6';
29
        $self->variables = [
30
            'SSH_KNOWN_HOSTS' => 'You should put this value in your secrets CI variables!',
31
            'SSH_PRIVATE_KEY' => 'You should put this value in your secrets CI variables!',
32
            'DOCKER_COMPOSE_FILENAME' => $dockerComposeFilename,
33
            'REGISTRY_DOMAIN_NAME' => $registryDomainName,
34
            'REMOTE_IP' => $remoteIP,
35
            'REMOTE_USER' => $remoteUser,
36
            'REMOTE_BASE_PATH' => $remoteBasePath,
37
        ];
38
        $self->script = [
39
            'sed -e "s/#ENVIRONMENT#/${CI_COMMIT_REF_SLUG}/g" ${DOCKER_COMPOSE_FILENAME} > ${DOCKER_COMPOSE_FILENAME}',
40
            'mkdir ~/.ssh',
41
            'echo "${SSH_KNOWN_HOSTS}" >> ~/.ssh/known_hosts',
42
            'chmod 644 ~/.ssh/known_hosts',
43
            'eval $(ssh-agent -s)',
44
            'ssh-add <(echo "${SSH_PRIVATE_KEY}"',
45
            'ssh ${REMOTE_USER}@${REMOTE_IP} "docker login -u ${CI_DEPLOY_USER} -p ${CI_DEPLOY_PASSWORD} ${REGISTRY_DOMAIN_NAME}"',
46
            'ssh ${REMOTE_USER}@${REMOTE_IP} "mkdir ${REMOTE_BASE_PATH}" || true',
47
            'ssh ${REMOTE_USER}@${REMOTE_IP} "cd ${REMOTE_BASE_PATH} && docker-compose down --rmi=all" || true',
48
            'scp ${DOCKER_COMPOSE_FILENAME} ${REMOTE_USER}@${REMOTE_IP}:${REMOTE_BASE_PATH}/docker-compose.yml',
49
            'ssh ${REMOTE_USER}@${REMOTE_IP} "cd ${REMOTE_BASE_PATH} && docker-compose up -d"'
50
        ];
51
52
        foreach ($branchesModel->getBranches() as $branch) {
53
            $self->addOnly($branch);
54
        }
55
        foreach ($branchesModel->getBranchesToIgnore() as $branch) {
56
            $self->addExcept($branch);
57
        }
58
        $self->manual = $isManual;
59
60
        return $self;
61
    }
62
}
63