Skip to content

Commit 410b5f4

Browse files
committed
make refactoring code
1 parent fcf2193 commit 410b5f4

File tree

4 files changed

+183
-39
lines changed

4 files changed

+183
-39
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ or add
2121

2222
to the require section of your `composer.json` file.
2323

24+
Subsequently, run
25+
26+
```
27+
./yii migrate/up --migrationPath=@vendor/avator/yii2-turbosms/migrations
28+
```
29+
2430
## Basic setup
2531

26-
You should have registered account at http://turbosms.ua/
32+
You should:
33+
* registered account at http://turbosms.ua/
34+
* add sender in page https://turbosms.ua/sign/add.html
35+
* create login and password for soap api in page https://turbosms.ua/route.html
2736

2837
### Configuration
2938

@@ -32,22 +41,22 @@ Add the following in your config:
3241
```php
3342
<?php
3443
...
35-
'components'=>array(
44+
'components' => [
3645
'turbosms' => [
3746
'class' => 'avator\turbosms\Turbosms',
3847
'sender' => 'your_sender',
3948
'login' => 'your_login',
4049
'password' => 'your_password',
4150
],
4251
...
43-
),
52+
],
4453
...
4554
```
4655
If you want test sms in debug mode change config:
4756
```php
4857
<?php
4958
...
50-
'components'=>array(
59+
'components' => [
5160
'turbosms' => [
5261
'class' => 'avator\turbosms\Turbosms',
5362
'sender' => 'your_sender',
@@ -56,7 +65,7 @@ If you want test sms in debug mode change config:
5665
'debug' => true,
5766
],
5867
...
59-
),
68+
],
6069
...
6170
```
6271
in debug mode sms not send only add to db table.
@@ -70,6 +79,12 @@ Once the extension is installed, simply use it in your code by :
7079
<?php Yii::$app->turbosms->send('test', '+380XXXXXXXXX'); ?>
7180
```
7281

82+
TODO
83+
-----
84+
* Translate message
85+
* Down migration
86+
* Save log to file
87+
7388
## License
7489

7590
**yii2-turbosms** is released under the BSD 3-Clause License. See the bundled `LICENSE.md` for details.

Turbosms.php

Lines changed: 124 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212
*
1313
* @author AVATOR (Oleksii Golub) <sclub2018@yandex.ua>
1414
* @since 1.0
15-
*
1615
*/
1716
class Turbosms extends Component
1817
{
1918

2019
/**
20+
* Soap login
21+
*
2122
* @var string
2223
*/
2324
public $login;
2425

2526
/**
27+
* Soap password
28+
*
2629
* @var string
2730
*/
2831
public $password;
@@ -33,58 +36,87 @@ class Turbosms extends Component
3336
public $sender;
3437

3538
/**
39+
* Debug mode
40+
*
3641
* @var bool
3742
*/
3843
public $debug = false;
3944

45+
/**
46+
* @var SoapClient
47+
*/
4048
protected $client;
4149

4250
/**
51+
* Wsdl url
52+
*
4353
* @var string
4454
*/
4555
protected $wsdl = 'http://turbosms.in.ua/api/wsdl.html';
4656

57+
/**
58+
* Debug suffix message
59+
*
60+
* @var string
61+
*/
62+
public $debugSuffixMessage = ' (тестовый режим)';
63+
64+
/**
65+
* Success message
66+
*
67+
* @var string
68+
*/
69+
public $successMessage = 'Сообщения успешно отправлено';
70+
71+
/**
72+
* Error message
73+
*
74+
* @var string
75+
*/
76+
public $errorMessage = 'Сообщения не отправлено (ошибка: "%error%")';
77+
78+
/**
79+
* Save to db log
80+
*
81+
* @var bool
82+
*/
83+
public $saveToDb = true;
84+
85+
/**
86+
* @var int
87+
*/
88+
protected $sendStatus = 1;
89+
4790
/**
4891
* Send sms
4992
*
50-
* @param $text
93+
* @param string $text
5194
* @param $phones
5295
*
5396
* @throws InvalidConfigException
5497
*/
55-
public function send($text, $phones) {
56-
57-
if (!$this->debug || !$this->client) {
58-
$this->connect();
59-
}
60-
98+
public function send($text, $phones)
99+
{
61100
if (!is_array($phones)) {
62101
$phones = [$phones];
63102
}
64103
foreach ($phones as $phone) {
65-
66-
$message = 'Сообщения успешно отправлено';
67-
if (!$this->debug) {
68-
$result = $this->client->SendSMS([
69-
'sender' => $this->sender,
70-
'destination' => $phone,
71-
'text' => $text
72-
]);
73-
74-
if ($result->SendSMSResult->ResultArray[0] != 'Сообщения успешно отправлены') {
75-
$message = 'Сообщения не отправлено (ошибка: "' . $result->SendSMSResult->ResultArray[0] . '")';
76-
}
104+
if (!$phone) {
105+
continue;
77106
}
78-
107+
$message = $this->sendMessage($text, $phone);
79108
$this->saveToDb($text, $phone, $message);
80109
}
81110
}
82111

83112
/**
113+
* Connetc to Turbosms by Soap
114+
*
84115
* @return SoapClient
85116
* @throws InvalidConfigException
86117
*/
87-
protected function connect() {
118+
protected function connect()
119+
{
88120

89121
if ($this->client) {
90122
return $this->client;
@@ -113,36 +145,95 @@ protected function connect() {
113145
/**
114146
* Save sms to db
115147
*
116-
* @param $text
117-
* @param $phone
118-
* @param $message
148+
* @param string $text
149+
* @param string $phone
150+
* @param string $message
151+
*
152+
* @return bool
119153
*/
120-
public function saveToDb($text, $phone, $message) {
154+
public function saveToDb($text, $phone, $message)
155+
{
156+
if (!$this->saveToDb) {
157+
return false;
158+
}
159+
121160
$model = new TurboSmsSent();
122161
$model->text = $text;
123162
$model->phone = $phone;
124-
$model->status = $message . ($this->debug ? ' (тестовый режим)' : '');
163+
$model->message = $message . ($this->debug ? $this->debugSuffixMessage : '');
164+
$model->status = $this->sendStatus;
125165
$model->save();
166+
167+
return true;
126168
}
127169

128170
/**
129171
* Get balance
130172
*
131173
* @return int
132174
*/
133-
public function getBalance() {
134-
$result = $this->client->GetCreditBalance();
135-
return intval($result->GetCreditBalanceResult);
175+
public function getBalance()
176+
{
177+
return $this->debug ? 0 : intval($this->getClient()->GetCreditBalance()->GetCreditBalanceResult);
136178
}
137179

138180
/**
181+
* Get message status
182+
*
139183
* @param $messageId
140184
*
141-
* @return mixed
185+
* @return string
142186
*/
143-
public function getMessageStatus($messageId) {
144-
$result = $this->client->GetMessageStatus(['MessageId' => $messageId]);
187+
public function getMessageStatus($messageId)
188+
{
189+
if ($this->debug || !$messageId) {
190+
return'';
191+
}
192+
$result = $this->getClient()->GetMessageStatus(['MessageId' => $messageId]);
145193
return $result->GetMessageStatusResult;
146194
}
147195

196+
/**
197+
* Get Soap client
198+
*
199+
* @return SoapClient
200+
* @throws InvalidConfigException
201+
*/
202+
protected function getClient()
203+
{
204+
if (!$this->client) {
205+
return $this->connect();
206+
}
207+
return $this->client;
208+
}
209+
210+
/**
211+
* @param $text
212+
* @param $phone
213+
* @return string
214+
*/
215+
protected function sendMessage($text, $phone)
216+
{
217+
$message = $this->successMessage;
218+
// set default status
219+
$this->sendStatus = 1;
220+
if ($this->debug) {
221+
return $message;
222+
}
223+
224+
$result = $this->getClient()->SendSMS([
225+
'sender' => $this->sender,
226+
'destination' => $phone,
227+
'text' => $text
228+
]);
229+
230+
if (empty($result->SendSMSResult->ResultArray[0]) ||
231+
$result->SendSMSResult->ResultArray[0] != 'Сообщения успешно отправлены') {
232+
$this->sendStatus = 0;
233+
$message = preg_replace('/%error%/i', $result->SendSMSResult->ResultArray, $this->errorMessage);
234+
}
235+
236+
return $message;
237+
}
238+
148239
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use yii\db\Schema;
4+
use yii\db\Migration;
5+
6+
class m160117_203749_rename_and_create_field extends Migration
7+
{
8+
public function up()
9+
{
10+
$this->renameColumn('{{%turbo_sms_sent}}', 'status', 'message');
11+
$this->addColumn('{{%turbo_sms_sent}}', 'status', Schema::TYPE_SMALLINT);
12+
13+
}
14+
15+
public function down()
16+
{
17+
echo "m160117_203749_rename_and_create_field cannot be reverted.\n";
18+
19+
return false;
20+
}
21+
22+
/*
23+
// Use safeUp/safeDown to run migration code within a transaction
24+
public function safeUp()
25+
{
26+
}
27+
28+
public function safeDown()
29+
{
30+
}
31+
*/
32+
}

models/TurboSmsSent.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
* @property string $date_sent
1212
* @property string $text
1313
* @property string $phone
14+
* @property string $message
1415
* @property string $status
1516
*/
1617
class TurboSmsSent extends \yii\db\ActiveRecord
1718
{
19+
const STATUS_SUCCESS = 1;
20+
const STATUS_ERROR = 0;
21+
1822
/**
1923
* @inheritdoc
2024
*/
@@ -31,7 +35,8 @@ public function rules()
3135
return [
3236
[['date_sent'], 'safe'],
3337
[['text'], 'string'],
34-
[['phone', 'status'], 'string', 'max' => 255]
38+
[['phone', 'message'], 'string', 'max' => 255],
39+
[['phone', 'status'], 'integer']
3540
];
3641
}
3742

@@ -45,6 +50,7 @@ public function attributeLabels()
4550
'date_sent' => Yii::t('app', 'Date Sent'),
4651
'text' => Yii::t('app', 'Text'),
4752
'phone' => Yii::t('app', 'Phone'),
53+
'message' => Yii::t('app', 'Message'),
4854
'status' => Yii::t('app', 'Status'),
4955
];
5056
}

0 commit comments

Comments
 (0)