Completed
Push — master ( 3ced2f...434446 )
by Vuong
01:47
created

BankWidget::defaultProviderClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\widgets\InputWidget;
15
16
use vxm\gatewayclients\BaseGateway;
17
18
/**
19
 * 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.
20
 *
21
 * @author Vuong Minh <[email protected]>
22
 * @since 1.0.3
23
 */
24
abstract class BankWidget extends InputWidget
25
{
26
27
    /**
28
     * @var BaseGateway đối tượng cổng thanh toán
29
     */
30
    public $gateway;
31
32
    /**
33
     * @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.
34
     */
35
    public $providerClasses = [];
36
37
    /**
38
     * @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...
39
     */
40
    public $providerConfig = [];
41
42
    /**
43
     * @inheritdoc
44
     * @throws \yii\base\InvalidConfigException
45
     */
46
    public function init()
47
    {
48
        parent::init();
49
50
        $this->gateway = Instance::ensure($this->gateway, PaymentGatewayInterface::class);
51
        $this->providerClasses = array_merge($this->defaultProviderClasses(), $this->providerClasses);
52
    }
53
54
    /**
55
     * @var null|BankProvider đối tượng cung cấp dữ liệu ngân hàng
56
     * @see [[getBankProvider()]]
57
     */
58
    private $_provider;
59
60
    /**
61
     * 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ị.
62
     *
63
     * @return BankProvider|object
64
     * @throws \yii\base\InvalidConfigException|NotSupportedException
65
     */
66
    public function getBankProvider(): BankProvider
67
    {
68
        if (!$this->_provider instanceof BankProvider) {
69
            $gatewayClass = get_class($this->gateway);
70
71
            if (isset($this->providerClasses[$gatewayClass])) {
72
                $config = array_merge($this->providerConfig, [
73
                    'class' => $this->providerClasses[$gatewayClass]
74
                ]);
75
                /** @var BankProvider $provider */
76
                return $this->_provider = Instance::ensure($config, BankProvider::class);
77
            } else {
78
                throw new NotSupportedException("Gateway: `$gatewayClass` is not supported!");
79
            }
80
        } else {
81
            return $this->_provider;
82
        }
83
    }
84
85
    /**
86
     * 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.
87
     *
88
     * @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.
89
     */
90
    protected function defaultProviderClasses(): array
91
    {
92
        return [
93
            'yiiviet\payment\baokim\PaymentGateway' => 'yiiviet\payment\baokim\BankProvider',
94
            'yiiviet\payment\vtcpay\PaymentGateway' => 'yiiviet\payment\vtcpay\BankProvider',
95
            'yiiviet\payment\nganluong\PaymentGateway' => 'yiiviet\payment\nganluong\BankProvider',
96
            'yiiviet\payment\vnpayment\PaymentGateway' => 'yiiviet\payment\vnpayment\BankProvider',
97
            'yiiviet\payment\onepay\PaymentGateway' => 'yiiviet\payment\onepay\BankProvider'
98
        ];
99
    }
100
}
101