GitLabRepoClient::getServerType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ConfigToken\FileClient\Types;
4
5
6
use ConfigToken\ConnectionSettings\Types\GitLabRepoConnectionSettings;
7
use ConfigToken\FileClient\Exception\FileClientConnectionException;
8
use ConfigToken\FileUtils;
9
10
class GitLabRepoClient extends AbstractFileClient
11
{
12
    /**
13
     * Get the file server type identifier corresponding to the client's implementation.
14
     *
15
     * @return string
16
     */
17
    public static function getServerType()
18
    {
19
        return 'gitlab';
20
    }
21
22
    /**
23
     * Return the file contents.
24
     *
25
     * @param string $fileName
26
     * @throws \Exception
27
     * @return array(string, string) [$contentType, $content]
0 ignored issues
show
Documentation introduced by
The doc-type array(string, could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
28
     */
29
    public function readFile($fileName)
30
    {
31
        $this->validateConnectionSettings();
32
33
        /** @var GitLabRepoConnectionSettings $connectionSettings */
34
        $connectionSettings = $this->getConnectionSettings();
35
        $remote = sprintf(
36
            'git@%s:%s/%s.git',
37
            $connectionSettings->getHostName(),
38
            $connectionSettings->getGroupName(),
39
            $connectionSettings->getRepoName()
40
        );
41
        $url = sprintf(
42
            "%s/projects/%s/repository/blobs/%s?private_token=%s&filepath=%s",
43
            $connectionSettings->getUrl(),
44
            str_replace('.', '%2E', urlencode($remote)),
45
            $connectionSettings->getNamedReference(),
46
            $connectionSettings->getApiToken(),
47
            $fileName
48
        );
49
50
        $ch = curl_init();
51
        curl_setopt($ch, CURLOPT_URL, $url);
52
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
53
        curl_setopt($ch, CURLOPT_HEADER, "application/json");
54
55
        $content = curl_exec($ch);
56
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
57
58
        curl_close($ch);
59
60
        if ($httpCode != 200) {
61
            throw new FileClientConnectionException(
62
                sprintf(
63
                    'Unable to fetch file "%s" from %s (%s) via GitLab API %s. Got response code %s.',
64
                    $fileName,
65
                    $remote,
66
                    $connectionSettings->getNamedReference(),
67
                    $connectionSettings->getUrl(),
68
                    $httpCode
69
                )
70
            );
71
        }
72
73
        $contentType = FileUtils::getContentTypeFromFileName($fileName);
74
        return array($contentType, $content);
75
    }
76
77
}