Test Setup Failed
Pull Request — master (#38)
by Christophe
03:20
created

ChartOutput::fullCharts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace CMEN\GoogleChartsBundle\Output\Javascript;
4
5
use CMEN\GoogleChartsBundle\Exception\GoogleChartsException;
6
use CMEN\GoogleChartsBundle\GoogleCharts\Chart;
7
use CMEN\GoogleChartsBundle\GoogleCharts\Charts\Diff\DiffChart;
8
use CMEN\GoogleChartsBundle\Output\AbstractChartOutput;
9
use CMEN\GoogleChartsBundle\Output\DataOutputInterface;
10
use CMEN\GoogleChartsBundle\Output\EventsOutputInterface;
11
use CMEN\GoogleChartsBundle\Output\OptionsOutputInterface;
12
13
/**
14
 * @author Christophe Meneses
15
 */
16
class ChartOutput extends AbstractChartOutput
17
{
18
    /** @var OptionsOutputInterface */
19
    private $optionsOutput;
20
21
    /** @var DataOutputInterface */
22
    private $dataOutput;
23
24
    /** @var EventsOutputInterface */
25
    private $eventsOutput;
26
27
    /**
28
     * ChartOutput constructor.
29
     *
30
     * @param string $version
31
     * @param string $language
32
     */
33 13
    public function __construct(
34
        $version,
35
        $language,
36
        OptionsOutputInterface $optionsOutput,
37
        DataOutputInterface $dataOutput,
38
        EventsOutputInterface $eventsOutput
39
    ) {
40 13
        parent::__construct($version, $language);
41
42 13
        $this->optionsOutput = $optionsOutput;
43 13
        $this->dataOutput = $dataOutput;
44 13
        $this->eventsOutput = $eventsOutput;
45 13
    }
46
47
    public function startChart(Chart $chart)
48
    {
49
        if (null === $chart->getElementID()) {
50 5
            throw new GoogleChartsException('Container is not defined.');
51
        }
52 5
53 1
        $js = 'var '.$chart->getName().' = new google.'.$chart->getLibrary().'.'.$chart->getType().
54
            '(document.getElementById("'.$chart->getElementID().'"));';
55
56 4
        if (!$chart instanceof DiffChart) {
57 4
            $js .= $this->dataOutput->draw($chart->getData(), $chart->getDataName());
58
        } else {
59 4
            $js .= $this->dataOutput->draw($chart->getOldChart()->getData(), 'old_'.$chart->getDataName()).
60 4
                $this->dataOutput->draw($chart->getNewChart()->getData(), 'new_'.$chart->getDataName()).
61 4
                'var '.$chart->getDataName().' = '.$chart->getName().
62
                '.computeDiff(old_'.$chart->getDataName().',
63
                 new_'.$chart->getDataName().');';
64
        }
65
66
        $js .= $this->optionsOutput->draw($chart->getOptions(), $chart->getOptionsName());
67
68
        return $js;
69 4
    }
70
71 4
    public function endChart(Chart $chart)
72
    {
73
        if ('visualization' == $chart->getLibrary()) {
74
            $options = $chart->getOptionsName();
75
        } else {
76
            /* Options conversion for material charts */
77 4
            $options = 'google.'.$chart->getLibrary().'.'.$chart->getType().
78
                '.convertOptions('.$chart->getOptionsName().')';
79 4
        }
80 4
81 4
        return $this->eventsOutput->draw($chart->getEvents(), $chart->getName()).$chart->getName().
82
            '.draw('.$chart->getDataName().', '.$options.');';
83
    }
84
85
    public function startCharts($charts, $elementsID = null)
86
    {
87 4
        if ($charts instanceof Chart) {
88 4
            $charts = [$charts];
89
90
            if ($elementsID) {
91
                if (!is_string($elementsID)) {
92
                    throw new GoogleChartsException('A string is expected for HTML element ID.');
93
                }
94 9
95
                $elementsID = [$elementsID];
96 9
            }
97 5
        } elseif (is_array($charts) && !empty($charts)) {
98
            $this->checkChartsTypes($charts);
99 5
100 1
            if (null !== $elementsID) {
101 1
                if (!is_array($elementsID)) {
102
                    throw new GoogleChartsException('An array of string is expected for HTML elements IDs.');
103
                }
104
105
                $this->checkElementsId($charts, $elementsID);
106 8
            }
107 3
        } else {
108
            throw new GoogleChartsException('An instance of Chart or an array of Chart is expected.');
109 2
        }
110 2
111
        $packages = [];
112
        $drawChartName = '';
113 1
        for ($i = 0; $i < count($charts); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
114
            if ($elementsID) {
115
                $charts[$i]->setElementID($elementsID[$i]);
116 4
            }
117 4
118 4
            if (!in_array($charts[$i]->getPackage(), $packages)) {
119 4
                $packages[] = $charts[$i]->getPackage();
120
            }
121
            $drawChartName .= $charts[$i]->getElementID();
122
        }
123 4
124 4
        $js = $this->loadLibraries($packages);
125 4
126 4
        $js .= $this->startCallback('drawChart'.ucfirst(md5($drawChartName)));
127 4
128
        foreach ($charts as $chart) {
129 4
            $js .= $this->startChart($chart);
130
        }
131 4
132
        return $js;
133 4
    }
134 4
135 4
    public function endCharts($charts)
136
    {
137 4
        if ($charts instanceof Chart) {
138
            $js = $this->endChart($charts).$this->endCallback();
139
        } elseif (is_array($charts) && !empty($charts)) {
140
            $this->checkChartsTypes($charts);
141
142
            $js = '';
143 4
            foreach ($charts as $chart) {
144
                $js .= $this->endChart($chart);
145 4
            }
146 4
147 4
            $js .= $this->endCallback();
148
        } else {
149
            throw new GoogleChartsException('An instance of Chart or an array of Chart is expected.');
150
        }
151
152
        return $js;
153
    }
154
155
    public function fullCharts($charts, $elementsID = null)
156
    {
157
        return $this->startCharts($charts, $elementsID).$this->endCharts($charts);
158
    }
159
160 4
    public function loadLibraries(array $packages)
161
    {
162
        array_walk($packages, function (&$item) {
163
            $item = "'".$item."'";
164
        });
165
166 4
        ($this->language) ? $language = ", language: '".$this->language."'" : $language = '';
167
168 4
        $load = "'".$this->version."', {packages:[".implode(',', $packages).']'.$language.'}';
169
170
        return "google.charts.load($load);";
171
    }
172
173
    public function startCallback($name)
174
    {
175
        return "google.charts.setOnLoadCallback($name); function $name() {";
176 5
    }
177 5
178 5
    public function endCallback()
179
    {
180 5
        return '}';
181
    }
182
}
183