Passed
Push — master ( ecb9c7...c6aa7b )
by Ramesh
55s queued 10s
created

Client::guzzleRequest()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 4
nop 4
crap 3
1
<?php
2
3
namespace BackblazeB2\Http;
4
5
use BackblazeB2\ErrorHandler;
6
use BackblazeB2\Exceptions\B2Exception;
7
use GuzzleHttp\Client as GuzzleClient;
8
use GuzzleHttp\Exception\GuzzleException;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Client wrapper around Guzzle.
13
 */
14
class Client extends GuzzleClient
15
{
16
    /**
17
     * Sends a response to the B2 API, automatically handling decoding JSON and errors.
18
     *
19
     * @param string $method
20
     * @param null   $uri
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $uri is correct as it would always require null to be passed?
Loading history...
21
     * @param array  $options
22
     * @param bool   $asJson
23
     *
24
     * @throws GuzzleException If the request fails.
25
     * @throws B2Exception     If the B2 server replies with an error.
26
     *
27
     * @return mixed|ResponseInterface|string
28
     */
29 27
    public function guzzleRequest($method, $uri = null, array $options = [], $asJson = true)
30
    {
31 27
        $response = parent::request($method, $uri, $options);
32
33 27
        if ($response->getStatusCode() !== 200) {
34 7
            ErrorHandler::handleErrorResponse($response);
35
        }
36
37 27
        if ($asJson) {
38 27
            return json_decode($response->getBody(), true);
39
        }
40
41 4
        return $response->getBody()->getContents();
42
    }
43
}
44