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) { |
|
|
|
|
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() { |
|
|
|
|
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
|
|
|
|
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: