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

BankProvider::getMerchantData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
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
9
namespace yiiviet\payment\baokim;
10
11
use yii\base\InvalidCallException;
12
13
use yiiviet\payment\BankProvider as BaseBankProvider;
14
15
/**
16
 * Lớp BankProvider
17
 *
18
 * @author Vuong Minh <[email protected]>
19
 * @since 1.0.3
20
 */
21
class BankProvider extends BaseBankProvider
22
{
23
    /**
24
     * Thẻ nội địa.
25
     */
26
    const TYPE_LOCAL_CARD = 1;
27
28
    /**
29
     * Thẻ tín dụng quốc tế.
30
     */
31
    const TYPE_CREDIT_CARD = 2;
32
33
    /**
34
     * Chuyển khoản online của các ngân hàng.
35
     */
36
    const TYPE_INTERNET_BANKING = 3;
37
38
    /**
39
     * Chuyển khoản ATM.
40
     */
41
    const TYPE_ATM_TRANSFER = 4;
42
43
    /**
44
     * Chuyển khoản truyền thống giữa các ngân hàng.
45
     */
46
    const TYPE_BANK_TRANSFER = 5;
47
48
    /**
49
     * @var int loại ngân hàng muốn lấy, ví dụ atm online, offline, giao dịch tại quầy...
50
     */
51
    public $type = self::TYPE_LOCAL_CARD;
52
53
    /**
54
     * @var mixed mã client dùng để truy xuất thông tin, nếu không chỉ định hệ thống sẽ tự động lấy theo client.
55
     */
56
    public $emailBusiness;
57
58
    /**
59
     * @var mixed mã client dùng để truy xuất thông tin, nếu không chỉ định hệ thống sẽ tự động lấy.
60
     */
61
    public $clientId;
62
63
    /**
64
     * @var PaymentGateway
65
     * @inheritdoc
66
     */
67
    protected $gateway;
68
69
    /**
70
     * @return array
71
     * @throws \ReflectionException
72
     * @throws \yii\base\InvalidConfigException
73
     */
74 2
    public function banks(): array
75
    {
76 2
        $merchantData = $this->getMerchantData();
77 2
        $banks = [];
78
79 2
        foreach ($merchantData['bank_payment_methods'] as $bank) {
80 2
            if ($bank['payment_method_type'] == $this->type) {
81 2
                $banks[$bank['id']] = $bank['name'];
82
            }
83
        }
84
85 2
        natsort($banks);
86
87 2
        return $banks;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     * @throws \ReflectionException|\yii\base\InvalidConfigException
93
     */
94 1
    public function getBankLogo($bankId): string
95
    {
96 1
        $merchantData = $this->getMerchantData();
97
98 1
        foreach ($merchantData['bank_payment_methods'] as $bank) {
99 1
            if ($bank['id'] == $bankId) {
100 1
                return $bank['logo_url'];
101
            }
102
        }
103
104
        throw new InvalidCallException('Can\'t get bank logo of bank id: ' . $bankId);
105
    }
106
107
    /**
108
     * Phương thức hổ trợ lấy thông tin merchant theo cấu hình chỉ định từ đó lấy ra thông tin các ngân hàng phù hợp.
109
     *
110
     * @return \GatewayClients\DataInterface|ResponseData đối tượng phản hồi từ cổng thanh toán chứa thông tin merchant.
111
     * @throws \ReflectionException|\yii\base\InvalidConfigException
112
     */
113 3
    protected function getMerchantData()
114
    {
115 3
        $data = $this->gateway->getMerchantData($this->emailBusiness, $this->clientId);
116
117 3
        if ($data->isOk) {
0 ignored issues
show
Bug introduced by
Accessing isOk on the interface GatewayClients\DataInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
118 3
            return $data;
119
        } else {
120
            throw new InvalidCallException('Can not get merchant data!');
121
        }
122
    }
123
124
}
125