GerritTrait::remoteCall()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * T3Bot.
4
 *
5
 * @author Frank Nägler <[email protected]>
6
 *
7
 * @link http://www.t3bot.de
8
 * @link http://wiki.typo3.org/T3Bot
9
 */
10
namespace T3Bot\Traits;
11
12
trait GerritTrait
13
{
14
    /**
15
     * @param string $query
16
     *
17
     * @return \stdClass|array|bool
18
     */
19 28
    protected function queryGerrit($query)
20
    {
21 28
        return $this->remoteCall('https://review.typo3.org/changes/?q=' . urlencode($query));
22
    }
23
24
    /**
25
     * @param int $changeId
26
     * @param int $revision
27
     *
28
     * @return mixed|string
29
     */
30 5
    protected function getFilesForPatch($changeId, $revision)
31
    {
32 5
        return $this->remoteCall(
33 5
            'https://review.typo3.org/changes/' . $changeId . '/revisions/' . $revision . '/files'
34
        );
35
    }
36
37
    /**
38
     * Resolve patch ID from URL
39
     * @param string $url
40
     * @return string
41
     */
42 8
    protected function resolvePatchIdFromUrl($url): string {
43 8
        $re = '@https://review\.typo3\.org/c/Packages/TYPO3\.CMS/\+/([0-9]*)@m';
44 8
        preg_match_all($re, $url, $matches, PREG_SET_ORDER, 0);
45 8
        return $matches[0][1];
46
    }
47
48
    /**
49
     * @param string $url
50
     *
51
     * @return bool|mixed
52
     */
53 28
    protected function remoteCall($url)
54
    {
55 28
        $ch = curl_init();
56 28
        $timeout = 5;
57 28
        curl_setopt($ch, CURLOPT_URL, $url);
58 28
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
59 28
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
60 28
        $data = curl_exec($ch);
61
62 28
        $result = false;
63 28
        if (!curl_errno($ch)) {
64 28
            curl_close($ch);
65 28
            $result = json_decode(str_replace(")]}'" . chr(10), '', $data));
66
        }
67
68 28
        return $result;
69
    }
70
}
71