Completed
Push — master ( 48ee71...212bb2 )
by Graham
05:58 queued 05:53
created

src/Adapter/GuzzleHttpAdapter.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace DigitalOceanV2\Adapter;
4
5
use DigitalOceanV2\Exception\HttpException;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\ClientInterface;
8
use GuzzleHttp\Exception\RequestException;
9
use GuzzleHttp\Psr7\Response;
10
11
/**
12
 * @author Marcos Sigueros <[email protected]>
13
 * @author Chris Fidao <[email protected]>
14
 * @author Graham Campbell <[email protected]>
15
 */
16
class GuzzleHttpAdapter implements AdapterInterface
17
{
18
    /**
19
     * @var ClientInterface
20
     */
21
    protected $client;
22
23
    /**
24
     * @var Response
25
     */
26
    protected $response;
27
28
    /**
29
     * @param string               $token
30
     * @param ClientInterface|null $client
31
     */
32
    public function __construct($token, ClientInterface $client = null)
33
    {
34
        if (version_compare(ClientInterface::VERSION, '6') === 1) {
35
            $this->client = $client ?: new Client(['headers' => ['Authorization' => sprintf('Bearer %s', $token)]]);
36
        } else {
37
            $this->client = $client ?: new Client();
38
39
            $this->client->setDefaultOption('headers/Authorization', sprintf('Bearer %s', $token));
40
        }
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function get($url)
47
    {
48
        try {
49
            $this->response = $this->client->get($url);
50
        } catch (RequestException $e) {
51
            $this->response = $e->getResponse();
0 ignored issues
show
Documentation Bug introduced by
It seems like $e->getResponse() can also be of type object<Psr\Http\Message\ResponseInterface>. However, the property $response is declared as type object<GuzzleHttp\Psr7\Response>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
52
            $this->handleError();
53
        }
54
55
        return (string)$this->response->getBody();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function delete($url)
62
    {
63
        try {
64
            $this->response = $this->client->delete($url);
65
        } catch (RequestException $e) {
66
            $this->response = $e->getResponse();
0 ignored issues
show
Documentation Bug introduced by
It seems like $e->getResponse() can also be of type object<Psr\Http\Message\ResponseInterface>. However, the property $response is declared as type object<GuzzleHttp\Psr7\Response>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
67
            $this->handleError();
68
        }
69
70
        return (string)$this->response->getBody();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function put($url, $content = '')
77
    {
78
        $options = [];
79
80
        $options[is_array($content) ? 'json' : 'body'] = $content;
81
82
        try {
83
            $this->response = $this->client->put($url, $options);
84
        } catch (RequestException $e) {
85
            $this->response = $e->getResponse();
0 ignored issues
show
Documentation Bug introduced by
It seems like $e->getResponse() can also be of type object<Psr\Http\Message\ResponseInterface>. However, the property $response is declared as type object<GuzzleHttp\Psr7\Response>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
86
            $this->handleError();
87
        }
88
89
        return (string)$this->response->getBody();
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function post($url, $content = '')
96
    {
97
        $options = [];
98
99
        $options[is_array($content) ? 'json' : 'body'] = $content;
100
101
        try {
102
            $this->response = $this->client->post($url, $options);
103
        } catch (RequestException $e) {
104
            $this->response = $e->getResponse();
0 ignored issues
show
Documentation Bug introduced by
It seems like $e->getResponse() can also be of type object<Psr\Http\Message\ResponseInterface>. However, the property $response is declared as type object<GuzzleHttp\Psr7\Response>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
105
            $this->handleError();
106
        }
107
108
        return (string)$this->response->getBody();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function getLatestResponseHeaders()
115
    {
116
        if (null === $this->response) {
117
            return null;
118
        }
119
120
        return [
121
            'reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'),
122
            'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'),
123
            'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit'),
124
        ];
125
    }
126
127
    /**
128
     * @throws HttpException
129
     */
130
    protected function handleError()
131
    {
132
        $body = (string) $this->response->getBody();
133
        $code = (int) $this->response->getStatusCode();
134
135
        $content = json_decode($body);
136
137
        throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
138
    }
139
}
140