|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\ZonerSmsGateway; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
6
|
|
|
use NotificationChannels\ZonerSmsGateway\Exceptions\CouldNotSendNotification; |
|
7
|
|
|
|
|
8
|
|
|
class ZonerSmsGateway |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var HttpClient HTTP Client */ |
|
11
|
|
|
protected $http; |
|
12
|
|
|
|
|
13
|
|
|
/** @var string|null Zoner SMS-API username. */ |
|
14
|
|
|
protected $username = null; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string|null Zoner SMS-API password. */ |
|
17
|
|
|
protected $password = null; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string|null Default sender number or text. */ |
|
20
|
|
|
protected $sender = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string|null $username |
|
24
|
|
|
* @param string|null $password |
|
25
|
|
|
* @param string|null $sender sender number or name |
|
26
|
|
|
* @param HttpClient|null $httpClient |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function __construct($username, $password, $sender = null, HttpClient $httpClient = null) |
|
29
|
|
|
{ |
|
30
|
4 |
|
$this->username = $username; |
|
31
|
4 |
|
$this->password = $password; |
|
32
|
|
|
|
|
33
|
4 |
|
$this->sender = $sender; |
|
34
|
4 |
|
$this->http = $httpClient; |
|
35
|
4 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Gets the HttpClient. |
|
39
|
|
|
* |
|
40
|
|
|
* @return HttpClient |
|
41
|
|
|
*/ |
|
42
|
2 |
|
protected function httpClient() |
|
43
|
|
|
{ |
|
44
|
2 |
|
return $this->http ?: $this->http = new HttpClient(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Sends a message via the gateway. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $receiver phone number where to send (for example "35840123456") |
|
51
|
|
|
* @param string $message message to send (UTF-8, but this function converts it to ISO-8859-15) |
|
52
|
|
|
* @param string|null $sender sender phone number (for example "35840123456") |
|
53
|
|
|
* or string (max 11 chars, a-ZA-Z0-9) |
|
54
|
|
|
* |
|
55
|
|
|
* @return tracking number |
|
56
|
|
|
* |
|
57
|
|
|
* @throws CouldNotSendNotification if sending failed. |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function sendMessage($receiver, $message, $sender = null) |
|
60
|
|
|
{ |
|
61
|
2 |
|
if (empty($this->username)) { |
|
62
|
|
|
throw CouldNotSendNotification::usernameNotProvided(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
if (empty($receiver)) { |
|
66
|
|
|
throw CouldNotSendNotification::receiverNotProvided(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
if (empty($sender)) { |
|
70
|
2 |
|
if (empty($this->sender)) { |
|
71
|
|
|
throw CouldNotSendNotification::senderNotProvided(); |
|
72
|
|
|
} else { |
|
73
|
2 |
|
$sender = $this->sender; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
if (empty($message)) { |
|
78
|
|
|
throw CouldNotSendNotification::emptyMessage(); |
|
79
|
|
|
} |
|
80
|
2 |
|
$endPointUrl = 'https://sms.zoner.fi/sms.php'; |
|
81
|
|
|
|
|
82
|
|
|
$params = [ |
|
83
|
2 |
|
'username' => $this->username, |
|
84
|
2 |
|
'password' => $this->password, |
|
85
|
2 |
|
'numberto' => $receiver, |
|
86
|
2 |
|
'numberfrom' => $sender, |
|
87
|
2 |
|
'message' => utf8_decode($message), |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
2 |
|
$response = $this->httpClient()->post($endPointUrl, [ |
|
91
|
2 |
|
'form_params' => $params, |
|
92
|
|
|
]); |
|
93
|
2 |
|
if ($response->getStatusCode() === 200) { |
|
94
|
2 |
|
$body = $response->getBody(); |
|
95
|
2 |
|
$statusAndCode = explode(' ', $body, 2); |
|
96
|
2 |
|
if ($statusAndCode[0] === 'OK') { |
|
97
|
2 |
|
return $statusAndCode[1]; |
|
98
|
|
|
} elseif ($statusAndCode[0] === 'ERR') { |
|
99
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($statusAndCode[1]); |
|
100
|
|
|
} |
|
101
|
|
|
} else { |
|
102
|
|
|
throw CouldNotSendNotification::unexpectedHttpStatus($response); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|