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 ( 9b8b96...7451cd )
by Alexandru
12:06
created

Api::doRequest()   A

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 2
cp 0
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 4
crap 2
1
<?php
2
/**
3
 * This file is part of the bitbucket-api package.
4
 *
5
 * (c) Alexandru Guzinschi <[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 Bitbucket\API;
12
13
use Bitbucket\API\Http\Listener\NormalizeArrayListener;
14
use Buzz\Message\MessageInterface;
15
use Buzz\Client\ClientInterface as BuzzClientInterface;
16
use Bitbucket\API\Http\ClientInterface;
17
use Bitbucket\API\Http\Client;
18
use Buzz\Client\Curl;
19
20
/**
21
 * @author Alexandru Guzinschi <[email protected]>
22
 */
23
class Api
24
{
25
    /**
26
     * Api response codes.
27
     */
28
    const HTTP_RESPONSE_OK              = 200;
29
    const HTTP_RESPONSE_CREATED         = 201;
30
    const HTTP_RESPONSE_NO_CONTENT      = 204;
31
    const HTTP_RESPONSE_BAD_REQUEST     = 400;
32
    const HTTP_RESPONSE_UNAUTHORIZED    = 401;
33
    const HTTP_RESPONSE_FORBIDDEN       = 403;
34
    const HTTP_RESPONSE_NOT_FOUND       = 404;
35
36
    /**
37
     * Transport object
38
     * @var BuzzClientInterface
39
     */
40
    protected $client;
41
42
    /**
43
     * @var ClientInterface
44
     */
45
    protected $httpClient;
46
47
    /**
48
     * Authentication object
49
     * @var Authentication\AuthenticationInterface
50
     */
51
    protected $auth;
52
53
    /**
54
     * @param  BuzzClientInterface $client
55
     */
56 250
    public function __construct(BuzzClientInterface $client = null)
57
    {
58
        // @todo[1]: This exists for keeping BC. To be removed!
59 250
        $this->client       = (is_null($client)) ? new Curl : $client;
60 250
        $this->httpClient   = new Client(array(), $client);
61
62 250
        $this->httpClient->addListener(new NormalizeArrayListener());
63 250
    }
64
65
    /**
66
     * @access public
67
     * @return ClientInterface
68
     */
69 74
    public function getClient()
70
    {
71 74
        return $this->httpClient;
72
    }
73
74
    /**
75
     * @access public
76
     * @param  ClientInterface $client
77
     * @return $this
78
     */
79 93
    public function setClient(ClientInterface $client)
80
    {
81 93
        $this->httpClient = $client;
82
83 93
        return $this;
84
    }
85
86
    /**
87
     * Set API login credentials
88
     *
89
     * @access public
90
     * @param  Authentication\AuthenticationInterface $auth
91
     * @return void
92
     */
93 11
    public function setCredentials(Authentication\AuthenticationInterface $auth)
94
    {
95
        // keep BC
96 11
        if ($auth instanceof Authentication\Basic) {
97 11
            $this->getClient()->addListener(
98 11
                new Http\Listener\BasicAuthListener($auth->getUsername(), $auth->getPassword())
99 11
            );
100 11
        }
101
102 11
        $this->auth = $auth;
103 11
    }
104
105
    /**
106
     * Make an HTTP GET request to API
107
     *
108
     * @access public
109
     * @param  string           $endpoint API endpoint
110
     * @param  string|array     $params   GET parameters
111
     * @param  array            $headers  HTTP headers
112
     * @return MessageInterface
113
     */
114 2
    public function requestGet($endpoint, $params = array(), $headers = array())
115
    {
116 2
        return $this->getClient()->get($endpoint, $params, $headers);
117
    }
118
119
    /**
120
     * Make an HTTP POST request to API
121
     *
122
     * @access public
123
     * @param  string           $endpoint API endpoint
124
     * @param  string|array     $params   POST parameters
125
     * @param  array            $headers  HTTP headers
126
     * @return MessageInterface
127
     */
128 1
    public function requestPost($endpoint, $params = array(), $headers = array())
129
    {
130 1
        return $this->getClient()->post($endpoint, $params, $headers);
131
    }
132
133
    /**
134
     * Make an HTTP PUT request to API
135
     *
136
     * @access public
137
     * @param  string           $endpoint API endpoint
138
     * @param  string|array     $params   POST parameters
139
     * @param  array            $headers  HTTP headers
140
     * @return MessageInterface
141
     */
142 1
    public function requestPut($endpoint, $params = array(), $headers = array())
143
    {
144 1
        return $this->getClient()->put($endpoint, $params, $headers);
145
    }
146
147
    /**
148
     * Make a HTTP DELETE request to API
149
     *
150
     * @access public
151
     * @param  string           $endpoint API endpoint
152
     * @param  string|array     $params   DELETE parameters
153
     * @param  array            $headers  HTTP headers
154
     * @return MessageInterface
155
     */
156 1
    public function requestDelete($endpoint, $params = array(), $headers = array())
157
    {
158 1
        return $this->getClient()->delete($endpoint, $params, $headers);
159
    }
160
161
    /**
162
     * Create HTTP request
163
     *
164
     * @access protected
165
     * @param  string           $method   HTTP method
166
     * @param  string           $endpoint Api endpoint
167
     * @param  string|array     $params   Request parameter(s)
168
     * @param  array            $headers  HTTP headers
169
     * @return MessageInterface
170
     *
171
     * @throws \RuntimeException
172
     */
173
    protected function doRequest($method, $endpoint, $params, array $headers)
174
    {
175
        return $this->getClient()->request($endpoint, $params, $method, $headers);
176
    }
177
178
    /**
179
     * Set the preferred format for response
180
     *
181
     * @access public
182
     * @param  string $name Format name
183
     * @return self
184
     *
185
     * @throws \InvalidArgumentException
186
     */
187 1
    public function setFormat($name)
188
    {
189 1
        $this->getClient()->setResponseFormat($name);
190
191 1
        return $this;
192
    }
193
194
    /**
195
     * Get current format used for response
196
     *
197
     * @access public
198
     * @return string
199
     */
200 1
    public function getFormat()
201
    {
202 1
        return $this->getClient()->getResponseFormat();
203
    }
204
205
    /**
206
     * @param  string $name
207
     * @return Api
208
     *
209
     * @throws \InvalidArgumentException
210
     */
211 12
    public function api($name)
212
    {
213 12
        if (!is_string($name) || $name === '') {
214 3
            throw new \InvalidArgumentException('No child specified.');
215
        }
216
217
        /** @var Api $child */
218 9
        $class = '\\Bitbucket\\API\\'.$name;
219
220 9
        if (!class_exists($class)) {
221 2
            throw new \InvalidArgumentException(sprintf('No such child class [%s].', $name));
222
        }
223
224 7
        $child = new $class($this->client);
225 7
        $child->setClient($this->getClient());
226
227 7
        if ($this->getClient()->hasListeners()) {
228 6
            $child->getClient()->setListeners($this->getClient()->getListeners());
229 6
        }
230
231 7
        return $child;
232
    }
233
234
    /**
235
     * Convert JSON to array with error check
236
     *
237
     * @access protected
238
     * @param  string $body JSON data
239
     * @return array
240
     *
241
     * @throws \InvalidArgumentException
242
     */
243 14
    protected function decodeJSON($body)
244
    {
245 14
        $params = json_decode($body, true);
246
247 14
        if (!is_array($params) || (JSON_ERROR_NONE !== json_last_error())) {
248 8
            throw new \InvalidArgumentException('Invalid JSON data provided.');
249
        }
250
251 6
        return $params;
252
    }
253
}
254