1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Facundo J Gonzalez |
4
|
|
|
* Date: 17/11/2017. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace NotificationChannels\SMSC\Clients\Http; |
8
|
|
|
|
9
|
|
|
use NotificationChannels\SMSC\Clients\SMSCApiResponseInterface; |
10
|
|
|
use NotificationChannels\SMSC\Exceptions\CouldNotSendNotification; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SMSCHttpApiResponse. |
14
|
|
|
*/ |
15
|
|
|
final class SMSCHttpApiResponse implements SMSCApiResponseInterface |
16
|
|
|
{ |
17
|
|
|
private $responseAttributes; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create a message response. |
21
|
|
|
* |
22
|
|
|
* @param string $responseBody |
23
|
|
|
*/ |
24
|
|
|
public function __construct($responseBody) |
25
|
|
|
{ |
26
|
|
|
$this->responseAttributes = $this->readResponseBodyString($responseBody); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the error code of the SMSC Api. |
31
|
|
|
* |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public function errorCode() |
35
|
|
|
{ |
36
|
|
|
if ($this->responseAttributes['code'] == 200) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->responseAttributes['code']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the error message og the SMSC Api. |
45
|
|
|
* |
46
|
|
|
* @return null|string |
47
|
|
|
*/ |
48
|
|
|
public function errorMessage() |
49
|
|
|
{ |
50
|
|
|
if ($this->responseAttributes['code'] == 200) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->responseAttributes['message']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Determine if the api responded with a success or not. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function isSuccess() |
63
|
|
|
{ |
64
|
|
|
return $this->responseAttributes['code'] == 200; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Read the message response body string. |
69
|
|
|
* |
70
|
|
|
* @param $responseBodyString |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
private function readResponseBodyString($responseBodyString) |
74
|
|
|
{ |
75
|
|
|
$decoded = json_decode($responseBodyString, true); |
76
|
|
|
|
77
|
|
|
if ($decoded == null) { |
78
|
|
|
throw CouldNotSendNotification::apiFailed('Invalid JSON'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $decoded; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|