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