1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace floor12\MindBox; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use floor12\MindBox\Exceptions\EmptyApiEndPointException; |
8
|
|
|
use floor12\MindBox\Exceptions\EmptyApiKeyException; |
9
|
|
|
use GuzzleHttp\Client; |
10
|
|
|
use GuzzleHttp\ClientInterface; |
11
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
12
|
|
|
use GuzzleHttp\Psr7\Request; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use function GuzzleHttp\Psr7\build_query; |
15
|
|
|
|
16
|
|
|
class MindBoxClient |
17
|
|
|
{ |
18
|
|
|
const MODE_ASYNCHRONOUS = 0; |
19
|
|
|
const MODE_SYNCHRONOUS = 1; |
20
|
|
|
const ASYNC_MINDBOX_API_URL = 'https://api.mindbox.ru/v3/operations/async'; |
21
|
|
|
const SYNC_MINDBOX_API_URL = 'https://api.mindbox.ru/v3/operations/sync'; |
22
|
|
|
const DEFAULT_HTTP_TIMEOUT = 2; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $endpointId; |
26
|
|
|
/** @var ClientInterface */ |
27
|
|
|
private $client; |
28
|
|
|
/** @var string[] */ |
29
|
|
|
private $headers; |
30
|
|
|
/** @var string */ |
31
|
|
|
private $secretKey; |
32
|
|
|
/** @var ResponseInterface */ |
33
|
|
|
private $response; |
34
|
|
|
/** @var Request */ |
35
|
|
|
private $httpRequest; |
36
|
|
|
/** @var int */ |
37
|
|
|
private $httpTimeOut = self::DEFAULT_HTTP_TIMEOUT; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $secretKey |
41
|
|
|
* @param string $endpointId |
42
|
|
|
* @param ClientInterface|null $client |
43
|
|
|
* @throws EmptyApiEndPointException |
44
|
|
|
* @throws EmptyApiKeyException |
45
|
|
|
*/ |
46
|
3 |
|
public function __construct( |
47
|
|
|
string $secretKey, |
48
|
|
|
string $endpointId, |
49
|
|
|
ClientInterface $client = null) |
50
|
|
|
{ |
51
|
3 |
|
$this->client = $client ?? new Client(); |
52
|
|
|
|
53
|
3 |
|
$this->secretKey = $secretKey; |
54
|
3 |
|
$this->endpointId = $endpointId; |
55
|
|
|
|
56
|
3 |
|
if (empty($this->secretKey)) |
57
|
1 |
|
throw new EmptyApiKeyException(); |
58
|
|
|
|
59
|
2 |
|
if (empty($this->endpointId)) |
60
|
1 |
|
throw new EmptyApiEndPointException(); |
61
|
|
|
|
62
|
1 |
|
$this->headers = [ |
63
|
1 |
|
'Content-Type' => 'application/json; charset=utf-8', |
64
|
1 |
|
'Accept' => 'application/json', |
65
|
1 |
|
'Authorization' => "Mindbox secretKey=\"{$this->secretKey}\"" |
66
|
|
|
]; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ClientInterface $client |
71
|
|
|
*/ |
72
|
|
|
public function setClient(ClientInterface $client): void |
73
|
|
|
{ |
74
|
|
|
$this->client = $client; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param MindBoxRequest $mindBoxRequest |
79
|
|
|
* @throws GuzzleException |
80
|
|
|
*/ |
81
|
1 |
|
public function sendData(MindBoxRequest $mindBoxRequest): void |
82
|
|
|
{ |
83
|
|
|
$httpRequestParams = [ |
84
|
1 |
|
'endpointId' => $this->endpointId, |
85
|
1 |
|
'operation' => $mindBoxRequest->getOperationName() |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
|
89
|
1 |
|
if ($mindBoxRequest->getDeviceUUID()) { |
90
|
1 |
|
$httpRequestParams['deviceUUID'] = $mindBoxRequest->getDeviceUUID(); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$baseUrl = $mindBoxRequest->isAsync() ? self::ASYNC_MINDBOX_API_URL : self::SYNC_MINDBOX_API_URL; |
94
|
|
|
|
95
|
1 |
|
$this->httpRequest = new Request( |
96
|
1 |
|
'POST', |
97
|
1 |
|
$baseUrl . '?' . build_query($httpRequestParams), |
|
|
|
|
98
|
1 |
|
$this->headers, |
99
|
1 |
|
$mindBoxRequest->getBodyAsJson() |
100
|
|
|
); |
101
|
1 |
|
$this->response = $this->client->send($this->httpRequest, ['timeout' => $this->httpTimeOut]); |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return ResponseInterface |
106
|
|
|
*/ |
107
|
|
|
public function getResponse(): ResponseInterface |
108
|
|
|
{ |
109
|
|
|
return $this->response; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Request |
114
|
|
|
*/ |
115
|
|
|
public function getHttpRequest(): Request |
116
|
|
|
{ |
117
|
|
|
return $this->httpRequest; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param int $httpTimeOut |
122
|
|
|
*/ |
123
|
|
|
public function setHttpTimeOut(int $httpTimeOut): void |
124
|
|
|
{ |
125
|
|
|
$this->httpTimeOut = $httpTimeOut; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.