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
Pull Request — develop ( #41 )
by
unknown
27:26 queued 12:27
created

Api::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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