RequestData::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 vxm\gatewayclients\RequestData as BaseRequestData;
12
13
/**
14
 * Lớp RequestData dùng để tạo các đối tượng tổng hợp dữ liệu đã kiểm tra tính trọn vẹn,
15
 * dùng để gửi yêu cầu thực thi đến eSMS.
16
 *
17
 * @property Client $client
18
 * @author Vuong Minh <[email protected]>
19
 * @since 1.0
20
 */
21
class RequestData extends BaseRequestData
22
{
23
24
    /**
25
     * @inheritdoc
26
     */
27 7
    public function rules()
28
    {
29
        return [
30 7
            [['ApiKey', 'SecretKey'], 'required', 'on' => [
31
                Gateway::RC_SEND_VOICE, Gateway::RC_SEND_SMS, Gateway::RC_GET_SEND_STATUS, Gateway::RC_GET_RECEIVER_STATUS
32
            ]],
33
            [['Phone'], 'required', 'on' => [Gateway::RC_SEND_VOICE, Gateway::RC_SEND_SMS]],
34
            [['SmsType', 'Content'], 'required', 'on' => Gateway::RC_SEND_SMS],
35
            [['ApiCode', 'PassCode'], 'required', 'on' => Gateway::RC_SEND_VOICE],
36
            [['RefId'], 'required', 'on' => [Gateway::RC_GET_SEND_STATUS, Gateway::RC_GET_RECEIVER_STATUS]]
37
        ];
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 7
    public function ensureAttributes(array &$attributes)
44
    {
45 7
        $command = $this->getCommand();
46 7
        if ($command !== Gateway::RC_GET_BALANCE) {
47 6
            $attributes['ApiKey'] = $this->client->apiKey;
48 6
            $attributes['SecretKey'] = $this->client->secretKey;
49
        }
50
51 7
        if ($command === Gateway::RC_SEND_SMS) {
52 2
            $attributes['SmsType'] = $attributes['SmsType'] ?? 4;
53 2
            $attributes['IsUnicode'] = $attributes['IsUnicode'] ?? 1;
54 2
            $attributes['Sandbox'] = $attributes['Sandbox'] ?? 0;
55
56 2
            if ($attributes['SmsType'] === 1 || $attributes['SmsType'] === 2) {
57
                $this->addRule(['Brandname'], 'required', ['on' => $command]);
58
            }
59
        }
60
61 7
        parent::ensureAttributes($attributes);
62 7
    }
63
}
64