Notification Setup Error

We have detected an error in your notification set-up (Event-ID dab39dc24f564ec7bd4628d1305fd03c). Currently, we cannot inform you about inspection progress. Please check that the user 557058:bca11929-8c2d-43f2-8a82-c5416880d395 still has access to your repository or update the API account.

Completed
Push — develop ( bb6001...f9dedd )
by Alexandru
03:55
created

Client::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 5
nc 3
nop 3
crap 4
1
<?php
2
3
/**
4
 * This file is part of the bitbucket-api package.
5
 *
6
 * (c) Alexandru G. <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Bitbucket\API\Http;
12
13
use Buzz\Client\ClientInterface as BuzzClientInterface;
14
use Buzz\Client\Curl;
15
use Buzz\Message\MessageInterface;
16
use Buzz\Message\RequestInterface;
17
use Buzz\Message\Request;
18
use Buzz\Message\Response;
19
use Bitbucket\API\Http\Listener\ListenerInterface;
20
21
/**
22
 * @author  Alexandru G.    <[email protected]>
23
 */
24
class Client extends ClientListener implements ClientInterface
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $options = array(
30
        'base_url'      => 'https://api.bitbucket.org',
31
        'api_version'   => '1.0',
32
        'api_versions'  => array('1.0', '2.0'),     // supported versions
33
        'format'        => 'json',
34
        'formats'       => array('json', 'xml'),    // supported response formats
35
        'user_agent'    => 'bitbucket-api-php/1.0.0-dev (https://bitbucket.org/gentlero/bitbucket-api)',
36
        'timeout'       => 10,
37
        'verify_peer'   => true
38
    );
39
40
    /**
41
     * @var BuzzClientInterface
42
     */
43
    protected $client;
44
45
    /**
46
     * @var RequestInterface
47
     */
48
    private $lastRequest;
49
50
    /**
51
     * @var MessageInterface
52
     */
53
    private $lastResponse;
54
55
    /**
56
     * @var MessageInterface
57
     */
58
    protected $responseObj;
59
60
    /**
61
     * @var RequestInterface
62
     */
63
    protected $requestObj;
64
65 271
    public function __construct(array $options = array(), BuzzClientInterface $client = null)
