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
|
10 |
|
public function __construct($username, $password, $sender = null, HttpClient $httpClient = null) |
29
|
|
|
{ |
30
|
10 |
|
$this->username = $username; |
31
|
10 |
|
$this->password = $password; |
32
|
|
|
|
33
|
10 |
|
$this->sender = $sender; |
34
|
10 |
|
$this->http = $httpClient; |
35
|
10 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Gets the HttpClient. |
39
|
|
|
* |
40
|
|
|
* @return HttpClient |
41
|
|
|
*/ |
42
|
10 |
|
protected function httpClient() |
43
|
|
|
{ |
44
|
10 |
|
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
|
10 |
|
public function sendMessage($receiver, $message, $sender = null) |
60
|
|
|
{ |
61
|
10 |
|
if (empty($this->username)) { |
62
|
|
|
throw CouldNotSendNotification::usernameNotProvided(); |
63
|
|
|
} |
64
|
|
|
|
65
|
10 |
|
if (empty($receiver)) { |
66
|
|
|
throw CouldNotSendNotification::receiverNotProvided(); |
67
|
|
|
} |
68
|
|
|
|
69
|
10 |
|
if (empty($sender)) { |
70
|
9 |
|
if (empty($this->sender)) { |
71
|
|
|
throw CouldNotSendNotification::senderNotProvided(); |
72
|
|
|
} else { |
73
|
9 |
|
$sender = $this->sender; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
10 |
|
if (empty($message)) { |
78
|
|
|
throw CouldNotSendNotification::emptyMessage(); |
79
|
|
|
} |
80
|
10 |
|
$endPointUrl = 'https://sms.zoner.fi/sms.php'; |
81
|
|
|
|
82
|
|
|
$params = [ |
83
|
10 |
|
'username' => $this->username, |
84
|
10 |
|
'password' => $this->password, |
85
|
10 |
|
'numberto' => $receiver, |
86
|
10 |
|
'numberfrom' => $sender, |
87
|
10 |
|
'message' => utf8_decode($message), |
88
|
|
|
]; |
89
|
|
|
|
90
|
10 |
|
$response = $this->httpClient()->post($endPointUrl, [ |
91
|
10 |
|
'form_params' => $params, |
92
|
|
|
]); |
93
|
9 |
|
if ($response->getStatusCode() === 200) { |
94
|
9 |
|
$body = $response->getBody(); |
95
|
9 |
|
$statusAndCode = explode(' ', $body, 2); |
96
|
9 |
|
if ($statusAndCode[0] === 'OK') { |
97
|
8 |
|
return $statusAndCode[1]; |
98
|
1 |
|
} elseif ($statusAndCode[0] === 'ERR') { |
99
|
1 |
|
throw CouldNotSendNotification::serviceRespondedWithAnError($statusAndCode[1]); |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
|
|
throw CouldNotSendNotification::unexpectedHttpStatus($response); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|