ChartCreator::createDefaultConfigFor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * Copyright (c) 2015 - 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
use OCA\ocUsageCharts\Entity\ChartConfig;
26
use OCA\ocUsageCharts\Entity\ChartConfigRepository;
27
use OCA\ocUsageCharts\Owncloud\Users;
28
29
/**
30
 * @author Arno van Rossum <[email protected]>
31
 */
32
class ChartCreator
33
{
34
    /**
35
     * @var ChartConfigRepository
36
     */
37
    private $chartConfigRepository;
38
39
    /**
40
     * @var Users
41
     */
42
    private $users;
43
44
    /**
45
     * @param ChartConfigRepository $repository
46
     * @param Users $users
47
     */
48
    public function __construct(ChartConfigRepository $repository, Users $users)
49
    {
50
        $this->chartConfigRepository = $repository;
51
        $this->users = $users;
52
    }
53
54
    /**
55
     * Create the default charts for all system users
56
     */
57
    public function createDefaultChartsForSystemUsers()
58
    {
59
        $users = $this->users->getSystemUsers();
60
61
        foreach($users as $username)
62
        {
63
            $this->createDefaultConfigFor($username);
64
        }
65
    }
66
67
    /**
68
     * Create default config for a user
69
     *
70
     * @param string $username
71
     */
72
    public function createDefaultConfigFor($username)
73
    {
74
        $charts = $this->getChartsFor($username);
75
76
        $types = $this->removeChartsThatAreAlreadySet($charts);
77
78
        $this->createDefaultConfigForChartsGiven($username, $types);
79
    }
80
81
    /**
82
     * @param string $username
83
     * @return array|\OCA\ocUsageCharts\Entity\ChartConfig[]
84
     */
85
    private function getChartsFor($username)
86
    {
87
        return $this->chartConfigRepository->findByUsername($username);
88
    }
89
90
    /**
91
     * @param array $charts
92
     * @return array
93
     */
94
    private function removeChartsThatAreAlreadySet($charts)
95
    {
96
        $types = array(
97
            'StorageUsageCurrent' => '',
98
            'StorageUsageLastMonth' => json_encode(array('size' => 'gb')),
99
            'StorageUsagePerMonth' => json_encode(array('size' => 'gb')),
100
            'ActivityUsagePerMonth' => '',
101
            'ActivityUsageLastMonth' => ''
102
        );
103
        foreach ($charts as $chart) {
104
            $type = $chart->getChartType();
105
            if (in_array($type, array_keys($types))) {
106
                unset($types[$type]);
107
            }
108
        }
109
        return $types;
110
    }
111
112
    /**
113
     * @param string $username
114
     * @param array $types
115
     */
116
    private function createDefaultConfigForChartsGiven($username, $types)
117
    {
118
        foreach ($types as $type => $metaData) {
119
            $config = new ChartConfig(
120
                null,
121
                new \DateTime(),
122
                $username,
123
                $type,
124
                'c3js',
125
                $metaData
126
            );
127
            $this->chartConfigRepository->save($config);
128
        }
129
    }
130
}
131