GuzzleHttpAdapter::handleError()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Laravel SMSFactor.
5
 *
6
 * (c) Filippo Galante <[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 IlGala\SMSFactor\Adapters;
13
14
use GuzzleHttp\Client;
15
use GuzzleHttp\ClientInterface;
16
use GuzzleHttp\Exception\RequestException;
17
use GuzzleHttp\Message\ResponseInterface;
18
use GuzzleHttp\Psr7\Response;
19
use IlGala\SMSFactor\Exceptions\HttpException;
20
21
/**
22
 * @author Filippo Galante <[email protected]>
23
 */
24
class GuzzleHttpAdapter implements AdapterInterface
25
{
26
27
    /**
28
     * @var ClientInterface
29
     */
30
    protected $client;
31
32
    /**
33
     * @var Response|ResponseInterface
34
     */
35
    protected $response;
36
37
    /**
38
     * @param string               $username
39
     * @param string               $password
40
     * @param string               $accept
41
     * @param ClientInterface|null $client
42
     */
43
    public function __construct($username, $password, $accept, ClientInterface $client = null)
44
    {
45
        if (version_compare(ClientInterface::VERSION, '6') === 1) {
46
            $this->client = $client ?: new Client(['headers' => [
47
                    'sfusername' => $username,
48
                    'sfpassword' => $password,
49
                    'Accept' => $accept
50
            ]]);
51
        } else {
52
            $this->client = $client ?: new Client();
53
            $this->client->setDefaultOption('headers/sfusername', $username);
0 ignored issues
show
Bug introduced by
The method setDefaultOption() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
            $this->client->setDefaultOption('headers/sfpassword', $password);
0 ignored issues
show
Bug introduced by
The method setDefaultOption() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            $this->client->setDefaultOption('headers/Accept', $accept);
0 ignored issues
show
Bug introduced by
The method setDefaultOption() does not seem to exist on object<GuzzleHttp\ClientInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 View Code Duplication
    public function get($url)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        try {
65
            $this->response = $this->client->get($url);
66
        } catch (RequestException $e) {
67
            $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\R...sage\ResponseInterface>. 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...
68
            $this->handleError();
69
        }
70
        return $this->response->getBody();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 View Code Duplication
    public function delete($url)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        try {
79
            $this->response = $this->client->delete($url);
80
        } catch (RequestException $e) {
81
            $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\R...sage\ResponseInterface>. 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...
82
            $this->handleError();
83
        }
84
        return $this->response->getBody();
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 View Code Duplication
    public function put($url, $content = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $options = [];
93
        $options[is_array($content) ? 'json' : 'body'] = $content;
94
        try {
95
            $this->response = $this->client->put($url, $options);
96
        } catch (RequestException $e) {
97
            $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\R...sage\ResponseInterface>. 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...
98
            $this->handleError();
99
        }
100
        return $this->response->getBody();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 View Code Duplication
    public function post($url, $content = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        $options = [];
109
        $options[is_array($content) ? 'json' : 'body'] = $content;
110
        try {
111
            $this->response = $this->client->post($url, $options);
112
        } catch (RequestException $e) {
113
            $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\R...sage\ResponseInterface>. 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...
114
            $this->handleError();
115
        }
116
        return $this->response->getBody();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 View Code Duplication
    public function getLatestResponseHeaders()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        if (null === $this->response) {
125
            return;
126
        }
127
        return [
128
            'reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'),
129
            'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'),
130
            'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit'),
131
        ];
132
    }
133
134
    /**
135
     * @throws HttpException
136
     */
137 View Code Duplication
    protected function handleError()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        $body = (string) $this->response->getBody();
140
        $code = (int) $this->response->getStatusCode();
141
        $content = json_decode($body);
142
        throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
143
    }
144
}
145