|
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); |
|
|
|
|
|
|
54
|
|
|
$this->client->setDefaultOption('headers/sfpassword', $password); |
|
|
|
|
|
|
55
|
|
|
$this->client->setDefaultOption('headers/Accept', $accept); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public function get($url) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
try { |
|
65
|
|
|
$this->response = $this->client->get($url); |
|
66
|
|
|
} catch (RequestException $e) { |
|
67
|
|
|
$this->response = $e->getResponse(); |
|
|
|
|
|
|
68
|
|
|
$this->handleError(); |
|
69
|
|
|
} |
|
70
|
|
|
return $this->response->getBody(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function delete($url) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
try { |
|
79
|
|
|
$this->response = $this->client->delete($url); |
|
80
|
|
|
} catch (RequestException $e) { |
|
81
|
|
|
$this->response = $e->getResponse(); |
|
|
|
|
|
|
82
|
|
|
$this->handleError(); |
|
83
|
|
|
} |
|
84
|
|
|
return $this->response->getBody(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
View Code Duplication |
public function put($url, $content = '') |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
98
|
|
|
$this->handleError(); |
|
99
|
|
|
} |
|
100
|
|
|
return $this->response->getBody(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
public function post($url, $content = '') |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
114
|
|
|
$this->handleError(); |
|
115
|
|
|
} |
|
116
|
|
|
return $this->response->getBody(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
public function getLatestResponseHeaders() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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
|
|
|
|
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.