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 ( a55da1...39a772 )
by
unknown
27:43 queued 12:35
created

Client   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 37
lcom 2
cbo 11
dl 0
loc 221
ccs 61
cts 61
cp 1
rs 9.44
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A isApiVersion() 0 4 1
A getApiBaseUrl() 0 4 1
A getLastRequest() 0 4 1
A getLastResponse() 0 4 1
A __construct() 0 19 2
A get() 0 9 4
A post() 0 4 1
A put() 0 4 1
A delete() 0 4 1
C request() 0 34 14
A getClient() 0 4 1
A getClientBuilder() 0 4 1
A getResponseFormat() 0 4 1
A setResponseFormat() 0 10 2
A getApiVersion() 0 4 1
A setApiVersion() 0 15 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 Bitbucket\API\Http\Plugin\ApiVersionPlugin;
14
use Bitbucket\API\Http\Plugin\HistoryPlugin;
15
use Http\Client\Common\HttpMethodsClient;
16
use Http\Client\Common\Plugin;
17
use Http\Discovery\UriFactoryDiscovery;
18
use Http\Message\MessageFactory;
19
use Psr\Http\Message\RequestInterface;
20
use Psr\Http\Message\ResponseInterface;
21
22
/**
23
 * @author  Alexandru G.    <[email protected]>
24
 */
25
class Client implements ClientInterface
26
{
27
    /**
28
     * @var array
29
     */
30
    protected $options = array(
31
        'base_url'      => 'https://api.bitbucket.org',
32
        'api_version'   => '2.0',
33
        'api_versions'  => array('1.0', '2.0'),     // supported versions
34
        'format'        => 'json',
35
        'formats'       => array('json', 'xml'),    // supported response formats
36
        'user_agent'    => 'bitbucket-api-php/2.0.0 (https://bitbucket.org/gentlero/bitbucket-api)',
37
        'timeout'       => 10,
38
        'verify_peer'   => true
39
    );
40
41
    /** @var HttpPluginClientBuilder */
42
    private $httpClientBuilder;
43
    /** @var MessageFactory */
44
    private $messageFactory;
45
    /** @var HistoryPlugin */
46
    private $responseHistory;
47
48
    public function __construct(array $options = array(), HttpPluginClientBuilder $httpClientBuilder = null)
49
    {
50
        $this->responseHistory = new HistoryPlugin();
51
        $this->options = array_merge(array_merge($this->options, $options));
52
        $this->httpClientBuilder = $httpClientBuilder ?: new HttpPluginClientBuilder();
53
54
        $this->httpClientBuilder->addPlugin(
55
            new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri($this->options['base_url']))
56
        );
57
        $this->httpClientBuilder->addPlugin(new Plugin\RedirectPlugin());
58
        $this->httpClientBuilder->addPlugin(new Plugin\HeaderDefaultsPlugin([
59
            'User-Agent' => $this->options['user_agent'],
60
        ]));
61
        $this->httpClientBuilder->addPlugin(new Plugin\HistoryPlugin($this->responseHistory));
62
63
        $this->setApiVersion($this->options['api_version']);
64
65 314
        $this->messageFactory = $this->httpClientBuilder->getMessageFactory();
66
    }
67 314
68 314
    /**
69
     * {@inheritdoc}
70 314
     */
71 314
    public function get($endpoint, $params = array(), $headers = array())
72 314
    {
73
        if (is_array($params) && count($params) > 0) {
74
            $endpoint   .= (strpos($endpoint, '?') === false ? '?' : '&').http_build_query($params, '', '&');
75
            $params     = array();
76
        }
77 8
78
        return $this->request($endpoint, $params, 'GET', $headers);
79 8
    }
80 1
81 1
    /**
82
     * {@inheritdoc}
83
     */
84 8
    public function post($endpoint, $params = array(), $headers = array())
85
    {
86
        return $this->request($endpoint, $params, 'POST', $headers);
87
    }
88
89
    /**
90 3
     * {@inheritdoc}
91
     */
92 3
    public function put($endpoint, $params = array(), $headers = array())
93
    {
94
        return $this->request($endpoint, $params, 'PUT', $headers);
95
    }
96
97
    /**
98 1
     * {@inheritdoc}
99
     */
100 1
    public function delete($endpoint, $params = array(), $headers = array())
