Completed
Push — master ( 7e4f78...5ca817 )
by Filippo
02:47
created

GuzzleAdapter::handleError()   A

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 Guzzle\Http\Client;
15
use Guzzle\Http\ClientInterface;
16
use Guzzle\Http\Exception\RequestException;
17
use Guzzle\Http\Message\Response;
18
use IlGala\SMSFactor\Exceptions\HttpException;
19
20
/**
21
 * @author Filippo Galante <[email protected]>
22
 */
23
class GuzzleAdapter implements AdapterInterface
24
{
25
26
    /**
27
     * @var ClientInterface
28
     */
29
    protected $client;
30
31
    /**
32
     * @var Response
33
     */
34
    protected $response;
35
36
    /**
37
     * @param string               $username
38
     * @param string               $password
39
     * @param string               $accept
40
     * @param ClientInterface|null $client
41
     */
42
    public function __construct($username, $password, $accept, ClientInterface $client = null)
43
    {
44
        $this->client = $client ?: new Client();
0 ignored issues
show
Documentation Bug introduced by
It seems like $client ?: new \Guzzle\Http\Client() can also be of type object<Guzzle\Http\Client>. However, the property $client is declared as type object<Guzzle\Http\ClientInterface>. 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...
45
        $this->client->setDefaultOption('headers/sfusername', $username);
46
        $this->client->setDefaultOption('headers/sfpassword', $password);
47
        $this->client->setDefaultOption('headers/Accept', $accept);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 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...
54
    {
55
        try {
56
            $this->response = $this->client->get($url)->send();
57
        } catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class Guzzle\Http\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
58
            $this->response = $e->getResponse();
59
            $this->handleError();
60
        }
61
        return $this->response->getBody(true);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 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...
68
    {
69
        try {
70
            $this->response = $this->client->delete($url)->send();
71
        } catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class Guzzle\Http\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
72
            $this->response = $e->getResponse();
73
            $this->handleError();
74
        }
75
        return $this->response->getBody(true);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 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...
82
    {
83
        $request = $this->client->put($url);
84
        if (is_array($content)) {
85
            $request->setBody(json_encode($content), 'application/json');
86
        } else {
87
            $request->setBody($content);
88
        }
89
        try {
90
            $this->response = $request->send();
91
        } catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class Guzzle\Http\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
92
            $this->response = $e->getResponse();
93
            $this->handleError();
94
        }
95
        return $this->response->getBody(true);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 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...
102
    {
103
        $request = $this->client->post($url);
104
        if (is_array($content)) {
105
            $request->setBody(json_encode($content), 'application/json');
106
        } else {
107
            $request->setBody($content);
108
        }
109
        try {
110
            $this->response = $request->send();
111
        } catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class Guzzle\Http\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
112
            $this->response = $e->getResponse();
113
            $this->handleError();
114
        }
115
        return $this->response->getBody(true);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 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...
122
    {
123
        if (null === $this->response) {
124
            return;
125
        }
126
        return [
127
            'reset' => (int) (string) $this->response->getHeader('RateLimit-Reset'),
128
            'remaining' => (int) (string) $this->response->getHeader('RateLimit-Remaining'),
129
            'limit' => (int) (string) $this->response->getHeader('RateLimit-Limit'),
130
        ];
131
    }
132
133
    /**
134
     * @throws HttpException
135
     */
136 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...
137
    {
138
        $body = (string) $this->response->getBody(true);
139
        $code = (int) $this->response->getStatusCode();
140
        $content = json_decode($body);
141
        throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
142
    }
143
144
}
145