66
    {
67 271
        $this->client   = (null === $client) ? new Curl() : $client;
68 271
        $this->options  = array_merge($this->options, $options);
69
70 271
        $this->client->setTimeout($this->options['timeout']);
71 271
        $this->client->setVerifyPeer($this->options['verify_peer']);
72 271
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 5
    public function get($endpoint, $params = array(), $headers = array())
78
    {
79 5
        if (is_array($params) && count($params) > 0) {
80 1
            $endpoint   .= (strpos($endpoint, '?') === false ? '?' : '&').http_build_query($params, '', '&');
81 1
            $params     = array();
82 1
        }
83
84 5
        return $this->request($endpoint, $params, 'GET', $headers);
85 1
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2
    public function post($endpoint, $params = array(), $headers = array())
91
    {
92 2
        return $this->request($endpoint, $params, 'POST', $headers);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    public function put($endpoint, $params = array(), $headers = array())
99
    {
100 1
        return $this->request($endpoint, $params, 'PUT', $headers);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 2
    public function delete($endpoint, $params = array(), $headers = array())
107
    {
108 2
        return $this->request($endpoint, $params, 'DELETE', $headers);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 10
    public function request($endpoint, $params = array(), $method = 'GET', array $headers = array())
115
    {
116 10
        $request = $this->createRequest($method, $endpoint);
117
118
        // add a default content-type if none was set
119 10
        if (empty($headers['Content-Type']) && in_array(strtoupper($method), array('POST', 'PUT'), true)) {
120 3
            $headers['Content-Type'] = 'application/x-www-form-urlencoded';
121 3
        }
122
123 10
        if (count($headers) > 0) {
124 8
            $request->addHeaders($headers);
125 8
        }
126
127 10
        if (count($params) > 0) {
128 5
            $request->setContent(is_array($params) ? http_build_query($params) : $params);
129 5
        }
130
131 10
        $response = is_object($this->responseObj) ? $this->responseObj : new Response();
132
133 10
        $this->executeListeners($request, 'preSend');
134
135 9
        $this->client->send($request, $response);
136
137 9
        $this->executeListeners($request, 'postSend', $response);
138
139 9
        $this->lastRequest  = $request;
140 9
        $this->lastResponse = $response;
141
142 9
        return $response;
143
    }
144
145
    /**
146
     * @access public
147
     * @return BuzzClientInterface
148
     */
149 1
    public function getClient()
150
    {
151 1
        return $this->client;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 12
    public function getResponseFormat()
158
    {
159 12
        return $this->options['format'];
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 4
    public function setResponseFormat($format)
166
    {
167 4
        if (!in_array($format, $this->options['formats'], true)) {
168 2
            throw new \InvalidArgumentException(sprintf('Unsupported response format %s', $format));
169
        }
170
171 3
        $this->options['format'] = $format;
172
173 3
        return $this;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 12
    public function getApiVersion()
180
    {
181 12
        return $this->options['api_version'];
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 66
    public function setApiVersion($version)
188
    {
189 66
        if (!in_array($version, $this->options['api_versions'], true)) {
190 1
            throw new \InvalidArgumentException(sprintf('Unsupported API version %s', $version));
191
        }
192
193 65
        $this->options['api_version'] = $version;
194
195 65
        return $this;
196
    }
197
198
    /**
199
     * Check if specified API version is the one currently in use.
200
     *
201
     * @access public
202
     * @param  float $version
203
     * @return bool
204
     */
205 1
    public function isApiVersion($version)
206
    {
207 1
        return abs($this->options['api_version'] - $version) < 0.00001;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213 11
    public function getApiBaseUrl()
214
    {
215 11
        return $this->options['base_url'].'/'.$this->getApiVersion();
216
    }
217
218
    /**
219
     * @access public
220
     * @return MessageInterface
221
     */
222 5
    public function getLastRequest()
223
    {
224 5
        return $this->lastRequest;
225
    }
226
227
    /**
228
     * @access public
229
     * @return RequestInterface
230
     */
231 2
    public function getLastResponse()
232
    {
233 2
        return $this->lastResponse;
234
    }
235
236
    /**
237
     * @access public
238
     * @param  MessageInterface $response
239
     * @return void
240
     */
241
    public function setResponse(MessageInterface $response)
242
    {
243
        $this->responseObj = $response;
244
    }
245
246
    /**
247
     * @access public
248
     * @param  RequestInterface $request
249
     * @return void
250
     */
251
    public function setRequest(RequestInterface $request)
252
    {
253
        $this->requestObj = $request;
254
    }
255
256
    /**
257
     * @access protected
258
     * @param  string           $method
259
     * @param  string           $url
260
     * @return RequestInterface
261
     */
262 10
    protected function createRequest($method, $url)
263
    {
264
        // do not set base URL if a full one was provided
265 10
        if (false === strpos($url, $this->getApiBaseUrl())) {
266 9
            $url = $this->getApiBaseUrl().'/'.$url;
267 9
        }
268
269
        // change the response format
270 10
        if (strpos($url, 'format=') === false) {
271 9
            $url .= (strpos($url, '?') === false ? '?' : '&').'format='.$this->getResponseFormat();
272 9
        }
273
274 10
        $request = is_object($this->requestObj) ? $this->requestObj : new Request();
275 10
        $request->setMethod($method);
276 10
        $request->addHeaders(array(
277 10
                'User-Agent' => $this->options['user_agent']
278 10
            ));
279 10
        $request->setProtocolVersion(1.1);
280 10
        $request->fromUrl($url);
281
282 10
        return $request;
283
    }
284
285
    /**
286
     * Execute all available listeners.
287
     *
288
     * $when can be: preSend or postSend
289
     *
290
     * @access protected
291
     * @param RequestInterface $request
292
     * @param string           $when     When to execute the listener
293
     * @param MessageInterface $response
294
     */
295 10
    protected function executeListeners(RequestInterface $request, $when = 'preSend', MessageInterface $response = null)
296
    {
297 10
        $haveListeners  = count($this->listeners) > 0;
298
299 10
        if (!$haveListeners) {
300 6
            return;
301
        }
302
303 5
        $params = array($request);
304
305 5
        if (null !== $response) {
306 3
            $params[] = $response;
307 3
        }
308
309 5
        ksort($this->listeners, SORT_ASC);
310
311 5
        array_walk_recursive(
312 5
            $this->listeners,
313
            function ($class) use ($when, $params) {
314 5
                if ($class instanceof ListenerInterface) {
315 5
                    call_user_func_array(array($class, $when), $params);
316 5
                }
317 5
            }
318 5
        );
319 3
    }
320
}
321