parseUsernamesToRow()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
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\Activity;
25
26
use OCA\ocUsageCharts\Adapters\ChartTypeAdapterInterface;
27
use OCA\ocUsageCharts\Adapters\c3js\c3jsBase;
28
29
/**
30
 * @author Arno van Rossum <[email protected]>
31
 */
32
class ActivityUsagePerMonthAdapter extends c3jsBase implements ChartTypeAdapterInterface
33
{
34
    /**
35
     * This method gives the ability to parse the data in any form you would like
36
     *
37
     * @param $data
38
     * @return mixed
39
     */
40
    public function formatData($data)
41
    {
42
        $x = array();
43
        $result = array();
44
        foreach($data as $date => $usernames)
45
        {
46
            if ( !in_array($date, $x) )
47
            {
48
                $x[] = $date;
49
            }
50
            $this->parseUsernamesToRow($result, $usernames);
51
        }
52
53
        $result["x"] = $x;
54
        $result = array_reverse($result);
55
56
        return $result;
57
    }
58
59
    /**
60
     * @param array $result
61
     * @param array $usernames
62
     * @return array
63
     */
64
    private function parseUsernamesToRow(&$result, array $usernames)
65
    {
66
        foreach($usernames as $uid => $count)
67
        {
68
            $username = $this->user->getDisplayName($uid);
69
            if ( !in_array($username, array_keys($result)) )
70
            {
71
                $result[$username] = array();
72
            }
73
74
            $result[$username][] = $count;
75
        }
76
    }
77
78
}
79