|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace floor12\DalliApi; |
|
4
|
|
|
|
|
5
|
|
|
use floor12\DalliApi\Exceptions\EmptyTokenException; |
|
6
|
|
|
use floor12\DalliApi\Models\DalliApiBody; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
10
|
|
|
use GuzzleHttp\Psr7\Request; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
|
|
13
|
|
|
class DalliClient |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var ClientInterface */ |
|
16
|
|
|
protected $client; |
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $dalliEndpoint; |
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
protected $errors = []; |
|
21
|
|
|
/** @var ResponseInterface */ |
|
22
|
|
|
protected $response; |
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $responseBody; |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected $authToken; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* DalliClient constructor. |
|
30
|
|
|
* @param string $dalliEndpoint |
|
31
|
|
|
* @param string|null $authToken |
|
32
|
|
|
* @param ClientInterface|null $client |
|
33
|
|
|
* @throws EmptyTokenException |
|
34
|
|
|
*/ |
|
35
|
4 |
|
public function __construct(string $dalliEndpoint, ?string $authToken, ClientInterface $client = null) |
|
36
|
|
|
{ |
|
37
|
4 |
|
if (empty($authToken)) |
|
38
|
1 |
|
throw new EmptyTokenException(); |
|
39
|
|
|
|
|
40
|
3 |
|
$this->client = $client ?? new Client(); |
|
41
|
3 |
|
$this->dalliEndpoint = $dalliEndpoint; |
|
42
|
3 |
|
$this->authToken = $authToken; |
|
43
|
3 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param DalliApiBody $bodyObject |
|
47
|
|
|
* @return DalliApiBody |
|
48
|
|
|
*/ |
|
49
|
3 |
|
protected function addAuthTokenToBody(DalliApiBody $bodyObject): DalliApiBody |
|
50
|
|
|
{ |
|
51
|
3 |
|
$auth = $bodyObject->mainElement->addChild('auth'); |
|
52
|
3 |
|
$auth->addAttribute('token', $this->authToken); |
|
53
|
3 |
|
return $bodyObject; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
3 |
|
protected function parseErrors(): bool |
|
60
|
|
|
{ |
|
61
|
3 |
|
$pattern = '/errorMessage=\'(.+)\'/'; |
|
62
|
3 |
|
if (!preg_match_all($pattern, $this->responseBody, $matches)) { |
|
63
|
1 |
|
return true; |
|
64
|
|
|
} |
|
65
|
2 |
|
$this->errors = $matches[1]; |
|
66
|
2 |
|
if ($this->errors[0] === 'Успешно') { |
|
67
|
1 |
|
$this->errors = []; |
|
68
|
1 |
|
return true; |
|
69
|
|
|
} |
|
70
|
1 |
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param DalliApiBody $bodyObject |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
* @throws GuzzleException |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public function sendApiRequest(DalliApiBody $bodyObject): bool |
|
79
|
|
|
{ |
|
80
|
3 |
|
$bodyObject = $this->addAuthTokenToBody($bodyObject); |
|
81
|
3 |
|
$headers = ['Content-type' => 'application/xml']; |
|
82
|
3 |
|
$request = new Request('POST', $this->dalliEndpoint, $headers, $bodyObject->getAsXmlString()); |
|
83
|
3 |
|
$this->response = $this->client->send($request); |
|
84
|
3 |
|
$this->responseBody = (clone $this->response)->getBody()->getContents(); |
|
85
|
3 |
|
return $this->parseErrors(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function getErrors(): array |
|
92
|
|
|
{ |
|
93
|
2 |
|
return $this->errors; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getResponseBody(): string |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->responseBody; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return ResponseInterface |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getResponse(): ResponseInterface |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->response; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|