Completed
Push — exceptions ( 2454be )
by Graham
832:48 queued 829:44
created

BuzzAdapter::handleError()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 2
eloc 5
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\Listener\ListenerInterface;
17
use Buzz\Message\Response;
18
use DigitalOceanV2\Exception\HttpException;
19
20
/**
21
 * @author Antoine Corcy <[email protected]>
22
 * @author Graham Campbell <[email protected]>
23
 */
24
class BuzzAdapter implements AdapterInterface
25
{
26
    /**
27
     * @var Browser
28
     */
29
    protected $browser;
30
31
    /**
32
     * @param string                 $token
33
     * @param Browser|null           $browser
34
     * @param ListenerInterface|null $listener
35
     */
36
    public function __construct($token, Browser $browser = null, ListenerInterface $listener = null)
37
    {
38
        $this->browser = $browser ?: new Browser(new Curl());
39
        $this->browser->addListener($listener ?: new BuzzOAuthListener($token));
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function get($url)
46
    {
47
        $response = $this->browser->get($url);
48
49
        $this->handleResponse($response);
1 ignored issue
show
Compatibility introduced by
$response of type object<Buzz\Message\MessageInterface> is not a sub-type of object<Buzz\Message\Response>. It seems like you assume a concrete implementation of the interface Buzz\Message\MessageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
50
51
        return $response->getContent();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function delete($url)
58
    {
59
        $response = $this->browser->delete($url);
60
61
        $this->handleResponse($response);
1 ignored issue
show
Compatibility introduced by
$response of type object<Buzz\Message\MessageInterface> is not a sub-type of object<Buzz\Message\Response>. It seems like you assume a concrete implementation of the interface Buzz\Message\MessageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function put($url, $content = '')
68
    {
69
        $headers = [];
70
71
        if (is_array($content)) {
72
            $content = json_encode($content);
73
            $headers[] = 'Content-Type: application/json';
74
        }
75
76
        $response = $this->browser->put($url, $headers, $content);
77
78
        $this->handleResponse($response);
1 ignored issue
show
Compatibility introduced by
$response of type object<Buzz\Message\MessageInterface> is not a sub-type of object<Buzz\Message\Response>. It seems like you assume a concrete implementation of the interface Buzz\Message\MessageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
79
80
        return $response->getContent();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function post($url, $content = '')
87
    {
88
        $headers = [];
89
90
        if (is_array($content)) {
91
            $content = json_encode($content);
92
            $headers[] = 'Content-Type: application/json';
93
        }
94
95
        $response = $this->browser->post($url, $headers, $content);
96
97
        $this->handleResponse($response);
1 ignored issue
show
Compatibility introduced by
$response of type object<Buzz\Message\MessageInterface> is not a sub-type of object<Buzz\Message\Response>. It seems like you assume a concrete implementation of the interface Buzz\Message\MessageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
98
99
        return $response->getContent();
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getLatestResponseHeaders()
106
    {
107
        if (null === $response = $this->browser->getLastResponse()) {
108
            return;
109
        }
110
111
        return [
112
            'reset' => (int) $response->getHeader('RateLimit-Reset'),
113
            'remaining' => (int) $response->getHeader('RateLimit-Remaining'),
114
            'limit' => (int) $response->getHeader('RateLimit-Limit'),
115
        ];
116
    }
117
118
    /**
119
     * @param Response $response
120
     *
121
     * @throws HttpException
122
     */
123
    protected function handleResponse(Response $response)
124
    {
125
        if ($response->isSuccessful()) {
126
            return;
127
        }
128
129
        $this->handleError($response);
130
    }
131
132
    /**
133
     * @param Response $response
134
     *
135
     * @throws HttpException
136
     */
137
    protected function handleError(Response $response)
138
    {
139
        $body = (string) $response->getContent();
0 ignored issues
show
Unused Code introduced by
$body is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
140
        $code = (int) $response->getStatusCode();
141
142
        $content = json_decode($response->getContent());
143
144
        throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
145
    }
146
}
147