AbstractChartOutput   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 57
ccs 12
cts 15
cp 0.8
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkChartsTypes() 0 5 3
A setLanguage() 0 5 1
A checkElementsId() 0 9 4
A __construct() 0 10 1
1
<?php
2
3
namespace CMEN\GoogleChartsBundle\Output;
4
5
use CMEN\GoogleChartsBundle\Exception\GoogleChartsException;
6
use CMEN\GoogleChartsBundle\GoogleCharts\Chart;
7
8
/**
9
 * @author Christophe Meneses
10
 */
11
abstract class AbstractChartOutput implements ChartOutputInterface
12
{
13 14
    public function __construct(
14
        /**
15
         * Version of Google Charts used.
16
         */
17
        protected string $version,
18
        /**
19
         * Locale to customize currencies, dates, and numbers.
20
         */
21
        protected string $language,
22
    ) {
23 14
    }
24
25
    /**
26
     * @return $this
27
     */
28
    public function setLanguage(string $language): ChartOutputInterface
29
    {
30
        $this->language = $language;
31
32
        return $this;
33
    }
34
35
    /**
36
     * Checks if all elements ID are string and same number as charts.
37
     *
38
     * @param Chart[]  $charts
39
     * @param string[] $elementsID
40
     *
41
     * @throws GoogleChartsException
42
     */
43 2
    protected function checkElementsId(array $charts, array $elementsID): void
44
    {
45 2
        if (count($charts) != count($elementsID)) {
46 1
            throw new GoogleChartsException('Array charts and array HTML elements ID do not have the same number of element.');
47
        }
48
49 1
        foreach ($elementsID as $elementId) {
50 1
            if (!is_string($elementId)) {
51 1
                throw new GoogleChartsException('A string is expected for HTML element ID.');
52
            }
53
        }
54
    }
55
56
    /**
57
     * Checks if all elements of an array are instances of Chart. Throws an exception if not.
58
     *
59
     * @param Chart[] $charts
60
     *
61
     * @throws GoogleChartsException
62
     */
63 4
    protected function checkChartsTypes(array $charts): void
64
    {
65 4
        foreach ($charts as $chart) {
66 4
            if (!$chart instanceof Chart) {
67 1
                throw new GoogleChartsException('An array of Chart is expected.');
68
            }
69
        }
70
    }
71
}
72