Client.php
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Lackoxygen\MinPayment\Utils;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\GuzzleException;
use Lackoxygen\MinPayment\Exception\Exception;
use Lackoxygen\MinPayment\Request\Request;
use Lackoxygen\MinPayment\Response;
class Client
{
/**
* @var Request $request
*/
protected $request;
/**
* @var array $config
*/
protected $config = [];
/**
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
}
/**
* @param Request $request
*
* @return Response
* @throws Exception
*/
public function __invoke(Request $request)
{
$this->request = $request;
$body = $request($this->config);
return $this->send($body);
}
/**
* @param array $body
*
* @return Response
* @throws Exception
*/
protected function send(array $body)
{
$client = new \GuzzleHttp\Client(\Arr::get($this->config, 'options.client'));
$retries = (int)\Arr::get($this->config, 'retry');
$retry = 0;
do {
try {
$response = $client->request($this->request->getMethod(), $this->request->getPath(), [
$this->request->getOption() => $body
]);
return new Response($response);
} catch (GuzzleException $e) {
throw new Exception($e);
}
} while ($retry++ < $retries);
}
}