ChartController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 7
c 5
b 1
f 3
lcom 1
cbo 5
dl 0
loc 96
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A frontpage() 0 9 1
A createDefaultChartsForLoggedInUser() 0 4 1
A displayChart() 0 21 4
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\Controller;
25
26
use OCA\ocUsageCharts\Exception\ChartServiceException;
27
use OCA\ocUsageCharts\Owncloud\User;
28
use OCA\ocUsageCharts\Service\ChartConfigService;
29
use OCA\ocUsageCharts\Service\ChartCreator;
30
use OCA\ocUsageCharts\Service\ChartService;
31
use \OCP\AppFramework\Controller;
32
use OCP\AppFramework\Http\RedirectResponse;
33
use OCP\AppFramework\Http\TemplateResponse;
34
use OCP\IRequest;
35
36
/**
37
 * @author Arno van Rossum <[email protected]>
38
 */
39
class ChartController extends Controller
40
{
41
    /**
42
     * @var ChartService
43
     */
44
    private $chartService;
45
46
    /**
47
     * @var ChartConfigService
48
     */
49
    private $configService;
50
51
    /**
52
     * @var ChartCreator
53
     */
54
    private $chartCreator;
55
56
    /**
57
     * @var User
58
     */
59
    private $user;
60
61
    /**
62
     * @param string $appName
63
     * @param IRequest $request
64
     * @param ChartService $chartService
65
     * @param ChartConfigService $configService
66
     * @param ChartCreator $chartCreator
67
     * @param User $user
68
     */
69
    public function __construct($appName, IRequest $request, ChartService $chartService, ChartConfigService $configService, ChartCreator $chartCreator, User $user)
70
    {
71
        $this->chartService = $chartService;
72
        $this->configService = $configService;
73
        $this->chartCreator = $chartCreator;
74
        $this->user = $user;
75
        parent::__construct($appName, $request);
76
    }
77
78
    /**
79
     * Entry point for the chart system
80
     *
81
     * @NoCSRFRequired
82
     * @NoAdminRequired
83
     * @return TemplateResponse
84
     */
85
    public function frontpage()
86
    {
87
        $this->createDefaultChartsForLoggedInUser();
88
89
        $charts = $this->configService->getChartsForLoggedInUser();
90
        $id = $charts[0]->getId();
91
        $url = \OCP\Util::linkToRoute('ocusagecharts.chart.display_chart', array('id' => $id));
92
        return new RedirectResponse($url);
93
    }
94
95
    /**
96
     * Create default charts for the logged in user
97
     */
98
    private function createDefaultChartsForLoggedInUser()
99
    {
100
        $this->chartCreator->createDefaultConfigFor($this->user->getSignedInUsername());
101
    }
102
103
    /**
104
     * Show a single chart
105
     *
106
     * @NoCSRFRequired
107
     * @NoAdminRequired
108
     * @param string $id
109
     * @throws \OCA\ocUsageCharts\Exception\ChartServiceException
110
     *
111
     * @return TemplateResponse
112
     */
113
    public function displayChart($id)
114
    {
115
        $selectedConfig = null;
116
        $chartConfigs = $this->configService->getChartsForLoggedInUser();
117
        foreach($chartConfigs as $config)
118
        {
119
            if ( $config->getId() == $id )
120
            {
121
                $selectedConfig = $config;
122
                break;
123
            }
124
        }
125
        if ( is_null($selectedConfig) )
126
        {
127
            throw new ChartServiceException('No config found for selected ID');
128
        }
129
130
        $chart = $this->chartService->getChartByConfig($selectedConfig);
131
        $templateName = 'main';  // will use templates/main.php
132
        return new TemplateResponse($this->appName, $templateName, array('chart' => $chart, 'configs' => $chartConfigs, 'requesttoken' => \OC_Util::callRegister()));
133
    }
134
}