1
|
|
|
<?php |
2
|
|
|
namespace Isign\Gateway\Http; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp; |
5
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
6
|
|
|
use Isign\Gateway\Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Adapter for GuzzleHttp client |
10
|
|
|
*/ |
11
|
|
|
class GuzzleClientAdapter implements ClientInterface |
12
|
|
|
{ |
13
|
|
|
/** @var GuzzleHttp\ClientInterface */ |
14
|
|
|
protected $client; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param type GuzzleHttp\ClientInterface $client |
18
|
|
|
* @return self |
19
|
|
|
*/ |
20
|
|
|
public function __construct(GuzzleHttp\ClientInterface $client) |
21
|
|
|
{ |
22
|
|
|
$this->client = $client; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Send HTTP request |
27
|
|
|
* @param string $method POST|GET |
28
|
|
|
* @param string $url http URL |
29
|
|
|
* @param array $options query options. Query values goes under 'body' key. |
30
|
|
|
* Example: |
31
|
|
|
* $options = [ |
32
|
|
|
* 'query' => [ |
33
|
|
|
* 'access_token' => 'foobar', |
34
|
|
|
* ], |
35
|
|
|
* 'body' => [ |
36
|
|
|
* 'param1' => 'value1', |
37
|
|
|
* 'param2' => 'value2', |
38
|
|
|
* ] |
39
|
|
|
* ] |
40
|
|
|
* @param bool $expectJson whether JSON response is expected |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function sendRequest( |
44
|
|
|
string $method, |
45
|
|
|
string $url, |
46
|
|
|
array $options = [], |
47
|
|
|
bool $expectJson = true |
48
|
|
|
): array { |
49
|
|
|
$result = []; |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
$response = $this->client->send( |
53
|
|
|
$this->client->createRequest($method, $url, $options) |
54
|
|
|
); |
55
|
|
|
if ($expectJson) { |
56
|
|
|
$result = $response->json(); |
57
|
|
|
} else { |
58
|
|
|
$result = [ 'body' => $response->getBody() ]; |
59
|
|
|
} |
60
|
|
|
} catch (BadResponseException $e) { |
61
|
|
|
if ($e->getCode() == 400) { |
62
|
|
|
throw new Exception\InvalidData( |
63
|
|
|
'Data validation failed', |
64
|
|
|
400, |
65
|
|
|
$e, |
66
|
|
|
$e->getResponse()->json() |
67
|
|
|
); |
68
|
|
|
} elseif ($e->getCode() == 403) { |
69
|
|
|
throw new Exception\InvalidApiKey( |
70
|
|
|
'Access forbidden. Invalid API key.', |
71
|
|
|
403, |
72
|
|
|
$e, |
73
|
|
|
$e->getResponse()->getBody() |
74
|
|
|
); |
75
|
|
|
} elseif ($e->getCode() == 404) { |
76
|
|
|
throw new Exception\NotFound( |
77
|
|
|
'Requested URL was not found.', |
78
|
|
|
404, |
79
|
|
|
$e, |
80
|
|
|
$e->getResponse()->getBody() |
81
|
|
|
); |
82
|
|
|
} elseif ($e->getCode() == 500) { |
83
|
|
|
throw new Exception\ServerError( |
84
|
|
|
'Error occurred on server side while handling request', |
85
|
|
|
500, |
86
|
|
|
$e, |
87
|
|
|
$e->getResponse()->getBody() |
88
|
|
|
); |
89
|
|
|
} elseif ($e->getCode() == 504) { |
90
|
|
|
throw new Exception\Timeout( |
91
|
|
|
'Request timeout', |
92
|
|
|
504, |
93
|
|
|
$e, |
94
|
|
|
$e->getResponse()->getBody() |
95
|
|
|
); |
96
|
|
|
} else { |
97
|
|
|
throw new Exception\UnexpectedResponse( |
98
|
|
|
'Unexpected error occurred', |
99
|
|
|
$e->getCode(), |
100
|
|
|
$e, |
101
|
|
|
$e->getResponse()->getBody() |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} catch (\Exception $e) { |
105
|
|
|
throw new Exception\UnexpectedError('Unexpected error occurred', 0, $e); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $result; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|