GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

HTTPDataService   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 13.56%

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 225
ccs 8
cts 59
cp 0.1356
rs 10
c 3
b 2
f 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getBaseUrl() 0 17 4
A verifyResult() 0 8 2
A transformJsonResponse() 0 11 2
A initQueryLimit() 0 10 1
A getProjects() 0 19 1
B getChangesets() 0 33 1
A buildQueryStringWithSameParameterName() 0 14 3
A getVersion() 0 10 1
1
<?php
2
/**
3
 * This file is part of the Gerrie package.
4
 *
5
 * (c) Andreas Grunwald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Gerrie\API\DataService;
12
13
class HTTPDataService extends BaseDataService
14
{
15
16
    /**
17
     * Constructor
18
     *
19
     * @param \Buzz\Browser $connector
20
     * @param array $config
21
     * @return \Gerrie\API\DataService\HTTPDataService
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
22
     */
23 9
    public function __construct(\Buzz\Browser $connector, array $config)
24
    {
25 9
        $this->setConnector($connector);
26 9
        $this->setConfig($config);
27 9
        $this->setName('HTTP');
28 9
    }
29
30
    /**
31
     * Gets the base url for all HTTP requests.
32
     *
33
     * @param bool $withAuthentication If true, the authentification string will be appended. False otherwise
34
     * @return string
35
     */
36
    protected function getBaseUrl($withAuthentication = false)
37
    {
38
        $config = $this->getConfig();
39
        $baseUrl = $config['scheme'] . '://' . rtrim($config['host'], '/') . '/';
40
41
        if (isset($config['path'])) {
42
            $baseUrl .= trim($config['path'], '/') . '/';
43
        }
44
45
        if ($withAuthentication === true
46
            && $this->getConnector()->getListener() instanceof \Buzz\Listener\BasicAuthListener) {
47
48
            $baseUrl .= 'a/';
49
        }
50
51
        return $baseUrl;
52
    }
53
54
    /**
55
     * Verifies the last request.
56
     * If the last request was not successful, it will be throw an exception.
57
     *
58
     * @param \Buzz\Message\Response $response The response object from the last reques
59
     * @param string $url The url which was requested
60
     * @return \Buzz\Message\Response
61
     * @throws \Exception
62
     */
63
    protected function verifyResult(\Buzz\Message\Response $response, $url)
64
    {
65
        if ($response->getStatusCode() !== 200) {
66
            throw new \Exception('Request to "' . $url . '" failed', 1364061673);
67
        }
68
69
        return $response;
70
    }
71
72
    /**
73
     * Transforms a JSON string into an array.
74
     * Regular, the json is the content from the response.
75
     *
76
     * @param string $json The json string
77
     * @return array|null
78
     */
79 2
    public function transformJsonResponse($json)
80
    {
81
        // In a REST-API call, the first five chars are )]}'\n
82
        // to decode it, we have to strip it
83
        // See https://review.typo3.org/Documentation/rest-api.html#output
84 2
        if (substr($json, 0, 4) === ')]}\'') {
85
            $json = substr($json, 5);
86
        }
87
88 2
        return json_decode($json, true);
89
    }
90
91
    /**
92
     * Initiales the query limit
93
     *
94
     * @return int
95
     */
96
    protected function initQueryLimit()
97
    {
98
        $url = $this->getBaseUrl(true) . 'accounts/self/capabilities?format=JSON';
99
        $response = $this->getConnector()->get($url);
100
        $response = $this->verifyResult($response, $url);
101
102
        $content = $this->transformJsonResponse($response->getContent());
103
104
        return $content['queryLimit']['max'];
105
    }
106
107
    /**
108
     * Requests projects at the Gerrit server
109
     *
110
     * @return array|null
111
     */
112
    public function getProjects()
113
    {
114
        $urlParts = array(
115
            'format' => 'JSON',
116
            'description' => '',
117
            'type' => 'all',
118
            'all' => '',
119
            'tree' => '',
120
        );
121
122
        $url = $this->getBaseUrl() . 'projects/?' . http_build_query($urlParts);
123
124
        $response = $this->getConnector()->get($url);
125
        $response = $this->verifyResult($response, $url);
126
127
        $content = $this->transformJsonResponse($response->getContent());
128
129
        return $content;
130
    }
