This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Sender.php
199 lines (167 loc) · 4.49 KB
/
Sender.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* @author Lukáš Piják 2018 TOPefekt s.r.o.
* @link https://www.bulkgate.com/
*/
namespace BulkGate\Sms;
use BulkGate;
use BulkGate\Message\IConnection, BulkGate\Message\Response, BulkGate\Message\Request;
use BulkGate\Sms\SenderSettings\ISenderSettings;
class Sender implements ISender
{
use BulkGate\Strict;
/** @var IConnection */
private $connection;
/** @var ISenderSettings|null */
private $senderSettings;
/** @var bool */
private $unicode = false;
/** @var bool */
private $flash = false;
/** @var null|string */
private $defaultCountry = null;
/**
* Sender constructor.
* @param IConnection $connection
*/
public function __construct(IConnection $connection)
{
$this->connection = $connection;
}
/**
* @param bool $unicode
* @return ISender
*/
public function unicode($unicode = true)
{
$this->unicode = (bool) $unicode;
return $this;
}
/**
* @param bool $flash
* @return ISender
*/
public function flash($flash = true)
{
$this->flash = (bool) $flash;
return $this;
}
/**
* @param ISenderSettings $senderSettings
* @return ISender
*/
public function setSenderSettings(ISenderSettings $senderSettings)
{
$this->senderSettings = $senderSettings;
return $this;
}
/**
* @param string $country
* @return ISender
* @throws InvalidIsoCodeException
*/
public function setDefaultCountry($country)
{
if(preg_match('~^[a-zA-Z]{2}$~', (string) $country))
{
$this->defaultCountry = strtolower($country);
return $this;
}
throw new InvalidIsoCodeException('Invalid ISO 3166-1 alpha-2 format - '.$country);
}
/**
* @param IMessage $message
* @return Response
*/
public function send(IMessage $message)
{
$this->fillDefaultCountryIso($message);
/** @var Response $response */
$response = $this->connection->send(new Request($message->getType(), [
self::MESSAGE => $message,
self::SENDER => $this->senderSettings instanceof ISenderSettings ? $this->senderSettings : [],
self::UNICODE => $this->unicode,
self::FLASH => $this->flash,
], true));
if($message instanceof BulkMessage)
{
if($response->isSuccess())
{
$message->setStatus($response);
}
}
else if($message instanceof Message)
{
$message->setStatus(
isset($response->status) ? $response->status : 'error',
isset($response->sms_id) ? $response->sms_id : '',
isset($response->price) ? $response->price : 0.0
);
}
return $response;
}
/**
* @param array|Message\PhoneNumber|string $phoneNumbers
* @param null|string $iso
* @return Response
* @throws BulkGate\Exception
*/
public function checkPhoneNumbers($phoneNumbers, $iso = null)
{
$data = [];
if(is_string($phoneNumbers))
{
$data[] = new BulkGate\Sms\Message\PhoneNumber($phoneNumbers, $iso);
}
elseif(is_array($phoneNumbers))
{
foreach($phoneNumbers as $phoneNumber)
{
if($phoneNumber instanceof BulkGate\Sms\Message\PhoneNumber)
{
$data[] = $phoneNumber;
}
elseif(is_string($phoneNumber))
{
$data[] = new BulkGate\Sms\Message\PhoneNumber($phoneNumber, $iso);
}
}
}
elseif ($phoneNumbers instanceof BulkGate\Sms\Message\PhoneNumber)
{
$data[] = $phoneNumbers;
}
if(count($data) > 0)
{
return $this->connection->send(new Request('check-phone-numbers', ['phoneNumbers' => $data], true));
}
throw new InvalidPhoneNumbersException("Request does not contain any phone numbers (int|string|array|BulkGate\\Sms\\Message\\PhoneNumber)");
}
/**
* @param IMessage $message
*/
private function fillDefaultCountryIso(IMessage $message)
{
if($this->defaultCountry !== null)
{
if($message instanceof Message)
{
if($message->getPhoneNumber()->getIso() === null)
{
$message->getPhoneNumber()->iso($this->defaultCountry);
}
}
else if($message instanceof BulkMessage)
{
/** @var Message $m */
foreach($message as $m)
{
if($m->getPhoneNumber()->getIso() === null)
{
$m->getPhoneNumber()->iso($this->defaultCountry);
}
}
}
}
}
}