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

ChartWidget::renderHeader()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 2018-10-28
6
 */
7
8
namespace app\components\visualizations\widgets;
9
10
11
use app\dictionaries\ChartType;
12
use Carbon\Carbon;
13
use dosamigos\chartjs\ChartJs;
14
use Yii;
15
use yii\base\Widget;
16
use yii\helpers\ArrayHelper;
17
use yii\helpers\Html;
18
19
class ChartWidget extends Widget
20
{
21
    public $icon = 'fa fa-bar-chart';
22
    public $title = 'Chart';
23
    public $type = ChartType::LINE;
24
    public $aspectRatio = 2;
25
26
    public $clientOptions = [];
27
28
    /**
29
     * @var \app\components\visualizations\dataproviders\AccountTrendsDataProvider|array
30
     */
31
    public $dataProvider;
32
33
    /**
34
     * @var Carbon
35
     */
36
    public $from;
37
38
    /**
39
     * @var Carbon
40
     */
41
    public $to;
42
43
    /**
44
     * @var \yii\db\ActiveRecord
45
     */
46
    public $model;
47
48
    public $labelFormat = 'date';
49
50
    /**
51
     * @var \app\components\Formatter
52
     */
53
    protected $formatter;
54
55
    public function init()
56
    {
57
        parent::init();
58
        $this->setId(sprintf('%s_chart', $this->getId()));
59
60
        $this->dataProvider = Yii::createObject(ArrayHelper::merge($this->dataProvider, [
61
            'account' => $this->model,
62
            'from' => $this->from,
63
            'to' => $this->to,
64
        ]));
65
        $this->formatter = Yii::$app->formatter;
0 ignored issues
show
Documentation Bug introduced by
Yii::app->formatter is of type yii\i18n\Formatter, but the property $formatter was declared to be of type app\components\Formatter. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
66
    }
67
68
    public function run()
69
    {
70
        $config = [
71
            'id' => $this->id,
72
            'type' => $this->type,
73
            'clientOptions' => ArrayHelper::merge([
74
                'responsive' => true,
75
                'aspectRatio' => $this->aspectRatio,
76
                'legend' => [
77
                    'position' => 'bottom',
78
                ],
79
                'tooltips' => [
80
                    'mode' => 'index',
81
                    'position' => 'nearest',
82
                ],
83
                'scales' => $this->dataProvider->scales(),
84
            ], $this->clientOptions),
85
            'data' => [
86
                'labels' => $this->dataProvider->labels(),
87
                'datasets' => $this->dataProvider->dataSets(),
88
            ],
89
        ];
90
91
        echo '<div class="box" id="' . $this->getId() . '_box">';
92
        $this->renderHeader();
93
        echo '<div class="box-body">';
94
        echo ChartJs::widget($config);
95
        echo '</div>';
96
        echo '</div>';
97
    }
98
99
    protected function renderHeader()
100
    {
101
        echo '<div class="box-header with-border">';
102
        echo $this->icon ? "<span class='$this->icon'></span>" : '';
103
        echo $this->title ? sprintf('<h3 class="box-title">%s <small>%s - %s</small></h3>',
104
            Html::encode($this->title),
105
            $this->formatter->asDate($this->dataProvider->getFrom()->timestamp),
106
            $this->formatter->asDate($this->dataProvider->getTo()->timestamp)
107
        ) : '';
108
        echo '</div>';
109
    }
110
}