Completed
Push — master ( 43760e...4526d9 )
by Valentin
04:56 queued 45s
created

ChartJSDataset   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B convert() 0 19 5
A pack() 0 7 1
1
<?php
2
3
namespace Spiral\Statistics\Extract\Dataset;
4
5
use Spiral\Statistics\Extract\Events;
6
7
class ChartJSDataset extends AbstractDataset
8
{
9
    /** @var array */
10
    private $params = [];
11
12
    /** @var array */
13
    private $labels = [];
14
15
    /**
16
     * ChartDataset constructor.
17
     *
18
     * @param array $params - assoc array, where key is event name and value is an options array
19
     *                      for this event dataset like: "label" => "Event Label", etc.
20
     */
21
    public function __construct(array $params = [])
22
    {
23
        $this->params = $params;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function convert()
30
    {
31
        foreach ($this->raw as $row) {
32
            $label = $row->getLabel();
33
            $this->labels[] = $label;
34
35
            foreach ($row->getEvents() as $event => $value) {
36
                if (!array_key_exists($event, $this->data)) {
37
                    if (array_key_exists($event, $this->params)) {
38
                        $this->data[$event] = $this->params[$event];
39
                    }
40
41
                    $this->data[$event]['data'] = [];
42
                }
43
44
                $this->data[$event]['data'][] = $value;
45
            }
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function pack(): array
53
    {
54
        return [
55
            'labels'   => $this->labels,
56
            'datasets' => array_values($this->data)
57
        ];
58
    }
59
}