Completed
Push — master ( 5ac6b4...cc61cd )
by Vuong
02:04
created

BankWidget::getBankProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 9
cp 0.7778
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.0987
1
<?php
2
/**
3
 * @link https://github.com/yiiviet/yii2-payment
4
 * @copyright Copyright (c) 2017 Yii Viet
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace yiiviet\payment;
9
10
use Yii;
11
12
use yii\base\NotSupportedException;
13
use yii\di\Instance;
14
use yii\helpers\Html;
15
use yii\widgets\InputWidget;
16
17
use vxm\gatewayclients\BaseGateway;
18
19
/**
20
 * Lớp BaseWidget hổ trợ render input danh sách ngân hàng cho user chọn hiện tại hổ trợ 2 định kiểu là radio list và dropdown list.
21
 *
22
 * @property BankProvider $bankProvider đối tượng cung cấp thông tin ngân hàng
23
 *
24
 * @author Vuong Minh <[email protected]>
25
 * @since 1.0.3
26
 */
27
class BankWidget extends InputWidget
28
{
29
30
    /**
31
     * @var BaseGateway đối tượng cổng thanh toán
32
     */
33
    public $gateway;
34
35
    /**
36
     * @var array có khóa là lớp đối tượng cổng thanh toán và giá trị là lớp đối tượng cung cấp thông tin ngân hàng.
37
     */
38
    public $providerClasses = [];
39
40
    /**
41
     * @var array hổ trợ thiết lập giá trị của đối tượng cung cấp thông tin ngân hàng như chỉ định chỉ lấy danh sách bank qr code, offline, online...
42
     */
43
    public $providerConfig = [];
44
45
    /**
46
     * @inheritdoc
47
     * @throws \yii\base\InvalidConfigException
48
     */
49 3
    public function init()
50
    {
51 3
        parent::init();
52
53 3
        $this->gateway = Instance::ensure($this->gateway, PaymentGatewayInterface::class);
54 3
        $this->providerClasses = array_merge($this->defaultProviderClasses(), $this->providerClasses);
55 3
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 1
    public function run()
61
    {
62 1
        if ($this->hasModel()) {
63
64
            return Html::activeDropDownList($this->model, $this->attribute, $this->bankProvider->banks(), $this->options);
65
        } else {
66
67 1
            return Html::dropDownList($this->name, $this->value, $this->bankProvider->banks(), $this->options);
68
        }
69
    }
70
71
    /**
72
     * @var null|BankProvider đối tượng cung cấp dữ liệu ngân hàng
73
     * @see [[getBankProvider()]]
74
     */
75
    private $_provider;
76
77
    /**
78
     * Phương thức hổ trợ lấy đối tượng cung cấp dữ liệu ngân hàng để lấy thông tin hiển thị.
79
     *
80
     * @return BankProvider|object
81
     * @throws \yii\base\InvalidConfigException|NotSupportedException
82
     */
83 3
    public function getBankProvider(): BankProvider
84
    {
85 3
        if (!$this->_provider instanceof BankProvider) {
86 3
            $gatewayClass = get_class($this->gateway);
87
88 3
            if (isset($this->providerClasses[$gatewayClass])) {
89 3
                $config = array_merge($this->providerConfig, [
90 3
                    'class' => $this->providerClasses[$gatewayClass]
91
                ]);
92
                /** @var BankProvider $provider */
93 3
                return $this->_provider = Yii::createObject($config, [$this->gateway]);
94
            } else {
95
                throw new NotSupportedException("Gateway: `$gatewayClass` is not supported!");
96
            }
97
        } else {
98
            return $this->_provider;
99
        }
100
    }
101
102
    /**
103
     * Phương thức cung cấp các lớp đối tượng lấy thông tin ngân hàng mặc định.
104
     *
105
     * @return array có khóa là lớp đối tượng cổng thanh toán và giá trị là lớp đối tượng cung cấp thông tin ngân hàng.
106
     */
107 3
    protected function defaultProviderClasses(): array
108
    {
109
        return [
110 3
            'yiiviet\payment\baokim\PaymentGateway' => 'yiiviet\payment\baokim\BankProvider'
111
        ];
112
    }
113
}
114