1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace floor12\MindBox; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use floor12\MindBox\Exceptions\EmptyApiEndPointException; |
8
|
|
|
use floor12\MindBox\Exceptions\EmptyApiKeyException; |
9
|
|
|
use floor12\MindBox\Requests\MindBoxRequest; |
10
|
|
|
use GuzzleHttp\Client; |
11
|
|
|
use GuzzleHttp\ClientInterface; |
12
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
13
|
|
|
use GuzzleHttp\Psr7\Request; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
use function GuzzleHttp\Psr7\build_query; |
16
|
|
|
|
17
|
|
|
class MindBoxClient |
18
|
|
|
{ |
19
|
|
|
const MODE_ASYNCHRONOUS = 0; |
20
|
|
|
const MODE_SYNCHRONOUS = 1; |
21
|
|
|
const ASYNC_MINDBOX_API_URL = 'https://api.mindbox.ru/v3/operations/async'; |
22
|
|
|
const SYNC_MINDBOX_API_URL = 'https://api.mindbox.ru/v3/operations/sync'; |
23
|
|
|
const DEFAULT_HTTP_TIMEOUT = 15; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $endpointId; |
27
|
|
|
/** @var Client */ |
28
|
|
|
private $client; |
29
|
|
|
/** @var string[] */ |
30
|
|
|
private $headers; |
31
|
|
|
/** @var string */ |
32
|
|
|
private $secretKey; |
33
|
|
|
/** @var ResponseInterface */ |
34
|
|
|
private $response; |
35
|
|
|
/** @var Request */ |
36
|
|
|
private $httpRequest; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $secretKey |
40
|
|
|
* @param string $endpointId |
41
|
|
|
* @param ClientInterface|null $client |
42
|
|
|
* @throws EmptyApiEndPointException |
43
|
|
|
* @throws EmptyApiKeyException |
44
|
|
|
*/ |
45
|
3 |
|
public function __construct( |
46
|
|
|
string $secretKey, |
47
|
|
|
string $endpointId, |
48
|
|
|
ClientInterface $client = null) |
49
|
|
|
{ |
50
|
3 |
|
$this->client = $client ?? new Client(); |
51
|
|
|
|
52
|
3 |
|
$this->secretKey = $secretKey; |
53
|
3 |
|
$this->endpointId = $endpointId; |
54
|
|
|
|
55
|
3 |
|
if (empty($this->secretKey)) |
56
|
1 |
|
throw new EmptyApiKeyException(); |
57
|
|
|
|
58
|
2 |
|
if (empty($this->endpointId)) |
59
|
1 |
|
throw new EmptyApiEndPointException(); |
60
|
|
|
|
61
|
1 |
|
$this->headers = [ |
62
|
1 |
|
'Content-Type' => 'application/json; charset=utf-8', |
63
|
1 |
|
'Accept' => 'application/json', |
64
|
1 |
|
'Authorization' => "Mindbox secretKey=\"{$this->secretKey}\"" |
65
|
|
|
]; |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param ClientInterface $client |
70
|
|
|
*/ |
71
|
|
|
public function setClient(ClientInterface $client): void |
72
|
|
|
{ |
73
|
|
|
$this->client = $client; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param MindBoxRequest $mindBoxRequest |
78
|
|
|
* @throws GuzzleException |
79
|
|
|
*/ |
80
|
1 |
|
public function sendData(MindBoxRequest $mindBoxRequest): void |
81
|
|
|
{ |
82
|
|
|
$httpRequestParams = [ |
83
|
1 |
|
'endpointId' => $this->endpointId, |
84
|
1 |
|
'operation' => $mindBoxRequest->getOperationName() |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
|
88
|
1 |
|
if ($mindBoxRequest->getDeviceUUID()) { |
89
|
1 |
|
$httpRequestParams['deviceUUID'] = $mindBoxRequest->getDeviceUUID(); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
$baseUrl = $mindBoxRequest->isAsync() ? self::ASYNC_MINDBOX_API_URL : self::SYNC_MINDBOX_API_URL; |
93
|
|
|
|
94
|
1 |
|
$this->httpRequest = new Request( |
95
|
1 |
|
'POST', |
96
|
1 |
|
$baseUrl . '?' . build_query($httpRequestParams), |
|
|
|
|
97
|
1 |
|
$this->headers, |
98
|
1 |
|
$mindBoxRequest->getBodyAsJson() |
99
|
|
|
); |
100
|
1 |
|
$this->response = $this->client->send($this->httpRequest, ['timeout' => self::DEFAULT_HTTP_TIMEOUT]); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return ResponseInterface |
105
|
|
|
*/ |
106
|
|
|
public function getResponse(): ResponseInterface |
107
|
|
|
{ |
108
|
|
|
return $this->response; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return Request |
113
|
|
|
*/ |
114
|
|
|
public function getHttpRequest(): Request |
115
|
|
|
{ |
116
|
|
|
return $this->httpRequest; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.