ChartService::getChartUsage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Copyright (c) 2014 - Arno van Rossum <[email protected]>
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
namespace OCA\ocUsageCharts\Service;
25
26
use OCA\ocUsageCharts\Adapters\ChartTypeAdapterInterface;
27
use OCA\ocUsageCharts\ChartTypeAdapterFactory;
28
use OCA\ocUsageCharts\Entity\ChartConfig;
29
use OCA\ocUsageCharts\Exception\ChartServiceException;
30
31
/**
32
 * @author Arno van Rossum <[email protected]>
33
 */
34
class ChartService
35
{
36
    /**
37
     * @var ChartDataProvider
38
     */
39
    private $provider;
40
41
    /**
42
     * @var ChartConfigService
43
     */
44
    private $config;
45
46
    /**
47
     * @var ChartTypeAdapterFactory
48
     */
49
    private $chartTypeAdapterFactory;
50
51
    /**
52
     * What providers are already loaded
53
     * @var array
54
     */
55
    private $isLoaded = array();
56
57
    /**
58
     * @param ChartDataProvider $provider
59
     * @param ChartConfigService $config
60
     * @param ChartTypeAdapterFactory $chartTypeAdapterFactory
61
     */
62
    public function __construct(ChartDataProvider $provider, ChartConfigService $config, ChartTypeAdapterFactory $chartTypeAdapterFactory)
63
    {
64
        $this->provider = $provider;
65
        $this->config = $config;
66
        $this->chartTypeAdapterFactory = $chartTypeAdapterFactory;
67
    }
68
69
    /**
70
     * Return all charts
71
     * @return array
72
     */
73
    public function getCharts()
74
    {
75
        $charts = array();
76
        $chartConfigs = $this->config->getChartsForLoggedInUser();
77
        foreach($chartConfigs as $config)
78
        {
79
            $charts[] = $this->getChartByConfig($config);
80
        }
81
        return $charts;
82
    }
83
84
    /**
85
     * @param integer $id
86
     * @return ChartTypeAdapterInterface
87
     * @throws \OCA\ocUsageCharts\Exception\ChartServiceException
88
     */
89
    public function getChart($id)
90
    {
91
        $config = $this->config->getChartConfigById($id);
92
        return $this->getChartByConfig($config);
93
    }
94
95
    /**
96
     * @param ChartConfig $config
97
     * @return ChartTypeAdapterInterface
98
     *
99
     * @throws \OCA\ocUsageCharts\Exception\ChartServiceException
100
     */
101
    public function getChartByConfig(ChartConfig $config)
102
    {
103
        /** @var ChartTypeAdapterInterface $chartAdapter */
104
        $chartAdapter = $this->chartTypeAdapterFactory->getChartTypeAdapterByConfig($config);
105
106
        if ( !in_array($config->getChartProvider(), $this->isLoaded) )
107
        {
108
            $chartAdapter->loadFrontend();
109
            $this->isLoaded[] = $config->getChartProvider();
110
        }
111
112
        return $chartAdapter;
113
    }
114
115
    /**
116
     * @param ChartConfig $chartConfig
117
     *
118
     * @return array
119
     */
120
    public function getChartUsage(ChartConfig $chartConfig)
121
    {
122
        return $this->provider->getChartUsage($chartConfig);
123
    }
124
}
125