BuzzAdapter   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 0
loc 123
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 4
A get() 0 8 1
A delete() 0 6 1
A put() 0 15 2
A post() 0 15 2
A getLatestResponseHeaders() 0 12 2
A handleResponse() 0 8 2
A handleError() 0 9 2
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\Message\Response;
18
use Buzz\Middleware\MiddlewareInterface;
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 MiddlewareInterface|null $middleware
36
     */
37
    public function __construct($token, Browser $browser = null, MiddlewareInterface $middleware = null)
38
    {
39
        $this->browser = $browser ?: new Browser(function_exists('curl_exec') ? new Curl() : new FileGetContents());
40
        $this->browser->addMiddleware($middleware ?: new BuzzOAuthMiddleware($token));
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function get($url)
47
    {
48
        $response = $this->browser->get($url);
49
50
        $this->handleResponse($response);
51
52
        return $response->getContent();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function delete($url)
59
    {
60
        $response = $this->browser->delete($url);
61
62
        $this->handleResponse($response);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function put($url, $content = '')
69
    {
70
        $headers = [];
71
72
        if (is_array($content)) {
73
            $content = json_encode($content);
74
            $headers[] = 'Content-Type: application/json';
75
        }
76
77
        $response = $this->browser->put($url, $headers, $content);
78
79
        $this->handleResponse($response);
80
81
        return $response->getContent();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function post($url, $content = '')
88
    {
89
        $headers = [];
90
91
        if (is_array($content)) {
92
            $content = json_encode($content);
93
            $headers[] = 'Content-Type: application/json';
94
        }
95
96
        $response = $this->browser->post($url, $headers, $content);
97
98
        $this->handleResponse($response);
99
100
        return $response->getContent();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getLatestResponseHeaders()
107
    {
108
        if (null === $response = $this->browser->getLastResponse()) {
109
            return null;
110
        }
111
112
        return [
113
            'reset' => (int) $response->getHeader('RateLimit-Reset'),
114
            'remaining' => (int) $response->getHeader('RateLimit-Remaining'),
115
            'limit' => (int) $response->getHeader('RateLimit-Limit'),
116
        ];
117
    }
118
119
    /**
120
     * @param Response $response
121
     *
122
     * @throws HttpException
123
     */
124
    protected function handleResponse(Response $response)
125
    {
126
        if ($response->getStatusCode() === 200) {
127
            return;
128
        }
129
130
        $this->handleError($response);
131
    }
132
133
    /**
134
     * @param Response $response
135
     *
136
     * @throws HttpException
137
     */
138
    protected function handleError(Response $response)
139
    {
140
        $body = $response->getContent();
141
        $code = $response->getStatusCode();
142
143
        $content = json_decode($body);
144
145
        throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
146
    }
147
}
148