Gateway::sendVoice()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
/**
3
 * @link https://github.com/yiiviet/yii2-esms
4
 * @copyright Copyright (c) 2017 Yii Viet
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
9
namespace yiiviet\esms;
10
11
use yii\httpclient\Client as HttpClient;
12
13
use GatewayClients\DataInterface;
14
15
use vxm\gatewayclients\RequestData as BaseRequestData;
16
use vxm\gatewayclients\BaseGateway;
17
use vxm\gatewayclients\RequestEvent;
18
19
20
/**
21
 * Lớp Gateway kế thừa và thực thi [[\\vxm\\gatewayclients\\BaseGateway]] cung cấp các phương thức hổ trợ giao tiếp với eSMS
22
 * như gửi sms/voice call, kiểm tra trạng thái, số dư. Hiện tại nó hổ trợ 100% tính năng từ eSMS API.
23
 *
24
 * @author Vuong Minh <[email protected]>
25
 * @since 1.0
26
 */
27
class Gateway extends BaseGateway
28
{
29
    /**
30
     * Lệnh thực thi gửi sms.
31
     */
32
    const RC_SEND_SMS = 'sendSMS';
33
34
    /**
35
     * Lệnh thực thi gửi voice call.
36
     */
37
    const RC_SEND_VOICE = 'sendVoice';
38
39
    /**
40
     * Lệnh thực thi lấy số dư tài khoản của `client`.
41
     */
42
    const RC_GET_BALANCE = 'getBalance';
43
44
    /**
45
     * Lệnh thực thi kiểm tra trang thái sms/voice call (tổng quan bao nhiều người nhận, số tin đã gửi v..v).
46
     */
47
    const RC_GET_SEND_STATUS = 'getSendStatus';
48
49
    /**
50
     * Lệnh thực thi kiểm tra trang thái sms/voice call (chi tiết trên từng số điện thoại).
51
     */
52
    const RC_GET_RECEIVER_STATUS = 'getReceiverStatus';
53
54
    /**
55
     * API endpoint dùng để lấy số dư.
56
     */
57
    const BALANCE_URL = 'GetBalance';
58
59
    /**
60
     * API endpoint dùng để gửi sms.
61
     */
62
    const SEND_SMS_URL = 'SendMultipleMessage_V4_get';
63
64
    /**
65
     * Full API endpoint dùng để gửi voice call.
66
     */
67
    const SEND_VOICE_FULL_URL = 'http://voiceapi.esms.vn/MainService.svc/json/MakeCall';
68
69
    /**
70
     * API endpoint dùng để kiểm tra trang thái sms/voice call (tổng quan bao nhiều người nhận, số tin đã gửi v..v).
71
     */
72
    const GET_SEND_STATUS_URL = 'GetSendStatus';
73
74
    /**
75
     * API endpoint dùng để kiểm tra trạng thái sms/voice call (chi tiết trên từng số điện thoại).
76
     */
77
    const GET_RECEIVER_STATUS_URL = 'GetSmsReceiverStatus_get';
78
79
    /**
80
     * @event [[RequestEvent]] sự kiện diễn ra trước khi gửi sms.
81
     */
82
    const EVENT_BEFORE_SEND_SMS = 'beforeSendSMS';
83
84
    /**
85
     * @event [[RequestEvent]] sự kiện diễn ra sau khi gửi sms.
86
     */
87
    const EVENT_AFTER_SEND_SMS = 'afterSendSMS';
88
89
    /**
90
     * @event [[RequestEvent]] sự kiện diễn ra trước khi gửi voice call.
91
     */
92
    const EVENT_BEFORE_SEND_VOICE = 'beforeSendVoice';
93
94
    /**
95
     * @event [[RequestEvent]] sự kiện diễn ra sau khi gửi voice call.
96
     */
97
    const EVENT_AFTER_SEND_VOICE = 'afterSendVoice';
98
99
    /**
100
     * @event [[RequestEvent]] sự kiện diễn ra trước khi lấy thông tin số dư.
101
     */
102
    const EVENT_BEFORE_GET_BALANCE = 'beforeGetBalance';
103
104
    /**
105
     * @event [[RequestEvent]] sự kiện diễn ra sau khi lấy thông tin số dư.
106
     */
107
    const EVENT_AFTER_GET_BALANCE = 'afterGetBalance';
108
109
    /**
110
     * @event [[RequestEvent]] sự kiện diễn ra trước khi lấy trạng thái tổng quan.
111
     */
112
    const EVENT_BEFORE_GET_SEND_STATUS = 'beforeGetSendStatus';
113
114
    /**
115
     * @event [[RequestEvent]] sự kiện diễn ra sau khi lấy trạng thái tổng quan.
116
     */
117
    const EVENT_AFTER_GET_SEND_STATUS = 'afterGetSendStatus';
118
119
    /**
120
     * @event [[RequestEvent]] sự kiện diễn ra trước khi lấy trạng thái chi tiết.
121
     */
122
    const EVENT_BEFORE_GET_RECEIVER_STATUS = 'beforeGetReceiverStatus';
123
124
    /**
125
     * @event [[RequestEvent]] sự kiện diễn ra sau lấy trạng thái chi tiết.
126
     */
127
    const EVENT_AFTER_GET_RECEIVER_STATUS = 'afterGetReceiverStatus';
128
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public $clientConfig = ['class' => Client::class];
134
135
    /**
136
     * @inheritdoc
137
     */
138
    public $requestDataConfig = ['class' => RequestData::class];
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public $responseDataConfig = ['class' => ResponseData::class];
144
145
    /**
146
     * @inheritdoc
147
     */
148 7
    public function getBaseUrl(): string
149
    {
150 7
        return 'http://rest.esms.vn/MainService.svc/json';
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156 7 View Code Duplication
    public function beforeRequest(RequestEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158 7
        switch ($event->requestData->getCommand()) {
159 7
            case self::RC_SEND_SMS:
160 2
                $this->trigger(self::EVENT_BEFORE_SEND_SMS);
161 2
                break;
162 5
            case self::RC_SEND_VOICE:
163 2
                $this->trigger(self::EVENT_BEFORE_SEND_VOICE);
164 2
                break;
165 3
            case self::RC_GET_BALANCE:
166 1
                $this->trigger(self::EVENT_BEFORE_GET_BALANCE);
167 1
                break;
168 2
            case self::RC_GET_SEND_STATUS:
169 1
                $this->trigger(self::EVENT_BEFORE_GET_SEND_STATUS);
170 1
                break;
171 1
            case self::RC_GET_RECEIVER_STATUS:
172 1
                $this->trigger(self::EVENT_BEFORE_GET_RECEIVER_STATUS);
173 1
                break;
174
            default:
175
                break;
176
        }
177
178 7
        parent::beforeRequest($event);
179 7
    }
180
181
    /**
182
     * @inheritdoc
183
     */
184 5 View Code Duplication
    public function afterRequest(RequestEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
    {
186 5
        switch ($event->requestData->getCommand()) {
187 5
            case self::RC_SEND_SMS:
188 1
                $this->trigger(self::EVENT_AFTER_SEND_SMS);
189 1
                break;
190 4
            case self::RC_SEND_VOICE:
191 1
                $this->trigger(self::EVENT_AFTER_SEND_VOICE);
192 1
                break;
193 3
            case self::RC_GET_BALANCE:
194 1
                $this->trigger(self::EVENT_AFTER_GET_BALANCE);
195 1
                break;
196 2
            case self::RC_GET_SEND_STATUS:
197 1
                $this->trigger(self::EVENT_AFTER_GET_SEND_STATUS);
198 1
                break;
199 1
            case self::RC_GET_RECEIVER_STATUS:
200 1
                $this->trigger(self::EVENT_AFTER_GET_RECEIVER_STATUS);
201 1
                break;
202
            default:
203
                break;
204
        }
205
206 5
        parent::afterRequest($event);
207 5
    }
208
209
    /**
210
     * Phương thức thực thi gửi sms.
211
     * Nó chính là phương thức ánh xạ của [[request()]] thực hiện lệnh [[RC_SEND_SMS]]
212
     *
213
     * @param array $data Mảng dữ liệu eSMS yêu cầu đề gửi sms.
214
     * @param null $clientId Client thực thi lệnh. Nếu không thiết lập giá trị của [[getDefaultClient()]] sẽ được sử dụng.
215
     * @return ResponseData|DataInterface Dữ liệu phản hồi từ eSMS.
216
     * @throws \ReflectionException|\yii\base\InvalidConfigException
217
     */
218 2
    public function sendSMS(array $data, $clientId = null): DataInterface
219
    {
220 2
        return $this->request(self::RC_SEND_SMS, $data, $clientId);
221
    }
222
223
    /**
224
     * Phương thức thực thi gửi voice call.
225
     * Nó chính là phương thức ánh xạ của [[request()]] thực hiện lệnh [[RC_SEND_VOICE]]
226
     *
227
     * @param array $data Mảng dữ liệu eSMS yêu cầu đề gửi voice call.
228
     * @param null $clientId Client thực thi lệnh. Nếu không thiết lập giá trị của [[getDefaultClient()]] sẽ được sử dụng.
229
     * @return ResponseData|DataInterface Dữ liệu phản hồi từ eSMS.
230
     * @throws \ReflectionException|\yii\base\InvalidConfigException
231
     */
232 2
    public function sendVoice(array $data, $clientId = null): DataInterface
233
    {
234 2
        return $this->request(self::RC_SEND_VOICE, $data, $clientId);
235
    }
236
237
    /**
238
     * Phương thức thực thi lấy số dư tài khoản `client`.
239
     * Nó chính là phương thức ánh xạ của [[request()]] thực hiện lệnh [[RC_GET_BALANCE]]
240
     *
241
     * @param array $data Mảng dữ liệu eSMS yêu cầu đề lấy số dư.
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
242
     * @param null $clientId Client thực thi lệnh. Nếu không thiết lập giá trị của [[getDefaultClient()]] sẽ được sử dụng.
243
     * @return ResponseData|DataInterface Dữ liệu phản hồi từ eSMS.
244
     * @throws \ReflectionException|\yii\base\InvalidConfigException
245
     */
246 1
    public function getBalance($clientId = null): DataInterface
247
    {
248 1
        return $this->request(self::RC_GET_BALANCE, [], $clientId);
249
    }
250
251
    /**
252
     * Phương thức thực thi kiểm tra tổng quan trạng thái nhận tin (sms/voice call).
253
     * Nó chính là phương thức ánh xạ của [[request()]] thực hiện lệnh [[RC_GET_SEND_STATUS]]
254
     *
255
     * @param array $data Mảng dữ liệu eSMS yêu cầu để thực hiện kiểm tra.
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
256
     * @param null $clientId Client thực thi lệnh. Nếu không thiết lập giá trị của [[getDefaultClient()]] sẽ được sử dụng.
257
     * @return ResponseData|DataInterface Dữ liệu phản hồi từ eSMS.
258
     * @throws \ReflectionException|\yii\base\InvalidConfigException
259
     */
260 1
    public function getSendStatus($refId, $clientId = null): DataInterface
261
    {
262 1
        $data = ['RefId' => $refId];
263
264 1
        return $this->request(self::RC_GET_SEND_STATUS, $data, $clientId);
265
    }
266
267
    /**
268
     * Phương thức thực thi kiểm tra chi tiết trạng thái đã nhận tin (sms/voice call) trên từng số điện thoại.
269
     * Nó chính là phương thức ánh xạ của [[request()]] thực hiện lệnh [[RC_GET_RECEIVER_STATUS]]
270
     *
271
     * @param array $data Mảng dữ liệu eSMS yêu cầu để thực hiện kiểm tra.
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
272
     * @param null $clientId Client thực thi lệnh. Nếu không thiết lập giá trị của [[getDefaultClient()]] sẽ được sử dụng.
273
     * @return ResponseData|DataInterface Dữ liệu phản hồi từ eSMS.
274
     * @throws \ReflectionException|\yii\base\InvalidConfigException
275
     */
276 1
    public function getReceiverStatus($refId, $clientId = null): DataInterface
277
    {
278 1
        $data = ['RefId' => $refId];
279
280 1
        return $this->request(self::RC_GET_RECEIVER_STATUS, $data, $clientId);
281
    }
282
283
    /**
284
     * @inheritdoc
285
     * @throws \yii\base\InvalidConfigException
286
     */
287 7
    protected function requestInternal(BaseRequestData $requestData, HttpClient $httpClient): array
288
    {
289
        /** @var Client $client */
290 7
        $client = $requestData->getClient();
291 7
        $command = $requestData->getCommand();
292
        $commandUrls = [
293 7
            self::RC_GET_BALANCE => self::BALANCE_URL,
294 7
            self::RC_GET_SEND_STATUS => self::GET_SEND_STATUS_URL,
295 7
            self::RC_GET_RECEIVER_STATUS => self::GET_RECEIVER_STATUS_URL,
296 7
            self::RC_SEND_SMS => self::SEND_SMS_URL,
297 7
            self::RC_SEND_VOICE => '',
298
        ];
299 7
        $url = $commandUrls[$command];
300 7
        $baseUrl = $httpClient->baseUrl;
301
302 7
        if ($command === self::RC_SEND_VOICE) {
303 2
            $httpClient->baseUrl = self::SEND_VOICE_FULL_URL;
304 5
        } elseif ($command === self::RC_GET_BALANCE) {
305 1
            $url .= "/{$client->apiKey}/{$client->secretKey}";
306
        }
307
308 7
        $data = $requestData->get();
309 5
        $data[0] = $url;
310 5
        $responseData = $httpClient->get($data)->send()->getData();
311 5
        $httpClient->baseUrl = $baseUrl;
312
313 5
        return $responseData;
314
    }
315
316
}
317