Passed
Push — settings-service ( f9bdf2...94e527 )
by Matias
04:00
created

SettingsController::getAppValue()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 7
nop 1
dl 0
loc 36
ccs 0
cts 31
cp 0
crap 56
rs 8.5546
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019-2020 Matias De lellis <[email protected]>
4
 *
5
 * @author Matias De lellis <[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\FaceRecognition\Controller;
25
26
use OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http\JSONResponse;
28
use OCP\IRequest;
29
use OCP\IUserManager;
30
use OCP\IUser;
31
use OCP\IL10N;
32
33
use OCA\FaceRecognition\Helper\MemoryLimits;
34
use OCA\FaceRecognition\Service\SettingsService;
35
36
class SettingsController extends Controller {
37
38
	/** @var SettingsService */
39
	private $settingsService;
40
41
	/** @var \OCP\IL10N */
42
	protected $l10n;
43
44
	/** @var IUserManager */
45
	private $userManager;
46
47
	/** @var string */
48
	private $userId;
49
50
	const STATE_OK = 0;
51
	const STATE_FALSE = 1;
52
	const STATE_SUCCESS = 2;
53
	const STATE_ERROR = 3;
54
55
	public function __construct ($appName,
56
	                             IRequest        $request,
57
	                             SettingsService $settingsService,
58
	                             IL10N           $l10n,
59
	                             IUserManager    $userManager,
60
	                             $userId)
61
	{
62
		parent::__construct($appName, $request);
63
64
		$this->appName         = $appName;
65
		$this->settingsService = $settingsService;
66
		$this->l10n            = $l10n;
67
		$this->userManager     = $userManager;
68
		$this->userId          = $userId;
69
	}
70
71
	/**
72
	 * @NoAdminRequired
73
	 * @param $type
74
	 * @param $value
75
	 * @return JSONResponse
76
	 */
77
	public function setUserValue($type, $value) {
78
		$status = self::STATE_SUCCESS;
79
		switch ($type) {
80
			case 'enabled':
81
				$enabled = ($value === 'true');
82
				$this->settingsService->setUserEnabled($enabled);
83
				if ($enabled) {
84
					$this->settingsService->setUserFullScanDone(false);
85
				}
86
				break;
87
			default:
88
				$status = self::STATE_ERROR;
89
				break;
90
		}
91
92
		// Response
93
		$result = [
94
			'status' => $status,
95
			'value' => $value
96
		];
97
		return new JSONResponse($result);
98
	}
99
100
	/**
101
	 * @NoAdminRequired
102
	 * @param $type
103
	 * @return JSONResponse
104
	 */
105
	public function getUserValue($type) {
106
		$status = self::STATE_OK;
107
		$value ='nodata';
108
		switch ($type) {
109
			case 'enabled':
110
				$value = $this->settingsService->getUserEnabled();
111
				break;
112
			default:
113
				$status = self::STATE_FALSE;
114
				break;
115
		}
116
		$result = [
117
			'status' => $status,
118
			'value' => $value
119
		];
120
		return new JSONResponse($result);
121
	}
122
123
	/**
124
	 * @param $type
125
	 * @param $value
126
	 * @return JSONResponse
127
	 */
128
	public function setAppValue($type, $value) {
129
		$status = self::STATE_SUCCESS;
130
		$message = "";
131
		switch ($type) {
132
			case 'sensitivity':
133
				$this->settingsService->setSensitivity ($value);
134
				$this->userManager->callForSeenUsers(function(IUser $user) {
135
					$this->settingsService->setNeedRecreateClusters(true, $user->getUID());
136
				});
137
				break;
138
			case 'min-confidence':
139
				$this->settingsService->setMinimumConfidence ($value);
140
				$this->userManager->callForSeenUsers(function(IUser $user) {
141
					$this->settingsService->setNeedRecreateClusters(true, $user->getUID());
142
				});
143
				break;
144
			case 'memory-limits':
145
				if (!is_numeric ($value)) {
146
					$status = self::STATE_ERROR;
147
					$message = $this->l10n->t("The format seems to be incorrect.");
148
					$value = '-1';
149
				}
150
				// Apply prundent limits.
151
				if ($value < SettingsService::MINIMUM_MEMORY_LIMITS) {
152
					$value = SettingsService::MINIMUM_MEMORY_LIMITS;
153
					$message = $this->l10n->t("Recommended memory for analysis is at least 1GB of RAM.");
154
					$status = self::STATE_ERROR;
155
				} else if ($value > SettingsService::MAXIMUM_MEMORY_LIMITS) {
156
					$value = SettingsService::MAXIMUM_MEMORY_LIMITS;
157
					$message = $this->l10n->t("We are not recommending using more than 4GB of RAM memory, as we see little to no benefit.");
158
					$status = self::STATE_ERROR;
159
				}
160
				// Valid according to RAM of php.ini setting.
161
				$memory = MemoryLimits::getAvailableMemory();
162
				if ($value > $memory) {
163
					$value = $memory;
164
					$message = $this->l10n->t("According to your system you can not use more than %s GB of RAM", ($value / 1024 / 1024 / 1024));
165
					$status = self::STATE_ERROR;
166
				}
167
				// If any validation error saves the value
168
				if ($status !== self::STATE_ERROR) {
169
					$message = $this->l10n->t("The changes were saved. It will be taken into account in the next analysis.");
170
					$this->settingsService->setMemoryLimits($value);
171
				}
172
				break;
173
			case 'show-not-grouped':
174
				$this->settingsService->setShowNotGrouped($value == 'true' ? true : false);
175
				break;
176
			default:
177
				break;
178
		}
179
180
		// Response
181
		$result = [
182
			'status' => $status,
183
			'message' => $message,
184
			'value' => $value
185
		];
186
		return new JSONResponse($result);
187
	}
188
189
	/**
190
	 * @param $type
191
	 * @return JSONResponse
192
	 */
193
	public function getAppValue($type) {
194
		$value = 'nodata';
195
		$status = self::STATE_OK;
196
		switch ($type) {
197
			case 'sensitivity':
198
				$value = $this->settingsService->getSensitivity();
199
				break;
200
			case 'min-confidence':
201
				$value = $this->settingsService->getMinimumConfidence();
202
				break;
203
			case 'memory-limits':
204
				$value = $this->settingsService->getMemoryLimits();
205
				// If it was not configured, returns the default
206
				// values used by the background task as a reference.
207
				if ($value == SettingsService::DEFAULT_MEMORY_LIMITS) {
208
					$memory = MemoryLimits::getAvailableMemory();
209
					if ($memory > SettingsService::MAXIMUM_MEMORY_LIMITS)
210
						$memory = SettingsService::MAXIMUM_MEMORY_LIMITS;
211
					$value = $memory;
212
					$status = self::STATE_FALSE;
213
				}
214
				break;
215
			case 'show-not-grouped':
216
				$value = $this->settingsService->getShowNotGrouped();
217
				break;
218
			default:
219
				break;
220
		}
221
222
		// Response
223
		$result = [
224
			'status' => $status,
225
			'value' => $value
226
		];
227
228
		return new JSONResponse($result);
229
	}
230
231
}
232