Completed
Pull Request — master (#97)
by
unknown
01:58
created

DropboxGuzzleHttpClient::getResponseBody()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Kunnu\Dropbox\Http\Clients;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Request;
7
use Psr\Http\Message\StreamInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use GuzzleHttp\Exception\RequestException;
10
use Kunnu\Dropbox\Http\DropboxRawResponse;
11
use GuzzleHttp\Exception\BadResponseException;
12
use Kunnu\Dropbox\Exceptions\DropboxClientException;
13
14
/**
15
 * DropboxGuzzleHttpClient.
16
 */
17
class DropboxGuzzleHttpClient implements DropboxHttpClientInterface
18
{
19
    /**
20
     * GuzzleHttp client.
21
     *
22
     * @var \GuzzleHttp\Client
23
     */
24
    protected $client;
25
26
    /**
27
     * Create a new DropboxGuzzleHttpClient instance.
28
     *
29
     * @param Client $client GuzzleHttp Client
30
     */
31 3
    public function __construct(Client $client = null)
32
    {
33
        //Set the client
34 3
        $this->client = $client ?: new Client();
35 3
    }
36
37
    /**
38
     * Send request to the server and fetch the raw response.
39
     *
40
     * @param  string $url     URL/Endpoint to send the request to
41
     * @param  string $method  Request Method
42
     * @param  string|resource|StreamInterface $body Request Body
43
     * @param  array  $headers Request Headers
44
     * @param  array  $options Additional Options
45
     *
46
     * @return \Kunnu\Dropbox\Http\DropboxRawResponse Raw response from the server
47
     *
48
     * @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
49
     */
50 2
    public function send($url, $method, $body, $headers = [], $options = [])
51
    {
52
        //Create a new Request Object
53 2
        $request = new Request($method, $url, $headers, $body);
54
55
        try {
56
            //Send the Request
57 2
            $rawResponse = $this->client->send($request, $options);
58
        } catch (BadResponseException $e) {
59
            throw new DropboxClientException($e->getResponse()->getBody(), $e->getCode(), $e);
60
        } catch (RequestException $e) {
61
            $rawResponse = $e->getResponse();
62
63
            if (! $rawResponse instanceof ResponseInterface) {
64
                throw new DropboxClientException($e->getMessage(), $e->getCode());
65
            }
66
        }
67
68
        //Something went wrong
69 2
        if ($rawResponse->getStatusCode() >= 400) {
70
            throw new DropboxClientException($rawResponse->getBody());
71
        }
72
73 2
        if (array_key_exists('sink', $options)) {
74
            //Response Body is saved to a file
75
            $body = '';
76
        } else {
77
            //Get the Response Body
78 2
            $body = $this->getResponseBody($rawResponse);
79
        }
80
81 2
        $rawHeaders = $rawResponse->getHeaders();
82 2
        $httpStatusCode = $rawResponse->getStatusCode();
83
84
        //Create and return a DropboxRawResponse object
85 2
        return new DropboxRawResponse($rawHeaders, $body, $httpStatusCode);
86
    }
87
88
    /**
89
     * Get the Response Body.
90
     *
91
     * @param string|\Psr\Http\Message\ResponseInterface $response Response object
92
     *
93
     * @return string
94
     */
95 2
    protected function getResponseBody($response)
96
    {
97
        //Response must be string
98 2
        $body = $response;
99
100 2
        if ($response instanceof ResponseInterface) {
101
            //Fetch the body
102 2
            $body = $response->getBody();
103
        }
104
105 2
        if ($body instanceof StreamInterface) {
106 2
            $body = $body->getContents();
107
        }
108
109 2
        return (string) $body;
110
    }
111
}
112