StorageUsageBase::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
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\Storage;
25
26
use OCA\ocUsageCharts\DataProviders\DataProviderInterface;
27
use OCA\ocUsageCharts\Entity\ChartConfig;
28
use OCA\ocUsageCharts\Entity\Storage\StorageUsage;
29
use OCA\ocUsageCharts\Entity\Storage\StorageUsageRepository;
30
use OCA\ocUsageCharts\Owncloud\Storage;
31
use OCA\ocUsageCharts\Owncloud\User;
32
33
/**
34
 * @author Arno van Rossum <[email protected]>
35
 */
36
abstract class StorageUsageBase implements DataProviderInterface
37
{
38
    /**
39
     * @var ChartConfig
40
     */
41
    protected $chartConfig;
42
43
    /**
44
     * @var StorageUsageRepository
45
     */
46
    protected $repository;
47
48
    /**
49
     * @var User
50
     */
51
    protected $user;
52
53
    /**
54
     * @var Storage
55
     */
56
    protected $storage;
57
58
    /**
59
     * @param ChartConfig $chartConfig
60
     * @param StorageUsageRepository $repository
61
     * @param User $user
62
     * @param Storage $storage
63
     */
64
    public function __construct(ChartConfig $chartConfig, StorageUsageRepository $repository, User $user, Storage $storage)
65
    {
66
        $this->chartConfig = $chartConfig;
67
        $this->repository = $repository;
68
        $this->user = $user;
69
        $this->storage = $storage;
70
    }
71
72
    /**
73
     * Check if the cron may update.
74
     *
75
     * @return boolean
76
     */
77
    public function isAllowedToUpdate()
78
    {
79
        $userName = $this->chartConfig->getUsername();
80
        $created = new \DateTime();
81
        $created->setTime(0,0,0);
82
        $results = $this->repository->findAfterCreated($userName, $created);
83
84
        // The cron has already ran today, therefor ignoring a current update, only update once a day.
85
        if ( count($results) === 0 )
86
        {
87
            return true;
88
        }
89
        return false;
90
    }
91
92
    /**
93
     * Return a CURRENT storage usage for a user
94
     *
95
     * @return StorageUsage
96
     */
97
    public function getChartUsageForUpdate()
98
    {
99
        $userName = $this->chartConfig->getUsername();
100
        return new StorageUsage(new \Datetime(), $this->storage->getStorageUsage($userName), $userName);
101
    }
102
103
    /**
104
     * Save the usage
105
     *
106
     * @param StorageUsage $usage
107
     * @return boolean
108
     */
109
    public function save($usage)
110
    {
111
        return $this->repository->save($usage);
112
    }
113
}