Completed
Push — master ( c2bc12...ef5bc2 )
by Dmitry
04:55
created

src/merchant/CurrenciesCollection.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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-2019, 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 hiqdev\yii2\merchant\Currencies;
16
use hiqdev\yii2\merchant\models\Currency;
17
use Yii;
18
19
/**
20
 * Class CurrenciesCollection
21
 *
22
 * @author Dmytro Naumenko <[email protected]>
23
 */
24
class CurrenciesCollection extends Currencies
25
{
26
    /** @var Currency[] */
27
    private $currencies = [];
28
29
    public function __construct()
30
    {
31
        $this->currencies = $this->fetchCurrencies();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->fetchCurrencies() of type array<integer,string> is incompatible with the declared type array<integer,object<hiq...chant\models\Currency>> of property $currencies.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
    }
33
34
    /**
35
     * @return string[]
36
     */
37
    public function getList(): array
38
    {
39
        return $this->currencies;
40
    }
41
42
    /**
43
     * @return string[]
44
     * @throws ResponseErrorException
45
     */
46
    private function fetchCurrencies(): array
47
    {
48
        $params = [];
49
        if (Yii::$app->user->getIsGuest()) {
50
            $params['seller'] = Yii::$app->params['user.seller'];
51
        } else {
52
            $params['client_id'] = Yii::$app->user->getId();
53
        }
54
55
        $result = [];
56
        $currencies = $this->requestCurrencies($params);
57
        foreach ($currencies ?? [] as $currency) {
58
            $result[] = new Currency(['code' => $currency['code']]);
59
        }
60
61
        return $result;
62
    }
63
64
    public function requestCurrencies($params): array
65
    {
66
        $userId = Yii::$app->user->getIsGuest() ? null : Yii::$app->user->identity->getId();
67
68
        try {
69
            return Yii::$app->getCache()->getOrSet([__METHOD__, $params, $userId], function () use ($params) {
70
                return Merchant::perform('get-possible-currencies', $params);
71
            }, 3600);
72
        } catch (ResponseErrorException $e) {
73
            if ($e->getResponse()->getData() === null) {
74
                Yii::info('No available currencies found', 'hipanel:finance');
75
                return [];
76
            }
77
78
            throw $e;
79
        }
80
    }
81
}
82