Completed
Pull Request — master (#117)
by Graham
856:40 queued 854:34
created

BuzzAdapter::post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 15
rs 9.4286
cc 2
eloc 8
nc 2
nop 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\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
     * {@inheritdoc}
45
     */
46
    public function get($url)
47
    {
48
        $response = $this->browser->get($url);
49
50
        $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...
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);
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...
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);
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...
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);
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...
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;
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->isSuccessful()) {
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 = (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...
141
        $code = (int) $response->getStatusCode();
142
143
        $content = json_decode($response->getContent());
144
145
        throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
146
    }
147
}
148