getChartUsageForUpdate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\DataProviders\Activity;
25
26
use OCA\ocUsageCharts\DataProviders\DataProviderInterface;
27
use OCA\ocUsageCharts\Entity\Activity\Collections\ActivityDayCollection;
28
use OCA\ocUsageCharts\Entity\Activity\ActivityUsageRepository;
29
use OCA\ocUsageCharts\Entity\ChartConfig;
30
use OCA\ocUsageCharts\Owncloud\User;
31
use OCA\ocUsageCharts\Owncloud\Users;
32
33
/**
34
 * @author Arno van Rossum <[email protected]>
35
 */
36
class ActivityUsageLastMonthProvider implements DataProviderInterface
37
{
38
    /**
39
     * @var ChartConfig
40
     */
41
    protected $chartConfig;
42
43
    /**
44
     * @var ActivityUsageRepository
45
     */
46
    protected $repository;
47
48
    /**
49
     * @var User
50
     */
51
    protected $user;
52
53
    /**
54
     * @var Users
55
     */
56
    protected $users;
57
58
    /**
59
     * @param ChartConfig $chartConfig
60
     * @param ActivityUsageRepository $repository
61
     * @param User $user
62
     * @param Users $users
63
     */
64
    public function __construct(ChartConfig $chartConfig, ActivityUsageRepository $repository, User $user, Users $users)
65
    {
66
        $this->chartConfig = $chartConfig;
67
        $this->repository = $repository;
68
        $this->user = $user;
69
        $this->users = $users;
70
    }
71
72
    /**
73
     * Return the chart data you want to return based on the ChartConfig
74
     *
75
     * @return array
76
     */
77
    public function getChartUsage()
78
    {
79
        $return = array();
80
        if ( $this->user->isAdminUser($this->user->getSignedInUsername()) )
81
        {
82
            $users = $this->users->getSystemUsers();
83
            foreach($users as $username)
84
            {
85
                $return[$username] = $this->getCollectionByUsername($username);
86
            }
87
        }
88
        else
89
        {
90
            $username = $this->chartConfig->getUsername();
91
            $return[$username] = $this->getCollectionByUsername($username);
92
        }
93
        return $return;
94
    }
95
96
    /**
97
     * Return a collection off 1 month ago based on the username supplied
98
     *
99
     * @param string $username
100
     * @return ActivityDayCollection
101
     */
102
    private function getCollectionByUsername($username)
103
    {
104
        $created = new \DateTime("-1 month");
105
        $data = $this->repository->findAfterCreatedByUsername($created, $username);
106
        $collection = new ActivityDayCollection();
107
        foreach($data as $activityUsage)
108
        {
109
            $collection->add($activityUsage);
110
        }
111
        return $collection;
112
    }
113
114
    /**
115
     * NEVER UPDATE, this is handled by activity app
116
     *
117
     * @return boolean
118
     */
119
    public function isAllowedToUpdate()
120
    {
121
        return false;
122
    }
123
124
    /**
125
     * See isAllowedToUpdate for more information
126
     *
127
     * @return null
128
     */
129
    public function getChartUsageForUpdate()
130
    {
131
        return null;
132
    }
133
134
    /**
135
     * See isAllowedToUpdate for more information
136
     *
137
     * @param $usage
138
     * @return boolean
139
     */
140
    public function save($usage)
141
    {
142
        return false;
143
    }
144
}