Completed
Push — 2.0 ( fe274c...2b8fbf )
by David
02:16
created

Config::getGitlabBuildId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\WashingMachine\Commands;
5
6
7
use Cz\Git\GitRepository;
8
use Symfony\Component\Console\Input\InputInterface;
9
10
/**
11
 * Object to retrieve the command configuration based on environment variables and input.
12
 */
13
class Config
14
{
15
    /**
16
     * @var InputInterface
17
     */
18
    private $input;
19
20
    public function __construct(InputInterface $input)
21
    {
22
        $this->input = $input;
23
    }
24
25
    public function getCloverFilePath() : string
26
    {
27
        return $this->input->getOption('clover');
28
    }
29
30
    public function getCrap4JFilePath() : string
31
    {
32
        return $this->input->getOption('crap4j');
33
    }
34
35 View Code Duplication
    public function getGitlabApiToken() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        $gitlabApiToken = $this->input->getOption('gitlab-api-token');
38
        if ($gitlabApiToken === null) {
39
            $gitlabApiToken = getenv('GITLAB_API_TOKEN');
40
            if ($gitlabApiToken === false) {
41
                throw new \RuntimeException('Could not find the Gitlab API token in the "GITLAB_API_TOKEN" environment variable. Either set this environment variable or pass the token via the --gitlab-api-token command line option.');
42
            }
43
        }
44
        return $gitlabApiToken;
45
    }
46
47
    public function getGitlabUrl() : string
48
    {
49
        $gitlabUrl = $this->input->getOption('gitlab-url');
50
        if ($gitlabUrl === null) {
51
            $ciProjectUrl = getenv('CI_REPOSITORY_URL');
52
            if ($ciProjectUrl === false) {
53
                throw new \RuntimeException('Could not find the Gitlab URL in the "CI_REPOSITORY_URL" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the URL via the --gitlab-url command line option.');
54
            }
55
            $parsed_url = parse_url($ciProjectUrl);
56
            $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
57
            $host     = isset($parsed_url['host']) ? $parsed_url['host'] : '';
58
            $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
59
            $gitlabUrl = $scheme.$host.$port;
60
        }
61
        return rtrim($gitlabUrl, '/');
62
    }
63
64
    public function getGitlabApiUrl() : string
65
    {
66
        return $this->getGitlabUrl().'/api/v3/';
67
    }
68
69
    public function getGitlabProjectName() : string
70
    {
71
        $projectName = $this->input->getOption('gitlab-project-name');
72
        if ($projectName === null) {
73
            $projectDir = getenv('CI_PROJECT_DIR');
74
            if ($projectDir === false) {
75
                throw new \RuntimeException('Could not find the Gitlab project name in the "CI_PROJECT_DIR" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the project name via the --gitlab-project-name command line option.');
76
            }
77
            $projectName = substr($projectDir, 8);
78
        }
79
        return $projectName;
80
    }
81
82 View Code Duplication
    public function getCommitSha() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $commitSha = $this->input->getOption('commit-sha');
85
86
        if ($commitSha === null) {
87
            $commitSha = getenv('CI_COMMIT_SHA');
88
            if ($commitSha === false) {
89
                throw new \RuntimeException('Could not find the Gitlab build reference in the "CI_COMMIT_SHA" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the build reference via the --commit-sha command line option.');
90
            }
91
        }
92
93
        return $commitSha;
94
    }
95
96 View Code Duplication
    public function getJobStage() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $commitSha = $this->input->getOption('job-stage');
99
100
        if ($commitSha === null) {
101
            $commitSha = getenv('CI_JOB_STAGE');
102
            if ($commitSha === false) {
103
                throw new \RuntimeException('Could not find the Gitlab job stage in the "CI_JOB_STAGE" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the job stage via the --job_stage command line option.');
104
            }
105
        }
106
107
        return $commitSha;
108
    }
109
110 View Code Duplication
    public function getGitlabBuildId() : int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $buildId = $this->input->getOption('gitlab-job-id');
113
        if ($buildId === null) {
114
            $buildId = getenv('CI_JOB_ID');
115
            if ($buildId === false) {
116
                throw new \RuntimeException('Could not find the Gitlab build id in the "CI_JOB_ID" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the build id via the --gitlab-job-id command line option.');
117
            }
118
        }
119
        return $buildId;
120
    }
121
122
    /**
123
     * Returns the current branch name (from Git)
124
     * @return string
125
     */
126
    public function getCurrentBranchName() : string
127
    {
128
        // Gitlab 9+
129
        $branchName = getenv('CI_COMMIT_REF_NAME');
130
        if ($branchName !== false) {
131
            return $branchName;
132
        }
133
134
        $repo = new GitRepository(getcwd());
135
        return $repo->getCurrentBranchName();
136
    }
137
138
    public function getFiles() : array
139
    {
140
        return $this->input->getOption('file');
141
    }
142
143
    public function isOpenIssue() : bool
144
    {
145
        return $this->input->getOption('open-issue');
146
    }
147
148
    public function isAddCommentsInCommits() : bool
149
    {
150
        return $this->input->getOption('add-comments-in-commits');
151
    }
152
}
153