Completed
Pull Request — master (#1)
by Jindun
06:14
created

DeployDockerComposeJob::newDeployOnRemoteServer()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 36
rs 9.488
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
            'mkdir ~/.ssh',
40
            'echo "${SSH_KNOWN_HOSTS}" >> ~/.ssh/known_hosts',
41
            'chmod 644 ~/.ssh/known_hosts',
42
            'eval $(ssh-agent -s)',
43
            'ssh-add <(echo "${SSH_PRIVATE_KEY}"',
44
            'ssh ${REMOTE_USER}@${REMOTE_IP} "docker login -u ${CI_DEPLOY_USER} -p ${CI_DEPLOY_PASSWORD} ${REGISTRY_DOMAIN_NAME}"',
45
            'ssh ${REMOTE_USER}@${REMOTE_IP} "mkdir ${REMOTE_BASE_PATH}" || true',
46
            'ssh ${REMOTE_USER}@${REMOTE_IP} "cd ${REMOTE_BASE_PATH} && docker-compose down --rmi=all" || true',
47
            'scp ${DOCKER_COMPOSE_FILENAME} ${REMOTE_USER}@${REMOTE_IP}:${REMOTE_BASE_PATH}/docker-compose.yml',
48
            'ssh ${REMOTE_USER}@${REMOTE_IP} "cd ${REMOTE_BASE_PATH} && docker-compose up -d"'
49
        ];
50
51
        foreach ($branchesModel->getBranches() as $branch) {
52
            $self->addOnly($branch);
53
        }
54
        foreach ($branchesModel->getBranchesToIgnore() as $branch) {
55
            $self->addExcept($branch);
56
        }
57
        $self->manual = $isManual;
58
59
        return $self;
60
    }
61
}
62