Completed
Push — master ( e07b14...eb64f2 )
by Dmitry
29:16
created

BillTypesProvider::getTypes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 6
1
<?php
2
3
namespace hipanel\modules\finance\providers;
4
5
use hipanel\components\ApiConnectionInterface;
6
use hipanel\helpers\ArrayHelper;
7
use hipanel\models\Ref;
8
use Yii;
9
10
/**
11
 * Class BillTypesProvider
12
 * @package hipanel\modules\finance\providers
13
 */
14
class BillTypesProvider
15
{
16
    /**
17
     * @var ApiConnectionInterface
18
     */
19
    private $api;
20
21
    /**
22
     * BillTypesProvider constructor.
23
     * @param ApiConnectionInterface $api
24
     */
25
    public function __construct(ApiConnectionInterface $api)
26
    {
27
        $this->api = $api;
28
    }
29
30
    /**
31
     * Returns key-value list of bill types.
32
     * `key` - type name
33
     * `value` - type label (translated)
34
     *
35
     * @return array
36
     */
37
    public function getTypesList()
38
    {
39
        return ArrayHelper::map($this->getTypes(), 'name', 'label');
40
    }
41
42
    /**
43
     * Returns array of types.
44
     * When user can not support, filters out unused types
45
     *
46
     * @return Ref[]
47
     */
48
    public function getTypes()
49
    {
50
        $options = ['select' => 'full', 'orderby' => 'name_asc', 'with_hierarchy' => true, ];
51
        $types = Ref::findCached('type,bill', 'hipanel:finance', $options);
0 ignored issues
show
Bug introduced by
The method findCached() does not seem to exist on object<hipanel\models\Ref>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
53
        if (!Yii::$app->user->can('support')) {
54
            $types = $this->removeUnusedTypes($types);
55
        }
56
57
        return $types;
58
    }
59
60
    /**
61
     * @param Ref[] $types
62
     * @return Ref[]
63
     */
64
    private function removeUnusedTypes($types)
65
    {
66
        $ids = Yii::$app->cache->getTimeCached(3600, [Yii::$app->user->id], function () use ($types) {
67
            return ArrayHelper::getColumn($this->api->get('billsGetUsedTypes'), 'id');
68
        });
69
70
        return array_filter($types, function ($model) use ($ids) {
71
            return in_array($model->id, $ids);
72
        });
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function getGroupedList()
79
    {
80
        $billTypes = [];
81
        $billGroupLabels = [];
82
83
        $types = $this->getTypesList();
84
85
        foreach ($types as $key => $title) {
86
            list($type, $name) = explode(',', $key);
87
88
            if (!isset($billTypes[$type])) {
89
                $billTypes[$type] = [];
90
                $billGroupLabels[$type] = ['label' => $title];
91
            }
92
93
            if (isset($name)) {
94
                foreach ($types as $k => $t) {
95
                    if (strpos($k, $type . ',') === 0) {
96
                        $billTypes[$type][$k] = $t;
97
                    }
98
                }
99
            }
100
        }
101
102
        return [$billTypes, $billGroupLabels];
103
    }
104
}
105