Completed
Push — master ( 035046...524244 )
by Vuong
02:35
created

MagicPropertiesTrait::getResponseCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
/**
3
 * @link https://github.com/yiiviet/yii2-payment
4
 * @copyright Copyright (c) 2017 Yii2VN
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace yiiviet\payment\onepay;
9
10
/**
11
 * Trait MagicPropertiesTrait là trait bổ sung phương thức getter và setter nhầm giảm hóa sự lập đi lập lại của prefix `vpc_`
12
 *
13
 * @property string|null $Message thông báo từ VNPayment.
14
 * @property int|null $ResponseCode mã phản hồi.
15
 *
16
 * @author Vuong Minh <[email protected]>
17
 * @since 1.0
18
 */
19
trait MagicPropertiesTrait
20
{
21
    /**
22
     * @inheritdoc
23
     * @throws \yii\base\UnknownPropertyException
24
     */
25 6
    public function __get($name)
26
    {
27
        try {
28 6
            return parent::__get($name);
29
        } catch (\yii\base\UnknownPropertyException $e) {
30
            if (isset($this["vpc_$name"])) {
31
                return $this["vpc_$name"];
32
            } else {
33
                throw $e;
34
            }
35
        }
36
    }
37
38
    /**
39
     * @inheritdoc
40
     * @throws \yii\base\UnknownPropertyException
41
     */
42
    public function __set($name, $value)
43
    {
44
        try {
45
            parent::__set($name, $value);
46
        } catch (\yii\base\UnknownPropertyException $e) {
47
            if (isset($this["vpc_$name"])) {
48
                $this["vpc_$name"] = $value;
49
            } else {
50
                throw $e;
51
            }
52
        }
53
    }
54
55
    /**
56
     * Phương thức hổ trợ lấy `vpc_Message` nhận từ OnePay.
57
     *
58
     * @return null|string Trả về NULL nếu như dữ liệu OnePay gửi về không tồn tại `vpc_Message` và ngược lại.
59
     */
60
    public function getMessage(): ?string
61
    {
62
        if (isset($this['vpc_Message'])) {
63
            return $this['vpc_Message'];
64
        } else {
65
            return null;
66
        }
67
    }
68
69
    /**
70
     * Phương thức hổ trợ lấy response code từ OnePay,
71
     * do tùy theo lệnh mà giá trị này nằm ở các attribute phản hồi khác nhau nên phương thức này sẽ tự động xác định.
72
     *
73
     * @return int|null Trả về NULL nếu như không có response code và ngược lại
74
     */
75
    public function getResponseCode(): ?int
76
    {
77
        if (isset($this['vpc_TxnResponseCode'])) {
78
            return $this['vpc_TxnResponseCode'];
79
        } elseif (isset($this['vpc_ResponseCode'])) {
80
            return $this['vpc_ResponseCode'];
81
        } else {
82
            return null;
83
        }
84
    }
85
86
}
87