Completed
Push — master ( c12077...3142ad )
by Andrii
13:16
created

Collection::requestMerchants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\merchant;
12
13
use hipanel\modules\finance\models\Merchant;
14
use hiqdev\hiart\ResponseErrorException;
15
use Yii;
16
use yii\helpers\ArrayHelper;
17
18
class Collection extends \hiqdev\yii2\merchant\Collection
19
{
20
    public function init()
21
    {
22
        parent::init();
23
        $this->loadMerchants($this->params);
24
    }
25
26
    public function loadMerchants(array $params = null)
27
    {
28
        $this->addItems($this->fetchMerchants($params));
0 ignored issues
show
Bug introduced by
It seems like $params defined by parameter $params on line 26 can also be of type null; however, hipanel\modules\finance\...ction::fetchMerchants() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
29
    }
30
31
    public static $supportedSystems = [
32
        'webmoney' => 1,
33
        'paypal' => 1,
34
        'yandex' => 1,
35
        'interkassa' => 1,
36
        'paxum' => 1,
37
        'ecoin' => 1,
38
        'okpay' => 1,
39
        'robokassa' => 1,
40
        'freekassa' => 1,
41
    ];
42
43
    public function fetchMerchants(array $params = [])
44
    {
45
        $defaults = [
46
            'sum' => $params['amount'] ?: 1,
47
            'site' => Yii::$app->request->getHostInfo(),
48
        ];
49
50
        if (Yii::$app->user->getIsGuest()) {
51
            $defaults['seller'] = Yii::$app->params['user.seller'];
52
        } else {
53
            $defaults['username'] = Yii::$app->user->identity->username;
54
        }
55
56
        $params = array_merge($defaults, (array)$params);
57
58
        try {
59
            $merchants = $this->requestMerchants($params);
60
        } catch (ResponseErrorException $e) {
61
            if ($e->getResponse()->getData() === null) {
62
                Yii::info('No available payment methods found', 'hipanel:finance');
63
                $merchants = [];
64
            } else {
65
                throw $e;
66
            }
67
        }
68
69
        $result = [];
70
        foreach (array_keys(static::$supportedSystems) as $system) {
71
            foreach ($merchants as $name => $merchant) {
72
                if ($merchant['system'] === $system) {
73
                    $result[$name] = $this->convertMerchant($merchant);
74
                }
75
            }
76
        }
77
78
        return $result;
79
    }
80
81
    public function requestMerchants($params)
82
    {
83
        return Yii::$app->getCache()->getOrSet([__METHOD__, $params], function () use ($params) {
84
            return Merchant::perform('prepare-info', $params, ['batch' => true]);
85
        }, 3600*24);
86
    }
87
88
    public function convertMerchant($data)
89
    {
90
        $data['currency'] = strtoupper($data['currency']);
91
        $data['amount']   = $data['sum'];
92
93
        return [
94
            'gateway' => $data['system'],
95
            'label' => $data['label'],
96
            'data' => $data,
97
        ];
98
    }
99
}
100