ConfigController::getGroupLimitList()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Controller;
25
26
use OCA\Deck\Service\DefaultBoardService;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\AppFramework\Http\NotFoundResponse;
29
use OCP\IConfig;
30
use OCP\IGroup;
31
use OCP\IGroupManager;
32
use OCP\IRequest;
33
use OCP\AppFramework\Http\TemplateResponse;
34
use OCP\AppFramework\Controller;
35
use OCP\IL10N;
36
37
class ConfigController extends Controller {
38
39
	private $config;
40
	private $userId;
41
	private $groupManager;
42
43
	public function __construct(
44
		$AppName,
45
		IRequest $request,
46
		IConfig $config,
47
		IGroupManager $groupManager,
48
		$userId
49
		) {
50
		parent::__construct($AppName, $request);
51
52
		$this->userId = $userId;
53
		$this->groupManager = $groupManager;
54
		$this->config = $config;
55
	}
56
57
	/**
58
	 * @NoCSRFRequired
59
	 */
60
	public function get() {
61
		$data = [
62
			'groupLimit' => $this->getGroupLimit(),
63
		];
64
		return new DataResponse($data);
65
	}
66
67
	/**
68
	 * @NoCSRFRequired
69
	 */
70
	public function setValue($key, $value) {
71
		switch ($key) {
72
			case 'groupLimit':
73
				$result = $this->setGroupLimit($value);
74
				break;
75
		}
76
		if ($result === null) {
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
77
			return new NotFoundResponse();
78
		}
79
		return new DataResponse($result);
80
	}
81
82
	private function setGroupLimit($value) {
83
		$groups = [];
84
		foreach ($value as $group) {
85
			$groups[] = $group['id'];
86
		}
87
		$data = implode(',', $groups);
88
		$this->config->setAppValue($this->appName, 'groupLimit', $data);
89
		return $groups;
90
	}
91
92 View Code Duplication
	private function getGroupLimitList() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
		$value = $this->config->getAppValue($this->appName, 'groupLimit', '');
94
		$groups = explode(',', $value);
95
		if ($value === '') {
96
			return [];
97
		}
98
		return $groups;
99
	}
100
101
	private function getGroupLimit() {
102
		$groups = $this->getGroupLimitList();
103
		$groups = array_map(function($groupId) {
104
			/** @var IGroup $groups */
105
			$group = $this->groupManager->get($groupId);
106
			if ($group === null) {
107
				return null;
108
			}
109
			return [
110
				'id' => $group->getGID(),
111
				'displayname' => $group->getDisplayName(),
112
			];
113
		}, $groups);
114
		return array_filter($groups);
115
	}
116
117
}
118