1 | <?php |
||
8 | class ZonerSmsGateway |
||
9 | { |
||
10 | /** URL of the Zoner SMS-API service. */ |
||
11 | const ENDPOINT_URL = 'https://sms.zoner.fi/sms.php'; |
||
12 | |||
13 | /** @var HttpClient HTTP Client */ |
||
14 | protected $http; |
||
15 | |||
16 | /** @var string|null Zoner SMS-API username. */ |
||
17 | protected $username = null; |
||
18 | |||
19 | /** @var string|null Zoner SMS-API password. */ |
||
20 | protected $password = null; |
||
21 | |||
22 | /** @var string|null Default sender number or text. */ |
||
23 | protected $sender = null; |
||
24 | |||
25 | /** |
||
26 | * @param string|null $username |
||
27 | * @param string|null $password |
||
28 | * @param string|null $sender sender number or name |
||
29 | * @param HttpClient|null $httpClient |
||
30 | */ |
||
31 | 11 | public function __construct($username, $password, $sender = null, HttpClient $httpClient = null) |
|
39 | |||
40 | /** |
||
41 | * Gets the HttpClient. |
||
42 | * |
||
43 | * @return HttpClient |
||
44 | */ |
||
45 | 10 | protected function httpClient() |
|
49 | |||
50 | /** |
||
51 | * Sends a message via the gateway. |
||
52 | * |
||
53 | * @param string $receiver phone number where to send (for example "35840123456") |
||
54 | * @param string $message message to send (UTF-8, but this function converts it to ISO-8859-15) |
||
55 | * @param string|null $sender sender phone number (for example "35840123456") |
||
56 | * or string (max 11 chars, a-ZA-Z0-9) |
||
57 | * |
||
58 | * @return string tracking number |
||
59 | * |
||
60 | * @throws ZonerSmsGatewayException if sending failed. |
||
61 | */ |
||
62 | 10 | public function sendMessage($receiver, $message, $sender = null) |
|
63 | { |
||
64 | 10 | if (empty($this->username)) { |
|
65 | throw ZonerSmsGatewayException::usernameNotProvided(); |
||
66 | } |
||
67 | |||
68 | 10 | if (empty($receiver)) { |
|
69 | throw ZonerSmsGatewayException::receiverNotProvided(); |
||
70 | } |
||
71 | |||
72 | 10 | if (empty($sender)) { |
|
73 | 9 | if (empty($this->sender)) { |
|
74 | throw ZonerSmsGatewayException::senderNotProvided(); |
||
75 | } else { |
||
76 | 9 | $sender = $this->sender; |
|
77 | } |
||
78 | } |
||
79 | |||
80 | 10 | if (empty($message)) { |
|
81 | throw ZonerSmsGatewayException::emptyMessage(); |
||
82 | } |
||
83 | $params = [ |
||
84 | 10 | 'username' => $this->username, |
|
85 | 10 | 'password' => $this->password, |
|
86 | 10 | 'numberto' => $receiver, |
|
87 | 10 | 'numberfrom' => $sender, |
|
88 | 10 | 'message' => utf8_decode($message), |
|
89 | ]; |
||
90 | |||
91 | 10 | $response = $this->httpClient()->post(self::ENDPOINT_URL, [ |
|
92 | 10 | 'form_params' => $params, |
|
93 | ]); |
||
94 | 9 | if ($response->getStatusCode() === 200) { |
|
95 | 9 | $body = $response->getBody(); |
|
96 | 9 | $statusAndCode = explode(' ', $body, 2); |
|
97 | 9 | if ($statusAndCode[0] === 'OK') { |
|
98 | 8 | return $statusAndCode[1]; |
|
99 | 1 | } elseif ($statusAndCode[0] === 'ERR') { |
|
100 | 1 | throw ZonerSmsGatewayException::serviceRespondedWithAnError($statusAndCode[1]); |
|
101 | } else { |
||
102 | throw ZonerSmsGatewayException::unknownZonerResponse($statusAndCode); |
||
103 | } |
||
104 | } else { |
||
105 | throw ZonerSmsGatewayException::unexpectedHttpStatus($response); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Returns the amount of credits left in the service, or whatever the service replies. |
||
111 | * |
||
112 | * @throws ZonerSmsGatewayException if request failed. |
||
113 | */ |
||
114 | public function getCredits() |
||
130 | } |
||
131 |