Passed
Push — master ( 7ba57e...634515 )
by Paweł
03:13
created

AccountChangesDataProvider::prepareDataSets()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 0
loc 23
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 2019-01-26
6
 */
7
8
namespace app\components\visualizations\dataproviders;
9
10
11
use app\components\stats\providers\AccountDiffDataProvider;
12
use app\components\visualizations\contracts\DataProviderInterface;
13
use app\components\visualizations\traits\AccountDataProviderTrait;
14
use app\dictionaries\Color;
15
use Yii;
16
use yii\helpers\ArrayHelper;
17
18
class AccountChangesDataProvider extends AccountDiffDataProvider implements DataProviderInterface
19
{
20
    use AccountDataProviderTrait;
21
22
    public function init()
23
    {
24
        $this->statsAttributes = [
25
            'followed_by',
26
        ];
27
28
        parent::init();
29
    }
30
31
    protected function prepareScales()
32
    {
33
        $arr = [];
34
        foreach ($this->statsAttributes as $attribute) {
35
            $arr[] = ArrayHelper::merge([
36
                'id' => $attribute,
37
                'type' => 'linear',
38
                'position' => 'left',
39
            ], ArrayHelper::getValue($this->scalesConfig, $attribute, []));
40
        }
41
42
        return ['yAxes' => $arr];
43
    }
44
45
    protected function prepareLabels()
46
    {
47
        $formatter = Yii::$app->formatter;
48
49
        return array_map(function ($label) use ($formatter) {
50
            return $formatter->format($label, $this->labelFormat);
51
        }, $this->getKeys());
52
    }
53
54
    protected function prepareDataSets()
55
    {
56
        $arr = [];
57
        foreach ($this->statsAttributes as $attribute) {
58
59
            $data = ArrayHelper::getColumn($this->getModels(), $attribute);
60
61
            $colors = [];
62
            foreach ($data as $value) {
63
                $colors[] = $value >= 0 ? ArrayHelper::getValue($this->colors, $attribute, Color::PRIMARY) : Color::DANGER;
64
            }
65
66
            $arr[] = ArrayHelper::merge([
67
                'label' => $this->account->getAttributeLabel($attribute),
68
                'yAxisID' => $attribute,
69
                'fill' => false,
70
                'data' => array_values($data),
71
                'backgroundColor' => $colors,
72
                'borderColor' => $colors,
73
            ], ArrayHelper::getValue($this->dataSetsConfig, $attribute, []));
74
        }
75
76
        return $arr;
77
    }
78
}