ActivityUsageLastMonthAdapter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 4 Features 0
Metric Value
wmc 10
c 7
b 4
f 0
lcom 1
cbo 4
dl 0
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A formatData() 0 22 3
A getRowX() 0 11 3
A getRowData() 0 10 2
A calculateTotalCountByCollection() 0 11 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
use OCA\ocUsageCharts\Entity\Activity\Collections\ActivityDayCollection;
29
30
/**
31
 * @author Arno van Rossum <[email protected]>
32
 */
33
class ActivityUsageLastMonthAdapter extends c3jsBase implements ChartTypeAdapterInterface
34
{
35
    /**
36
     * @param array $data
37
     * @return array
38
     */
39
    public function formatData($data)
40
    {
41
        $x = array();
42
        $result = array();
43
44
        /** @var ActivityDayCollection $collection */
45
        foreach(array_values($data) as $collection)
46
        {
47
            $x = $this->getRowX($x, $collection);
48
        }
49
        /** @var ActivityDayCollection $collection */
50
        foreach($data as $uid => $collection)
51
        {
52
            $username = $this->user->getDisplayName($uid);
53
            $row = $this->getRowData($x, $collection);
54
            $result[$username] = $row;
55
        }
56
        $result["x"] = $x;
57
        $result = array_reverse($result);
58
59
        return $result;
60
    }
61
62
    /**
63
     * Get a data row for the X line
64
     *
65
     * @param array $x
66
     * @param ActivityDayCollection $collection
67
     * @return array
68
     */
69
    private function getRowX(array $x, $collection)
70
    {
71
        foreach($collection as $date => $subjectCollection)
72
        {
73
            if ( !in_array($date, $x) )
74
            {
75
                $x[] = $date;
76
            }
77
        }
78
        return $x;
79
    }
80
81
    /**
82
     * get Row Data for X/Y axes
83
     *
84
     * @param array $x
85
     * @param ActivityDayCollection $collection
86
     * @return array
87
     */
88
    private function getRowData($x, $collection)
89
    {
90
        $row = array();
91
        $total = count($x);
92
        for($i = 0; $i < $total; $i++)
93
        {
94
            $row[$i] = $this->calculateTotalCountByCollection(new \DateTime($x[$i]), $collection);
95
        }
96
        return $row;
97
    }
98
99
    /**
100
     * @param \DateTime $date
101
     * @param ActivityDayCollection $collection
102
     * @return integer
103
     */
104
    private function calculateTotalCountByCollection(\DateTime $date, $collection)
105
    {
106
        $return = 0;
107
        $subjectCollection = $collection->getByDate($date);
108
        if ( !is_null($subjectCollection) )
109
        {
110
            $return = $subjectCollection->totalCount();
111
        }
112
113
        return $return;
114
    }
115
}
116