ChartConfigService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 7
c 6
b 1
f 2
lcom 1
cbo 3
dl 0
loc 69
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getChartsForLoggedInUser() 0 4 1
A getChartsByUsername() 0 4 1
A getChartConfigById() 0 13 3
A save() 0 4 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\Service;
25
26
use OCA\ocUsageCharts\Entity\ChartConfigRepository;
27
use OCA\ocUsageCharts\Entity\ChartConfig;
28
use OCA\ocUsageCharts\Exception\ChartConfigServiceException;
29
use OCA\ocUsageCharts\Owncloud\User;
30
31
/**
32
 * @author Arno van Rossum <[email protected]>
33
 */
34
class ChartConfigService
35
{
36
    /**
37
     * @var ChartConfigRepository
38
     */
39
    private $repository;
40
41
    /**
42
     * @var User
43
     */
44
    private $user;
45
46
    public function __construct(ChartConfigRepository $repository, User $user)
47
    {
48
        $this->repository = $repository;
49
        $this->user = $user;
50
    }
51
52
    /**
53
     * Get all charts for the loggedin user
54
     *
55
     * @return array|\OCA\ocUsageCharts\Entity\ChartConfig[]
56
     */
57
    public function getChartsForLoggedInUser()
58
    {
59
        return $this->repository->findByUsername($this->user->getSignedInUsername());
60
    }
61
62
    /**
63
     * Get all charts by username
64
     *
65
     * @param string $userName
66
     * @return array|\OCA\ocUsageCharts\Entity\ChartConfig[]
67
     */
68
    public function getChartsByUsername($userName)
69
    {
70
        return $this->repository->findByUsername($userName);
71
    }
72
73
    /**
74
     * Get a config by id
75
     *
76
     * @param integer $id
77
     * @return ChartConfig
78
     * @throws ChartConfigServiceException
79
     */
80
    public function getChartConfigById($id)
81
    {
82
        $configs = $this->repository->findByUsername($this->user->getSignedInUsername());
83
        foreach($configs as $config)
84
        {
85
            if ( $config->getId() == $id )
86
            {
87
                return $config;
88
            }
89
        }
90
91
        throw new ChartConfigServiceException("No config found for given id " . $id);
92
    }
93
94
    /**
95
     * @param ChartConfig $config
96
     * @return boolean
97
     */
98
    public function save(ChartConfig $config)
99
    {
100
        return $this->repository->save($config);
101
    }
102
}