Test Failed
Push — master ( d818fc...2d2f9a )
by Christophe
08:50
created

DataOutput::draw()   C

Complexity

Conditions 8
Paths 23

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 28
nc 23
nop 2
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
9
/**
10
 * @author Christophe Meneses
11
 */
12
class DataOutput implements DataOutputInterface
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function draw(Data $data, $dataName)
18
    {
19
        $arrayToDataTable = $data->getArrayToDataTable();
20
21
        if (!$arrayToDataTable) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arrayToDataTable of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
22
            throw new GoogleChartsException(
23
                'There is no data for chart. Use method setArrayToDataTable() to provide data.'
24
            );
25
        }
26
27
        $js = "var $dataName = new google.visualization.arrayToDataTable([";
28
29
        end($arrayToDataTable);
30
        $lastKeyRow = key($arrayToDataTable);
31
        foreach ($data->getArrayToDataTable() as $keyRow => $row) {
32
            $js .= '[';
33
34
            end($row);
35
            $lastKeyValue = key($row);
36
            foreach ($row as $key => $value) {
37
                if ($value instanceof \DateTime) {
38
                    $js .= 'new Date('.$value->format('Y').', '.($value->format('n') - 1).', '.
39
                        $value->format('d').', '.$value->format('H').', '.$value->format('i').', '.
40
                        $value->format('s').')';
41
                } else {
42
                    $js .= json_encode($value);
43
                }
44
45
                if ($key != $lastKeyValue) {
46
                    $js .= ', ';
47
                }
48
            }
49
            unset($value);
50
            $js .= ']';
51
52
            if ($keyRow != $lastKeyRow) {
53
                $js .= ', ';
54
            }
55
        }
56
        unset($row);
57
58
        $data->isFirstRowIsData() ? $js .= '], true);' : $js .= '], false);';
59
60
        return $js;
61
    }
62
}
63