|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace prgTW\BaseCRM\Transport; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
7
|
|
|
use GuzzleHttp\Exception\ServerException; |
|
8
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
|
9
|
|
|
use prgTW\BaseCRM\Client\ClientInterface; |
|
10
|
|
|
use prgTW\BaseCRM\Client\GuzzleClient; |
|
11
|
|
|
use prgTW\BaseCRM\Exception\RepresentationErrorException; |
|
12
|
|
|
use prgTW\BaseCRM\Utils\Convert; |
|
13
|
|
|
|
|
14
|
|
|
class Transport |
|
15
|
|
|
{ |
|
16
|
|
|
const TOKEN_FUTUERSIMPLE_NAME = 'X-Futuresimple-Token'; |
|
17
|
|
|
const TOKEN_PIPEJUMP_NAME = 'X-Pipejump-Auth'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var ClientInterface */ |
|
20
|
|
|
private $client; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $token; |
|
24
|
|
|
|
|
25
|
|
|
/** @var ResponseInterface */ |
|
26
|
|
|
protected $lastResponse; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $token |
|
30
|
|
|
* @param ClientInterface $client |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($token = '', ClientInterface $client = null) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->client = null !== $client ? $client : new GuzzleClient; |
|
|
|
|
|
|
35
|
|
|
$this->token = $token; |
|
36
|
|
|
$this->lastResponse = null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $uri |
|
41
|
|
|
* @param string $key |
|
|
|
|
|
|
42
|
|
|
* @param array $options |
|
43
|
|
|
* |
|
44
|
|
|
* @return array|bool |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
public function get($uri, $key = null, array $options = []) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$options = $this->getOptions($options); |
|
49
|
|
|
$response = $this->client->request('GET', $this->getUri($uri), $options); |
|
50
|
|
|
$decoded = $this->processResponse($response, $key); |
|
51
|
|
|
|
|
52
|
|
|
return $decoded; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $uri |
|
57
|
|
|
* @param string $key |
|
|
|
|
|
|
58
|
|
|
* @param array $options |
|
59
|
|
|
* |
|
60
|
|
|
* @return array|bool |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public function post($uri, $key = null, array $options = []) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$options = $this->getOptions($options); |
|
65
|
|
|
$response = $this->client->request('POST', $this->getUri($uri), $options); |
|
66
|
|
|
$decoded = $this->processResponse($response, $key); |
|
67
|
|
|
|
|
68
|
|
|
return $decoded; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $uri |
|
73
|
|
|
* @param string $key |
|
|
|
|
|
|
74
|
|
|
* @param array $options |
|
75
|
|
|
* |
|
76
|
|
|
* @return array|bool |
|
77
|
|
|
*/ |
|
78
|
|
View Code Duplication |
public function put($uri, $key = null, array $options = []) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$options = $this->getOptions($options); |
|
81
|
|
|
$response = $this->client->request('PUT', $this->getUri($uri), $options); |
|
82
|
|
|
$decoded = $this->processResponse($response, $key); |
|
83
|
|
|
|
|
84
|
|
|
return $decoded; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $uri |
|
89
|
|
|
* @param string $key |
|
|
|
|
|
|
90
|
|
|
* @param array $options |
|
91
|
|
|
* |
|
92
|
|
|
* @return array|bool |
|
93
|
|
|
*/ |
|
94
|
|
View Code Duplication |
public function delete($uri, $key = null, array $options = []) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$options = $this->getOptions($options); |
|
97
|
|
|
$response = $this->client->request('DELETE', $this->getUri($uri), $options); |
|
98
|
|
|
$decoded = $this->processResponse($response, $key); |
|
99
|
|
|
|
|
100
|
|
|
return $decoded; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param ResponseInterface $response |
|
105
|
|
|
* @param string $key |
|
|
|
|
|
|
106
|
|
|
* |
|
107
|
|
|
* @throws RepresentationErrorException on 422 response |
|
108
|
|
|
* @throws ClientException on 4xx errors |
|
109
|
|
|
* @throws ServerException on 5xx errors |
|
110
|
|
|
* @throws BadResponseException when key is not found in response data |
|
111
|
|
|
* @return array|bool |
|
112
|
|
|
*/ |
|
113
|
|
|
private function processResponse(ResponseInterface $response, $key = null) |
|
114
|
|
|
{ |
|
115
|
|
|
$status = $response->getStatusCode(); |
|
116
|
|
|
if (204 == $status) |
|
117
|
|
|
{ |
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$decoded = $response->json(); |
|
122
|
|
|
$decoded = Convert::objectToArray($decoded); |
|
123
|
|
|
|
|
124
|
|
|
if (422 == $status) |
|
125
|
|
|
{ |
|
126
|
|
|
//@codeCoverageIgnoreStart |
|
127
|
|
|
throw new RepresentationErrorException('', $this->client->getLastRequest(), $response); |
|
|
|
|
|
|
128
|
|
|
//@codeCoverageIgnoreEnd |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (200 <= $status && 300 > $status) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->lastResponse = $response; |
|
134
|
|
|
|
|
135
|
|
|
if (null === $key) |
|
136
|
|
|
{ |
|
137
|
|
|
return $decoded; |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (false === array_key_exists($key, $decoded)) |
|
141
|
|
|
{ |
|
142
|
|
|
//@codeCoverageIgnoreStart |
|
143
|
|
|
$message = sprintf('Key "%s" not found in data', $key); |
|
144
|
|
|
throw new BadResponseException($message, $this->client->getLastRequest(), $response); |
|
|
|
|
|
|
145
|
|
|
//@codeCoverageIgnoreEnd |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $decoded[$key]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
//@codeCoverageIgnoreStart |
|
152
|
|
|
throw BadResponseException::create($this->client->getLastRequest(), $response); |
|
|
|
|
|
|
153
|
|
|
//@codeCoverageIgnoreEnd |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $uri |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function getUri($uri) |
|
162
|
|
|
{ |
|
163
|
|
|
return $uri . '.json'; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param array $options |
|
168
|
|
|
* |
|
169
|
|
|
* @return array |
|
170
|
|
|
*/ |
|
171
|
|
|
private function getOptions(array $options) |
|
172
|
|
|
{ |
|
173
|
|
|
$options = array_merge_recursive([ |
|
174
|
|
|
'headers' => [ |
|
175
|
|
|
self::TOKEN_PIPEJUMP_NAME => $this->token, |
|
176
|
|
|
self::TOKEN_FUTUERSIMPLE_NAME => $this->token, |
|
177
|
|
|
], |
|
178
|
|
|
], $options); |
|
179
|
|
|
|
|
180
|
|
|
return $options; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getToken() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->token; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $token |
|
193
|
|
|
* |
|
194
|
|
|
* @return $this |
|
195
|
|
|
*/ |
|
196
|
|
|
public function setToken($token) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->token = $token; |
|
199
|
|
|
|
|
200
|
|
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.