|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace floor12\DalliApi; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\ClientInterface; |
|
7
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
8
|
|
|
use GuzzleHttp\Psr7\Request; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
|
|
11
|
|
|
class DalliClient |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ClientInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $client; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $dalliEndpoint; |
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
private $errors = []; |
|
23
|
|
|
/** @var ResponseInterface */ |
|
24
|
|
|
private $response; |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $responseBody; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* DalliClient constructor. |
|
30
|
|
|
* @param string $dalliEndpoint |
|
31
|
|
|
* @param ClientInterface|null $client |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function __construct(string $dalliEndpoint, ClientInterface $client = null) |
|
34
|
|
|
{ |
|
35
|
2 |
|
$this->client = $client ?? new Client(); |
|
36
|
2 |
|
$this->dalliEndpoint = $dalliEndpoint; |
|
37
|
2 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return bool |
|
41
|
|
|
*/ |
|
42
|
2 |
|
private function parseErrors(): bool |
|
43
|
|
|
{ |
|
44
|
2 |
|
$pattern = '/errorMessage=\'(.+)\'/'; |
|
45
|
2 |
|
if (preg_match_all($pattern, $this->responseBody, $matches)) { |
|
46
|
1 |
|
$this->errors = $matches[1]; |
|
47
|
1 |
|
return false; |
|
48
|
|
|
} |
|
49
|
1 |
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $body Body as XML string |
|
54
|
|
|
* @return boolean |
|
55
|
|
|
* @throws GuzzleException |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function sendApiRequest(string $body): bool |
|
58
|
|
|
{ |
|
59
|
2 |
|
$headers = ['Content-type' => 'application/xml']; |
|
60
|
2 |
|
$request = new Request('POST', $this->dalliEndpoint, $headers, $body); |
|
61
|
2 |
|
$this->response = $this->client->send($request); |
|
62
|
2 |
|
$this->responseBody = $this->response->getBody()->getContents(); |
|
63
|
2 |
|
return $this->parseErrors(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function getErrors() |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->errors; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getResponseBody(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->responseBody; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|