DataOutput   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 40
ccs 22
cts 22
cp 1
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B draw() 0 34 8
A __construct() 0 2 1
1
<?php
2
3
namespace CMEN\GoogleChartsBundle\Output\Javascript;
4
5
use CMEN\GoogleChartsBundle\Exception\GoogleChartsException;
6
use CMEN\GoogleChartsBundle\GoogleCharts\Data;
7
use CMEN\GoogleChartsBundle\Output\DataOutputInterface;
8
use CMEN\GoogleChartsBundle\Output\DateOutputInterface;
9
10
/**
11
 * @author Christophe Meneses
12
 */
13
class DataOutput implements DataOutputInterface
14
{
15 5
    public function __construct(private readonly DateOutputInterface $dateOutput)
16
    {
17 5
    }
18
19 5
    public function draw(Data $data, string $dataName): string
20
    {
21 5
        $arrayToDataTable = $data->getArrayToDataTable();
22
23 5
        if (empty($arrayToDataTable)) {
24 1
            throw new GoogleChartsException('There is no data for chart. Use method setArrayToDataTable() to provide data.');
25
        }
26
27 4
        $js = "var $dataName = new google.visualization.arrayToDataTable([";
28 4
        $lastKeyRow = array_key_last($arrayToDataTable);
29 4
        foreach ($data->getArrayToDataTable() as $keyRow => $row) {
30 4
            $js .= '[';
31 4
            $lastKeyValue = array_key_last($row);
32 4
            foreach ($row as $key => $value) {
33 4
                if ($value instanceof \DateTimeInterface) {
34 2
                    $js .= $this->dateOutput->draw($value);
35
                } else {
36 4
                    $js .= json_encode($value);
37
                }
38
39 4
                if ($key != $lastKeyValue) {
40 4
                    $js .= ', ';
41
                }
42
            }
43 4
            $js .= ']';
44
45 4
            if ($keyRow != $lastKeyRow) {
46 4
                $js .= ', ';
47
            }
48
        }
49
50 4
        $data->isFirstRowIsData() ? $js .= '], true);' : $js .= '], false);';
51
52 4
        return $js;
53
    }
54
}
55