-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYiiSMS.php
66 lines (47 loc) · 1.65 KB
/
YiiSMS.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
<?php
Yii::import('application.modules.oi.extensions.yii-sms.*');
Yii::import('application.modules.oi.extensions.yii-sms.providers.*');
class YiiSMS extends CApplicationComponent {
public $dryRun=false;
public $defaultProvider=null;
public $providers=array();
public $afterSend=null;
private $_providers=array();
public function init() {
if ($this->defaultProvider===null) {
throw new CHttpException(500,'Please set the default SMS provider you want to use.');
}
if (count($this->providers)===0) {
throw new CHttpException(500,'Please define a SMS provider.');
}
foreach ($this->providers as $name => $config) {
$config['class']="{$name}SMSProvider";
$this->_providers[$name]=Yii::createComponent($config);
}
parent::init();
}
public function send($to, $text, $extra=array()) {
$this->getProvider()->lastErrorMessage=null;
if ($this->dryRun) {
$sent=true;
} else {
$sent=$this->getProvider()->sendSMS($to, $text);
}
Yii::log("Send SMS to {$to}: $text", CLogger::LEVEL_TRACE, 'sms');
if ($this->afterSend !== null) {
call_user_func_array($this->afterSend, array($to, $text, $sent, $this->getProvider()->lastErrorMessage, $extra));
}
return $sent;
}
public function getLastErrorMessage() {
$result=$this->getProvider()->lastErrorMessage;
return $result;
}
/**
* Returns the SMS provider
* @return BaseSMSProvider
*/
private function getProvider() {
return $this->_providers[$this->defaultProvider];
}
}