1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\AllMySms; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
6
|
|
|
use GuzzleHttp\RequestOptions; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
class AllMySms |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The HTTP client. |
13
|
|
|
* |
14
|
|
|
* @var \GuzzleHttp\Client |
15
|
|
|
*/ |
16
|
|
|
protected $http; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The service configuration. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $config; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new AllMySms instance. |
27
|
|
|
* |
28
|
|
|
* @param \GuzzleHttp\Client $http |
29
|
|
|
* @param array $config |
30
|
|
|
*/ |
31
|
2 |
|
public function __construct(HttpClient $http, array $config) |
32
|
|
|
{ |
33
|
2 |
|
$this->http = $http; |
34
|
2 |
|
$this->config = $config; |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get HTTP client. |
39
|
|
|
* |
40
|
|
|
* @return \GuzzleHttp\Client |
41
|
|
|
*/ |
42
|
2 |
|
protected function httpClient(): HttpClient |
43
|
|
|
{ |
44
|
2 |
|
return $this->http ?? new HttpClient(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Send the sms. |
49
|
|
|
* |
50
|
|
|
* @param string $to |
51
|
|
|
* @param array $data |
52
|
|
|
* @param string|null $sender |
53
|
|
|
* @return \Psr\Http\Message\ResponseInterface|null |
54
|
|
|
*/ |
55
|
2 |
|
public function sendSms(string $to, array $data, ?string $sender = null): ?ResponseInterface |
56
|
|
|
{ |
57
|
2 |
|
$response = $this->httpClient()->post($this->getFullUrl('sendSms'), [ |
58
|
2 |
|
RequestOptions::FORM_PARAMS => $this->formatRequest($to, $data, $sender), |
59
|
|
|
]); |
60
|
|
|
|
61
|
2 |
|
return $response->getStatusCode() === 200 |
62
|
1 |
|
? $this->checkResponseAndReturn($response) |
63
|
2 |
|
: $response; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
protected function checkResponseAndReturn(ResponseInterface $response): ResponseInterface |
67
|
|
|
{ |
68
|
1 |
|
$content = json_decode($response->getBody()->getContents()); |
69
|
|
|
|
70
|
1 |
|
$code = data_get($content, 'status'); |
71
|
|
|
|
72
|
1 |
|
return $code === 100 |
73
|
|
|
? $response |
74
|
1 |
|
: $response->withStatus(400, data_get($content, 'statusText', 'An error occurred!')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the full url for the given path. |
79
|
|
|
* |
80
|
|
|
* @param string $path |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
2 |
|
protected function getFullUrl(string $path): string |
84
|
|
|
{ |
85
|
2 |
|
return trim($this->config['uri'], '/').'/'.ltrim($path, '/'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the form params. |
90
|
|
|
* |
91
|
|
|
* @param string $to |
92
|
|
|
* @param array $data |
93
|
|
|
* @param string|null $sender |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
2 |
|
protected function formatRequest(string $to, array $data, ?string $sender = null): array |
97
|
|
|
{ |
98
|
2 |
|
$index = 1; |
99
|
|
|
$parameters = collect(data_get($data, 'parameters', []))->mapWithKeys(function ($item) use (&$index) { |
100
|
|
|
return ["PARAM_{$index}" => $item]; |
101
|
2 |
|
})->toArray(); |
102
|
|
|
|
103
|
|
|
$sms = [ |
104
|
|
|
'DATA' => [ |
105
|
2 |
|
'MESSAGE' => $data['message'], |
106
|
|
|
'SMS' => [ |
107
|
2 |
|
array_merge([ |
108
|
2 |
|
'MOBILEPHONE' => $to, |
109
|
2 |
|
], $parameters), |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
]; |
113
|
|
|
|
114
|
2 |
|
if (! empty($parameters)) { |
115
|
|
|
$sms['DATA']['DYNAMIC'] = count($parameters); |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
if ($sender = data_get($data, 'sender', $sender)) { |
119
|
|
|
$sms['DATA']['TPOA'] = $sender; |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
if ($campaign = data_get($data, 'campaign')) { |
123
|
|
|
$sms['DATA']['CAMPAIGN_NAME'] = $campaign; |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
if ($date = data_get($data, 'date')) { |
127
|
|
|
$sms['DATA']['DATE'] = $date; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return [ |
131
|
2 |
|
'login' => $this->config['login'], |
132
|
2 |
|
'apiKey' => $this->config['api_key'], |
133
|
2 |
|
'returnformat' => $this->config['format'], |
134
|
2 |
|
'smsData' => json_encode($sms), |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|