Passed
Pull Request — master (#38)
by Christophe
05:14
created

AbstractChartOutput   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 75
ccs 14
cts 18
cp 0.7778
rs 10
c 1
b 0
f 0
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 4 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
     * Version of Google Charts used.
15
     *
16
     * @var string
17
     */
18
    protected $version;
19
20
    /**
21
     * Locale to customize currencies, dates, and numbers.
22
     *
23
     * @var string
24
     */
25
    protected $language;
26
27
    /**
28
     * @param string $version
29
     * @param string $language
30
     */
31 14
    public function __construct($version, $language)
32
    {
33 14
        $this->version = $version;
34 14
        $this->language = $language;
35 14
    }
36
37
    /**
38
     * @param string $language
39
     *
40
     * @return $this
41
     */
42
    public function setLanguage($language)
43
    {
44
        $this->language = $language;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Checks if all elements ID are string and same number as charts.
51
     *
52
     * @param Chart[]  $charts
53
     * @param string[] $elementsID
54
     *
55
     * @return void
56
     *
57
     * @throws GoogleChartsException
58
     */
59 2
    protected function checkElementsId(array $charts, array $elementsID)
60
    {
61 2
        if (count($charts) != count($elementsID)) {
62 1
            throw new GoogleChartsException('Array charts and array HTML elements ID do not have the same number of element.');
63
        }
64
65 1
        foreach ($elementsID as $elementId) {
66 1
            if (!is_string($elementId)) {
67 1
                throw new GoogleChartsException('A string is expected for HTML element ID.');
68
            }
69
        }
70
    }
71
72
    /**
73
     * Checks if all elements of an array are instances of Chart. Throws an exception if not.
74
     *
75
     * @param Chart[] $charts
76
     *
77
     * @return void
78
     *
79
     * @throws GoogleChartsException
80
     */
81 4
    protected function checkChartsTypes(array $charts)
82
    {
83 4
        foreach ($charts as $chart) {
84 4
            if (!$chart instanceof Chart) {
85 4
                throw new GoogleChartsException('An array of Chart is expected.');
86
            }
87
        }
88 3
    }
89
}
90