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

Client::executeListeners()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

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