Completed
Pull Request — master (#156)
by
unknown
01:57
created

BuzzAdapter::setTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the DigitalOceanV2 library.
5
 *
6
 * (c) Antoine Corcy <[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
12
namespace DigitalOceanV2\Adapter;
13
14
use Buzz\Browser;
15
use Buzz\Client\Curl;
16
use Buzz\Client\FileGetContents;
17
use Buzz\Listener\ListenerInterface;
18
use Buzz\Message\Response;
19
use DigitalOceanV2\Exception\HttpException;
20
21
/**
22
 * @author Antoine Corcy <[email protected]>
23
 * @author Graham Campbell <[email protected]>
24
 */
25
class BuzzAdapter implements AdapterInterface
26
{
27
    /**
28
     * @var Browser
29
     */
30
    protected $browser;
31
32
    /**
33
     * @param string                 $token
34
     * @param Browser|null           $browser
35
     * @param ListenerInterface|null $listener
36
     * @param int                    $timeout
37
     */
38
    public function __construct($token, Browser $browser = null, ListenerInterface $listener = null, $timeout = 5)
39
    {
40
        $this->browser = $browser ?: new Browser(function_exists('curl_exec') ? new Curl() : new FileGetContents());
41
        $this->browser->addListener($listener ?: new BuzzOAuthListener($token));
42
        $this->setTimeout($timeout);
43
    }
44
45
    /**
46
     * @param $timeout
47
     */
48
    public function setTimeout($timeout)
49
    {
50
        $this->browser->getClient()->setTimeout($timeout);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function get($url)
57
    {
58
        $response = $this->browser->get($url);
59
60
        $this->handleResponse($response);
61
62
        return $response->getContent();
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function delete($url)
69
    {
70
        $response = $this->browser->delete($url);
71
72
        $this->handleResponse($response);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function put($url, $content = '')
79
    {
80
        $headers = [];
81
82
        if (is_array($content)) {
83
            $content = json_encode($content);
84
            $headers[] = 'Content-Type: application/json';
85
        }
86
87
        $response = $this->browser->put($url, $headers, $content);
88
89
        $this->handleResponse($response);
90
91
        return $response->getContent();
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function post($url, $content = '')
98
    {
99
        $headers = [];
100
101
        if (is_array($content)) {
102
            $content = json_encode($content);
103
            $headers[] = 'Content-Type: application/json';
104
        }
105
106
        $response = $this->browser->post($url, $headers, $content);
107
108
        $this->handleResponse($response);
109
110
        return $response->getContent();
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getLatestResponseHeaders()
117
    {
118
        if (null === $response = $this->browser->getLastResponse()) {
119
            return;
120
        }
121
122
        return [
123
            'reset' => (int) $response->getHeader('RateLimit-Reset'),
124
            'remaining' => (int) $response->getHeader('RateLimit-Remaining'),
125
            'limit' => (int) $response->getHeader('RateLimit-Limit'),
126
        ];
127
    }
128
129
    /**
130
     * @param Response $response
131
     *
132
     * @throws HttpException
133
     */
134
    protected function handleResponse(Response $response)
135
    {
136
        if ($response->isSuccessful()) {
137
            return;
138
        }
139
140
        $this->handleError($response);
141
    }
142
143
    /**
144
     * @param Response $response
145
     *
146
     * @throws HttpException
147
     */
148
    protected function handleError(Response $response)
149
    {
150
        $body = (string) $response->getContent();
151
        $code = (int) $response->getStatusCode();
152
153
        $content = json_decode($body);
154
155
        throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
156
    }
157
}
158