131
132
    /**
133
     * Requests changesets at the Gerrit server.
134
     *
135
     * This method is not implemented yet, because at the moment (2013-03-24) Gerrit 2.6.* is not released.
136
     * Many Gerrit systems (e.g. TYPO3, WikiMedia, OpenStack, etc.) are running at 2.5.*.
137
     * In 2.5.* the SSH API delivers more information than the REST API.
138
     *
139
     * If Gerrit 2.6 is released, the HTTP DataService will be extended and fully implemented.
140
     * Maybe, you want to help me?
141
     *
142
     * SSH commands:
143
     * /usr/bin/ssh -p 29418 review.typo3.org gerrit query --format 'JSON' --current-patch-set
144
     *                                                     --all-approvals --files --comments
145
     *                                                     --commit-message --dependencies --submit-records
146
     *                                                     'project:Documentation/ApiTypo3Org' limit:'500' 2>&1
147
     * /usr/bin/ssh -p 29418 review.typo3.org gerrit query --format 'JSON' --current-patch-set
148
     *                                                     --all-approvals --files --comments
149
     *                                                     --commit-message --dependencies --submit-records
150
     *                                                     'project:Documentation/ApiTypo3Org' limit:'500'
151
     *                                                     resume_sortkey:'00215ec7000041b3' 2>&1
152
     *
153
     * @param string $projectName The project name
154
     * @param string $resumeKey The key where the request will be resumed
155
     * @param integer $start Number of changesets which will be skipped
156
     * @return array|null
157
     * @throws \Exception
158
     */
159
    public function getChangesets($projectName, $resumeKey = null, $start = 0)
160
    {
161
        throw new \Exception(__METHOD__ . ' not implemented yet. Will you help me?', 1374257295);
162
163
        $urlParts = array(
0 ignored issues
show
Unused Code introduced by
$urlParts = array('q' =>...this->getQueryLimit()); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
164
            'q' => sprintf('project:%s', $projectName),
165
            'n' => $this->getQueryLimit()
166
        );
167
168
        // The "o" parameter can be applied more than one time
169
        // This parameter defines how detailed the answer will be
170
        $oOptions = array(
171
            'LABELS',
172
            'DETAILED_LABELS',
173
            'CURRENT_REVISION',
174
            'ALL_REVISIONS',
175
            'CURRENT_COMMIT',
176
            'ALL_COMMITS',
177
            'CURRENT_FILES',
178
            'ALL_FILES',
179
            'DETAILED_ACCOUNTS',
180
            'MESSAGES'
181
        );
182
        $additionalFields = $this->buildQueryStringWithSameParameterName('o', $oOptions);
183
        $url = $this->getBaseUrl() . 'changes/?' . http_build_query($urlParts) . '&' . $additionalFields;
184
185
        $response = $this->getConnector()->get($url);
186
        $response = $this->verifyResult($response, $url);
187
188
        $content = $this->transformJsonResponse($response->getContent());
189
190
        return $content;
191
    }
192
193
    /**
194
     * This function build a url query string with a parameter which can be applied more than one time.
195
     * E.g. http://www.google.de/?q=5&q=6&q=7&q=8&q=9...
196
     *
197
     * This method is used to apply the parameter "o" in GET /changes/ command for REST-API.
198
     *
199
     * @see https://review.typo3.org/Documentation/rest-api-changes.html#list-changes
200
     *
201
     * @param string $parameterName Parametername which should be used more than one time
202
     * @param array $values Various of values
203
     * @return string
204
     */
205
    private function buildQueryStringWithSameParameterName($parameterName, array $values)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
206
    {
207
        $queryString = '';
208
209
        foreach ($values as $value) {
210
            if ($queryString) {
211
                $queryString .= '&';
212
            }
213
214
            $queryString .= http_build_query(array($parameterName => $value));
215
        }
216
217
        return $queryString;
218
    }
219
220
    /**
221
     * Returns the version of the Gerrit server
222
     *
223
     * @link https://review.typo3.org/Documentation/rest-api-config.html
224
     *
225
     * @return string
226
     */
227
    public function getVersion()
228
    {
229
        $url = $this->getBaseUrl() . 'config/server/version';
230
        $response = $this->getConnector()->get($url);
231
        $response = $this->verifyResult($response, $url);
232
233
        $content = $this->transformJsonResponse($response->getContent());
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->transformJsonResp...esponse->getContent()); of type array|null adds the type array to the return on line 235 which is incompatible with the return type declared by the interface Gerrie\API\DataService\D...ceInterface::getVersion of type string.
Loading history...
234
235
        return $content;
236
    }
237
}