1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Fazland\SkebbyRestClient\DataStructure; |
6
|
|
|
|
7
|
|
|
use Fazland\SkebbyRestClient\Constant\SendMethods; |
8
|
|
|
use Fazland\SkebbyRestClient\Exception\EmptyResponseException; |
9
|
|
|
use Fazland\SkebbyRestClient\Exception\UnknownErrorResponseException; |
10
|
|
|
use Fazland\SkebbyRestClient\Exception\XmlLoadException; |
11
|
|
|
use Throwable; |
12
|
|
|
|
13
|
|
|
use function libxml_get_errors; |
14
|
|
|
use function libxml_use_internal_errors; |
15
|
|
|
use function simplexml_load_string; |
16
|
|
|
use function sprintf; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Represents a Skebby Response. |
20
|
|
|
*/ |
21
|
|
|
class Response |
22
|
|
|
{ |
23
|
|
|
private string $status; |
|
|
|
|
24
|
|
|
private ?string $code = null; |
25
|
|
|
private ?string $errorMessage = null; |
26
|
|
|
private ?string $messageId = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @throws EmptyResponseException |
30
|
|
|
* @throws UnknownErrorResponseException |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $rawResponse) |
33
|
|
|
{ |
34
|
|
|
if (empty($rawResponse)) { |
35
|
|
|
throw new EmptyResponseException(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$doc = null; |
39
|
|
|
|
40
|
|
|
$useErrors = libxml_use_internal_errors(true); |
41
|
|
|
try { |
42
|
|
|
$doc = @simplexml_load_string($rawResponse); |
43
|
8 |
|
|
44
|
|
|
if ($doc === false) { |
45
|
8 |
|
throw new XmlLoadException($rawResponse, libxml_get_errors()); |
46
|
1 |
|
} |
47
|
|
|
} catch (Throwable $e) { |
48
|
|
|
throw new UnknownErrorResponseException($e->getMessage(), $rawResponse, $e); |
49
|
7 |
|
} finally { |
50
|
|
|
libxml_use_internal_errors($useErrors); |
51
|
7 |
|
} |
52
|
|
|
|
53
|
7 |
|
foreach (SendMethods::all() as $method) { |
54
|
|
|
if (! isset($doc->$method)) { |
55
|
7 |
|
continue; |
56
|
7 |
|
} |
57
|
|
|
|
58
|
|
|
$element = $doc->$method; |
59
|
|
|
|
60
|
7 |
|
if (! isset($element->status)) { |
61
|
7 |
|
throw new UnknownErrorResponseException('Missing response status value from Skebby', $rawResponse); |
62
|
|
|
} |
63
|
|
|
|
64
|
7 |
|
$this->status = (string) $element->status; |
65
|
7 |
|
$this->messageId = isset($element->id) ? (string) $element->id : null; |
66
|
7 |
|
|
67
|
|
|
if (! $this->isSuccessful()) { |
68
|
|
|
$this->code = isset($element->code) ? (string) $element->code : null; |
69
|
7 |
|
$this->errorMessage = isset($element->message) ? (string) $element->message : 'Unknown error'; |
70
|
|
|
} |
71
|
7 |
|
|
72
|
1 |
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
throw new UnknownErrorResponseException('Missing response status value from Skebby', $rawResponse); |
76
|
6 |
|
} |
77
|
|
|
|
78
|
6 |
|
/** |
79
|
1 |
|
* Gets the status. |
80
|
1 |
|
*/ |
81
|
|
|
public function getStatus(): string |
82
|
|
|
{ |
83
|
6 |
|
return $this->status; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Whether the response is successful or not. |
88
|
|
|
*/ |
89
|
|
|
public function isSuccessful(): bool |
90
|
|
|
{ |
91
|
|
|
return $this->status === 'success'; |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
/** |
95
|
|
|
* Gets the code. |
96
|
1 |
|
*/ |
97
|
|
|
public function getCode(): ?string |
98
|
|
|
{ |
99
|
|
|
return $this->code; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets the error message. |
104
|
6 |
|
*/ |
105
|
|
|
public function getErrorMessage(): ?string |
106
|
6 |
|
{ |
107
|
|
|
return $this->errorMessage; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets the message id. |
112
|
|
|
*/ |
113
|
|
|
public function getMessageId(): ?string |
114
|
|
|
{ |
115
|
|
|
return $this->messageId; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function __toString(): string |
119
|
|
|
{ |
120
|
|
|
return sprintf('Response status: %s, code: %s, error_message: %s, message_id: %s', $this->status, $this->status ?? '', $this->errorMessage ?? '', $this->messageId ?? ''); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|