Completed
Push — 2.0 ( 4e07f8...e8af7e )
by David
14s
created

Config::getGitlabApiUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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 getGitlabJobId() : 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 View Code Duplication
    public function getGitlabBuildName() : 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...
123
    {
124
        $buildName = $this->input->getOption('gitlab-build-name');
125
        if ($buildName === null) {
126
            $buildName = getenv('CI_BUILD_NAME');
127
            if ($buildName === false) {
128
                throw new \RuntimeException('Could not find the Gitlab build name in the "CI_BUILD_NAME" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the build id via the --gitlab-build-name command line option.');
129
            }
130
        }
131
        return $buildName;
132
    }
133
134
    /**
135
     * Returns the current branch name (from Git)
136
     * @return string
137
     */
138
    public function getCurrentBranchName() : string
139
    {
140
        // Gitlab 9+
141
        $branchName = getenv('CI_COMMIT_REF_NAME');
142
        if ($branchName !== false) {
143
            return $branchName;
144
        }
145
146
        $repo = new GitRepository(getcwd());
147
        return $repo->getCurrentBranchName();
148
    }
149
150
    public function getFiles() : array
151
    {
152
        return $this->input->getOption('file');
153
    }
154
155
    public function isOpenIssue() : bool
156
    {
157
        return $this->input->getOption('open-issue');
158
    }
159
160
    public function isAddCommentsInCommits() : bool
161
    {
162
        return $this->input->getOption('add-comments-in-commits');
163
    }
164
}
165