101
    {
102
        return $this->request($endpoint, $params, 'DELETE', $headers);
103
    }
104
105
    /**
106 1
     * {@inheritdoc}
107
     */
108 1
    public function request($endpoint, $params = array(), $method = 'GET', array $headers = array())
109
    {
110
        // add a default content-type if none was set
111
        if (empty($headers['Content-Type']) && in_array(strtoupper($method), array('POST', 'PUT'), true)) {
112
            $headers['Content-Type'] = 'application/x-www-form-urlencoded';
113
        }
114 16
115
        $paramsString = null;
116
        if (is_array($params) && count($params) > 0) {
117 16
            if (isset($headers['Content-Type']) && $headers['Content-Type'] === 'application/json') {
118
                $paramsString = json_encode($params);
119
            } else {
120 16
                $paramsString = http_build_query($params);
121 2
            }
122
        }
123
124 16
        $body = null;
125 10
        if (is_string($paramsString) && $paramsString !== null) {
126
            $body = $paramsString;
127
        }
128 16
129 16
        if (is_string($params) && $params !== null) {
130 4
            $body = $params;
131
        }
132
133 16
        // change the response format
134 4
        if ($this->getApiVersion() === '1.0' && strpos($endpoint, 'format=') === false) {
135
            $endpoint .= (strpos($endpoint, '?') === false ? '?' : '&').'format='.$this->getResponseFormat();
136
        }
137 16
138 2
        $request = $this->messageFactory->createRequest($method, $endpoint, $headers, $body);
139
140
        return $this->getClient()->sendRequest($request);
141 16
    }
142
143 16
    /**
144
     * @return HttpMethodsClient
145 14
     */
146
    public function getClient()
147 14
    {
148
        return $this->httpClientBuilder->getHttpClient();
149 14
    }
150 14
151
    /**
152 14
     * @return HttpPluginClientBuilder
153
     */
154
    public function getClientBuilder()
155
    {
156
        return $this->httpClientBuilder;
157
    }
158
159 1
    /**
160
     * {@inheritdoc}
161 1
     */
162
    public function getResponseFormat()
163
    {
164
        return $this->options['format'];
165
    }
166
167 12
    /**
168
     * {@inheritdoc}
169 12
     */
170
    public function setResponseFormat($format)
171
    {
172
        if (!in_array($format, $this->options['formats'], true)) {
173
            throw new \InvalidArgumentException(sprintf('Unsupported response format %s', $format));
174
        }
175 4
176
        $this->options['format'] = $format;
177 4
178 2
        return $this;
179
    }
180
181 3
    /**
182
     * {@inheritdoc}
183 3
     */
184
    public function getApiVersion()
185
    {
186
        return $this->options['api_version'];
187
    }
188
189 17
    /**
190
     * {@inheritdoc}
191 17
     */
192
    public function setApiVersion($version)
193
    {
194
        if (!in_array($version, $this->options['api_versions'], true)) {
195
            throw new \InvalidArgumentException(sprintf('Unsupported API version %s', $version));
196
        }
197 113
198
        if (!$this->isApiVersion($version) || !$this->httpClientBuilder->hasPlugin(ApiVersionPlugin::class)) {
199 113
            $this->options['api_version'] = $version;
200 8
201
            $this->httpClientBuilder->removePlugin(ApiVersionPlugin::class);
202
            $this->httpClientBuilder->addPlugin(new ApiVersionPlugin($this->options['api_version']));
203 105
        }
204
205 105
        return $this;
206
    }
207
208
    /**
209
     * Check if specified API version is the one currently in use.
210
     *
211
     * @access public
212
     * @param  string $version
213
     * @return bool
214
     */
215 1
    public function isApiVersion($version)
216
    {
217 1
        return abs($this->options['api_version'] - $version) < 0.00001;
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223 16
    public function getApiBaseUrl()
224
    {
225 16
        return $this->options['base_url'].'/'.$this->getApiVersion();
226
    }
227
228
    /**
229
     * @access public
230
     * @return RequestInterface
231
     */
232 7
    public function getLastRequest()
233
    {
234 7
        return $this->responseHistory->getLastRequest();
235
    }
236
237
    /**
238
     * @access public
239
     * @return ResponseInterface
240
     */
241 2
    public function getLastResponse()
242
    {
243 2
        return $this->responseHistory->getLastResponse();
244
    }
245
}
246