1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erdemkeren\JetSms\Http\Responses; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class JetSMSHttpApiResponse. |
9
|
|
|
*/ |
10
|
|
|
final class JetSmsHttpResponse implements JetSmsResponseInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The read response of SMS message request.. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $responseAttributes = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The JetSMS error codes. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $statuses = [ |
25
|
|
|
'0' => 'Success', |
26
|
|
|
'-1' => 'The specified SMS outbox name is invalid', |
27
|
|
|
'-5' => 'The SMS service credentials are incorrect', |
28
|
|
|
'-6' => 'The specified data is malformed', |
29
|
|
|
'-7' => 'The send date of the SMS message has already expired', |
30
|
|
|
'-8' => 'The SMS gsm number is invalid', |
31
|
|
|
'-9' => 'The SMS message body is missing', |
32
|
|
|
'-15' => 'The SMS service is having some trouble at the moment', |
33
|
|
|
'-99' => 'The SMS service encountered an unexpected error', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a message response. |
38
|
|
|
* |
39
|
|
|
* @param string $responseBody |
40
|
|
|
*/ |
41
|
9 |
|
public function __construct($responseBody) |
42
|
|
|
{ |
43
|
9 |
|
$this->responseAttributes = $this->readResponseBodyString($responseBody); |
44
|
9 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Determine if the operation was successful or not. |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
3 |
|
public function isSuccessful() |
52
|
|
|
{ |
53
|
3 |
|
return '0' === $this->statusCode(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the status code. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
5 |
|
public function statusCode() |
62
|
|
|
{ |
63
|
5 |
|
return (string) $this->responseAttributes['statusCode']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the string representation of the status. |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
1 |
|
public function status() |
72
|
|
|
{ |
73
|
1 |
|
return array_key_exists($this->statusCode(), self::$statuses) |
74
|
1 |
|
? self::$statuses[$this->statusCode()] |
75
|
1 |
|
: 'Unknown'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the message of the response. |
80
|
|
|
* |
81
|
|
|
* @return null|string |
82
|
|
|
*/ |
83
|
1 |
|
public function message() |
84
|
|
|
{ |
85
|
1 |
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the group identifier from the response. |
90
|
|
|
*/ |
91
|
1 |
|
public function groupId() |
92
|
|
|
{ |
93
|
1 |
|
throw new BadMethodCallException( |
94
|
|
|
"JetSms Http API responses do not group bulk message identifiers. Use messageReportIdentifiers instead." |
95
|
1 |
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the message report identifiers for the messages sent. |
100
|
|
|
* Message report id returns -1 if invalid Msisdns, -2 if invalid message text. |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
2 |
|
public function messageReportIdentifiers() |
105
|
|
|
{ |
106
|
2 |
|
if (array_key_exists('messageids', $this->responseAttributes)) { |
107
|
1 |
|
return explode('|', $this->responseAttributes['messageids']); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return []; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Read the message response body string. |
115
|
|
|
* |
116
|
|
|
* @param $responseBodyString |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
private function readResponseBodyString($responseBodyString) |
120
|
|
|
{ |
121
|
9 |
|
$responseLines = array_filter(array_map(function ($value) { |
122
|
9 |
|
return trim($value); |
123
|
9 |
|
}, explode("\n", $responseBodyString))); |
124
|
|
|
|
125
|
9 |
|
$result = []; |
126
|
9 |
|
foreach ($responseLines as $responseLine) { |
127
|
9 |
|
$responseParts = explode('=', $responseLine); |
128
|
9 |
|
$result[strtolower($responseParts[0])] = $responseParts[1]; |
129
|
9 |
|
} |
130
|
|
|
|
131
|
9 |
|
$status = (int) $result['status']; |
132
|
9 |
|
unset($result['status']); |
133
|
9 |
|
$result['success'] = ($status >= 0) ? true : false; |
134
|
9 |
|
$result['statusCode'] = $status; |
135
|
|
|
|
136
|
9 |
|
return $result; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|