Completed
Push — 1.0 ( 5bcb16...e7f44c )
by David
02:12
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 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...
31
    {
32
        $gitlabApiToken = $this->input->getOption('gitlab-api-token');
33
        if ($gitlabApiToken === null) {
34
            $gitlabApiToken = getenv('GITLAB_API_TOKEN');
35
            if ($gitlabApiToken === false) {
36
                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.');
37
            }
38
        }
39
        return $gitlabApiToken;
40
    }
41
42
    public function getGitlabUrl() : string
43
    {
44
        $gitlabUrl = $this->input->getOption('gitlab-url');
45
        if ($gitlabUrl === null) {
46
            $ciProjectUrl = getenv('CI_BUILD_REPO');
47
            if ($ciProjectUrl === false) {
48
                throw new \RuntimeException('Could not find the Gitlab URL in the "CI_BUILD_REPO" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the URL via the --gitlab-url command line option.');
49
            }
50
            $parsed_url = parse_url($ciProjectUrl);
51
            $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
52
            $host     = isset($parsed_url['host']) ? $parsed_url['host'] : '';
53
            $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
54
            $gitlabUrl = $scheme.$host.$port;
55
        }
56
        return rtrim($gitlabUrl, '/');
57
    }
58
59
    public function getGitlabApiUrl() : string
60
    {
61
        return $this->getGitlabUrl().'/api/v3/';
62
    }
63
64
    public function getGitlabProjectName() : string
65
    {
66
        $projectName = $this->input->getOption('gitlab-project-name');
67
        if ($projectName === null) {
68
            $projectDir = getenv('CI_PROJECT_DIR');
69
            if ($projectDir === false) {
70
                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.');
71
            }
72
            $projectName = substr($projectDir, 8);
73
        }
74
        return $projectName;
75
    }
76
77 View Code Duplication
    public function getGitlabBuildRef() : 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...
78
    {
79
        $buildRef = $this->input->getOption('gitlab-build-ref');
80
        if ($buildRef === null) {
81
            $buildRef = getenv('CI_BUILD_REF');
82
            if ($buildRef === false) {
83
                throw new \RuntimeException('Could not find the Gitlab build reference in the "CI_BUILD_REF" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the build reference via the --gitlab-build-ref command line option.');
84
            }
85
        }
86
        return $buildRef;
87
    }
88
89
    /**
90
     * Returns the current branch name (from Git)
91
     * @return string
92
     */
93
    public function getCurrentBranchName() : string
94
    {
95
        $repo = new GitRepository(getcwd());
96
        return $repo->getCurrentBranchName();
97
    }
98
}
99