Completed
Pull Request — master (#154)
by
unknown
03:51
created

BuzzAdapter::getBrowser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     */
37
    public function __construct($token, Browser $browser = null, ListenerInterface $listener = null)
38
    {
39
        $this->browser = $browser ?: new Browser(function_exists('curl_exec') ? new Curl() : new FileGetContents());
40
        $this->browser->addListener($listener ?: new BuzzOAuthListener($token));
41
    }
42
43
    /**
44
     * @return Browser
45
     */
46
    public function getBrowser()
47
    {
48
        return $this->browser;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function get($url)
55
    {
56
        $response = $this->browser->get($url);
57
58
        $this->handleResponse($response);
59
60
        return $response->getContent();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function delete($url)
67
    {
68
        $response = $this->browser->delete($url);
69
70
        $this->handleResponse($response);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function put($url, $content = '')
77
    {
78
        $headers = [];
79
80
        if (is_array($content)) {
81
            $content = json_encode($content);
82
            $headers[] = 'Content-Type: application/json';
83
        }
84
85
        $response = $this->browser->put($url, $headers, $content);
86
87
        $this->handleResponse($response);
88
89
        return $response->getContent();
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function post($url, $content = '')
96
    {
97
        $headers = [];
98
99
        if (is_array($content)) {
100
            $content = json_encode($content);
101
            $headers[] = 'Content-Type: application/json';
102
        }
103
104
        $response = $this->browser->post($url, $headers, $content);
105
106
        $this->handleResponse($response);
107
108
        return $response->getContent();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function getLatestResponseHeaders()
115
    {
116
        if (null === $response = $this->browser->getLastResponse()) {
117
            return;
118
        }
119
120
        return [
121
            'reset' => (int) $response->getHeader('RateLimit-Reset'),
122
            'remaining' => (int) $response->getHeader('RateLimit-Remaining'),
123
            'limit' => (int) $response->getHeader('RateLimit-Limit'),
124
        ];
125
    }
126
127
    /**
128
     * @param Response $response
129
     *
130
     * @throws HttpException
131
     */
132
    protected function handleResponse(Response $response)
133
    {
134
        if ($response->isSuccessful()) {
135
            return;
136
        }
137
138
        $this->handleError($response);
139
    }
140
141
    /**
142
     * @param Response $response
143
     *
144
     * @throws HttpException
145
     */
146
    protected function handleError(Response $response)
147
    {
148
        $body = (string) $response->getContent();
149
        $code = (int) $response->getStatusCode();
150
151
        $content = json_decode($body);
152
153
        throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
154
    }
155
}
156