ChartUpdaterService::updateChartsForUsers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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\Entity\ChartConfig;
27
use OCA\ocUsageCharts\Owncloud\Users;
28
29
/**
30
 * @author Arno van Rossum <[email protected]>
31
 */
32
class ChartUpdaterService
33
{
34
    /**
35
     * @var ChartDataProvider
36
     */
37
    private $dataProvider;
38
39
    /**
40
     * @var ChartConfigService
41
     */
42
    private $configService;
43
44
    /**
45
     * @var Users
46
     */
47
    private $users;
48
49
    /**
50
     * @param ChartDataProvider $dataProvider
51
     * @param ChartConfigService $configService
52
     * @param Users $users
53
     */
54
    public function __construct(ChartDataProvider $dataProvider, ChartConfigService $configService, Users $users)
55
    {
56
        $this->dataProvider = $dataProvider;
57
        $this->configService = $configService;
58
        $this->users = $users;
59
    }
60
61
    /**
62
     * Update all charts for all users
63
     */
64
    public function updateChartsForUsers()
65
    {
66
        $users = $this->users->getSystemUsers();
67
        foreach($users as $userName)
68
        {
69
            $this->updateChartsForUser($userName);
70
        }
71
    }
72
73
    /**
74
     * Update all charts for one specific user
75
     * @param string $userName
76
     */
77
    private function updateChartsForUser($userName)
78
    {
79
        $charts = $this->configService->getChartsByUsername($userName);
80
        foreach($charts as $chartConfig)
81
        {
82
            $this->updateChart($chartConfig);
83
        }
84
    }
85
86
    /**
87
     * Update a specific chart
88
     * @param ChartConfig $chartConfig
89
     * @return boolean
90
     */
91
    private function updateChart(ChartConfig $chartConfig)
92
    {
93
        if ( !$this->dataProvider->isAllowedToUpdate($chartConfig) )
94
        {
95
            return false;
96
        }
97
        $usage = $this->dataProvider->getChartUsageForUpdate($chartConfig);
98
        return $this->dataProvider->save($chartConfig, $usage);
99
    }
100
}