-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSms.php
272 lines (239 loc) · 6.33 KB
/
Sms.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* @package yii2-sms
* @author Yuri Shekhovtsov <shekhovtsovy@yandex.ru>
* @copyright Copyright © Yuri Shekhovtsov, lowbase.ru, 2016
* @version 1.0.0
*/
namespace lowbase\sms;
use yii\base\ErrorException;
use yii\base\Exception;
use yii\base\Object;
use lowbase\sms\models\Sms as smsModel;
use yii\helpers\ArrayHelper;
/**
* Class Sms
* @package lowbase\sms
*/
class Sms extends Object
{
/**
* Sms services and their settings
*
* 'services' => [
* ['providerName_1'] => [
* 'class' => '',
* 'login' => '',
* 'password' => '',
* 'order' => '',
* ],
* ['providerName_2'] => [
* 'class' => '',
* 'login' => '',
* 'password' => ''
* 'order' => '',
* ],
* ]
*
* @var array
*/
protected $services = [];
protected $availableServices = [];
/** @var AbstractService $currentService */
protected $currentService;
protected $currentServiceName;
protected $cascade = false;
/**
* Initialize the component.
*/
public function init()
{
parent::init();
$this->registerTranslations();
if (!$this->services) {
throw new ErrorException(\Yii::t('sms', 'Services are not configured.'));
}
foreach ($this->services as $service) {
if (!key_exists('class', $service)) {
throw new ErrorException(\Yii::t('sms', 'Class Unknown.'));
}
if (!key_exists('login', $service)) {
throw new ErrorException(\Yii::t('sms', 'Login Unknown.'));
}
if (!key_exists('password', $service)) {
throw new ErrorException(\Yii::t('sms', 'Password Unknown.'));
}
if (!key_exists('order', $service)) {
throw new ErrorException(\Yii::t('sms', 'Order Unknown.'));
}
if (!class_exists($service['class'])) {
throw new ErrorException(\Yii::t('sms', 'Service not found.'));
}
if ($service['class'] instanceof AbstractService) {
throw new ErrorException(\Yii::t('sms', 'Class must be inherited from the AbstractService.'));
}
}
ArrayHelper::multisort($this->services, 'order');
$this->availableServices = $this->services;
$this->currentServiceName = array_keys($this->availableServices)[0];
$this->currentService = $this->getServiceByName($this->currentServiceName);
}
/**
* Include translation messages for component
*/
public static function registerTranslations()
{
if (!isset(\Yii::$app->i18n->translations['sms']) && !isset(\Yii::$app->i18n->translations['sms/*'])) {
\Yii::$app->i18n->translations['sms'] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@vendor/lowbase/yii2-sms/messages',
'forceTranslation' => true,
'fileMap' => [
'sms' => 'sms.php'
]
];
}
}
/**
* Send Sms and put info into database
*
* @param $phone
* @param $text
* @param bool $saveInfo
* @param null $type
* @param null $forUserId
* @param null $mustSentAt
* @param array $options
* @return bool
*/
public function sendSms($phone, $text, $saveInfo = true, $type = null, $forUserId = null, $mustSentAt = null, $options = [])
{
$model = new smsModel([
'phone' => $phone,
'text' => $text,
'provider' => $this->currentServiceName,
'type' => $type,
'for_user_id' => $forUserId,
'must_sent_at' => $mustSentAt
]);
if ($model->validate()) {
$result = $this->currentService->sendSms($phone, $text, $mustSentAt, $options);
if ($saveInfo) {
$model->status = $result['status'];
$model->provider_answer = $result['answer'];
if (isset($result['id'])) {
$model->provider_sms_id = $result['id'];
}
$model->save();
}
unset($this->availableServices[$this->currentServiceName]);
while ($result['status'] === smsModel::STATUS_FAILED && $this->cascade && count($this->availableServices)) {
// retry send sms with new Service
$currentServiceName = array_keys($this->availableServices)[0];
$this->useService($currentServiceName);
$result['status'] = $this->sendSms($phone, $text, $saveInfo, $type, $forUserId, $mustSentAt, $options);
}
return $result['status'];
}
return false;
}
/**
* Get sms status by my id (from database)
*
* @param $id
* @param bool $saveInfo
* @param array $options
* @return bool|integer
* @throws ErrorException
*/
public function getSmsStatusById($id, $saveInfo = true, $options = [])
{
$model = smsModel::findOne($id);
if ($model === null || $model->provider_sms_id === null) {
return false;
}
$this->currentService = $this->getServiceByName($model->provider);
$result = $this->currentService->getSmsStatus($model->provider_sms_id, array_merge(['phone' => $model->phone], $options));
if ($saveInfo) {
$model->status = $result['status'];
$model->provider_answer = $result['answer'];
$model->save();
}
return $result['status'];
}
/**
* Get sms status by provider id
*
* Some service need phone in options for status!!!
*
* @param $providerSmsId
* @param array $options
* @return integer
*/
public function getSmsStatusByProviderId($providerSmsId, $options = [])
{
$result = $this->currentService->getSmsStatus($providerSmsId, $options);
return $result['status'];
}
/**
* Get account status
*
* @param array $options
* @return bool|float
*/
public function getBalance($options = [])
{
return $this->currentService->getBalance($options);
}
/**
* Get service by name
*
* @param $serviceName
* @return object
* @throws ErrorException
*/
public function getServiceByName($serviceName)
{
if (in_array($serviceName, array_keys($this->services))) {
$service = $this->services[$serviceName];
return \Yii::createObject([
'class' => $service['class'],
'login' => $service['login'],
'password' => $service['password'],
]);
} else {
throw new ErrorException(\Yii::t('sms', 'Service not found.'));
}
}
/**
* Use specific service
*
* @param $serviceName
* @return $this
* @throws ErrorException
*/
public function useService($serviceName)
{
$this->currentService = $this->getServiceByName($serviceName);
$this->currentServiceName = $serviceName;
return $this;
}
/**
* Set services
*
* @param $services
*/
public function setServices($services)
{
$this->services = $services;
}
/**
* Set cascade
*
* @param $cascade
*/
public function setCascade($cascade)
{
$this->cascade = $cascade;
}
}