StorageUsageLastMonthAdapter::getRowX()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
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\Adapters\c3js\Storage;
25
26
use OCA\ocUsageCharts\Entity\ChartConfig;
27
use OCA\ocUsageCharts\Adapters\c3js\c3jsBase;
28
use OCA\ocUsageCharts\Owncloud\User;
29
use OCA\ocUsageCharts\ValueObject\Measurements\GigaByteMetric;
30
use OCA\ocUsageCharts\ValueObject\Measurements\KiloByteMetric;
31
use OCA\ocUsageCharts\ValueObject\Measurements\MegaByteMetric;
32
use OCA\ocUsageCharts\ValueObject\Measurements\TeraByteMetric;
33
34
/**
35
 * @author Arno van Rossum <[email protected]>
36
 */
37
class StorageUsageLastMonthAdapter extends c3jsBase
38
{
39
    /**
40
     * @var array
41
     */
42
    private $allowedSizes = array('kb', 'mb', 'gb', 'tb');
43
44
    /**
45
     * @var string
46
     */
47
    private $size;
48
49
    /**
50
     * @param ChartConfig $chartConfig
51
     * @param User $user
52
     */
53
    public function __construct(ChartConfig $chartConfig, User $user)
54
    {
55
        $metaData = json_decode($chartConfig->getMetaData());
56
        if ( !empty($metaData) )
57
        {
58
            $size = $metaData->size;
59
        }
60
        if ( empty($size) || !in_array($size, $this->allowedSizes) )
61
        {
62
            $size = 'gb';
63
        }
64
65
        $this->size = $size;
66
        parent::__construct($chartConfig, $user);
67
    }
68
69
70
    /**
71
     * Parse the usage given to a chosen format
72
     *
73
     * @param $usage
74
     * @return float
75
     */
76
    protected function calculateUsage($usage)
77
    {
78
        switch($this->size)
79
        {
80
            case 'tb':
81
                $metric = new TeraByteMetric($usage);
82
                break;
83
            case 'gb':
84
                $metric = new GigaByteMetric($usage);
85
                break;
86
            case 'mb':
87
                $metric = new MegaByteMetric($usage);
88
                break;
89
            case 'kb':
90
            default:
91
                $metric = new KiloByteMetric($usage);
92
                break;
93
        }
94
        return $metric->getValue();
95
    }
96
97
    /**
98
     * Format the data given to proper usage for the C3.js provider
99
     *
100
     * @param array $data
101
     * @return array
102
     */
103
    public function formatData($data)
104
    {
105
        $x = array();
106
        $result = array();
107
        foreach($data as $uid => $items )
108
        {
109
            $username = $this->user->getDisplayName($uid);
110
            // For the first item, add to X
111
            if ( count($x) === 0 )
112
            {
113
                $x = $this->getRowX($items);
114
            }
115
            $row = $this->getRowData($items);
116
117
            $result[$username] = $row;
118
        }
119
        $result["x"] = $x;
120
        $result = array_reverse($result);
121
122
        return $result;
123
    }
124
125
    /**
126
     * Get a data row for the X line
127
     *
128
     * @param array $items
129
     * @return array
130
     */
131
    private function getRowX($items)
132
    {
133
        $x = array();
134
        $totalItems = count($items);
135
        for($i = 0; $i < $totalItems; $i++)
136
        {
137
            $x[] = $items[$i]->getDate()->format('Y-m-d');
138
        }
139
        return $x;
140
    }
141
142
    /**
143
     * get Row Data for X/Y axes
144
     *
145
     * @param $items
146
     * @return array
147
     */
148
    private function getRowData($items)
149
    {
150
        $row = array();
151
        $totalItems = count($items);
152
        for($i = 0; $i < $totalItems; $i++)
153
        {
154
            $row[] = $this->calculateUsage($items[$i]->getUsage());
155
        }
156
        return $row;
157
    }
158
}
159