1212 *
1313 * @author AVATOR (Oleksii Golub) <sclub2018@yandex.ua>
1414 * @since 1.0
15- *
1615 */
1716class 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}
0 commit comments