Completed
Pull Request — master (#93)
by Janis
14:36
created

AdminSettingsController::setRedis()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 9
nc 1
nop 3
1
<?php
2
3
/**
4
 * Nextcloud - OCR
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Janis Koehr <[email protected]>
10
 * @copyright Janis Koehr 2017
11
 */
12
namespace OCA\Ocr\Controller;
13
14
use OCP\AppFramework\Controller;
15
use OCP\IRequest;
16
use OCP\IL10N;
17
use OCA\Ocr\Config\AppConfig;
18
use OCP\AppFramework\Http\DataResponse;
19
use OCA\Ocr\Service\AppConfigService;
20
use OCA\Ocr\Service\NotFoundException;
21
use OCA\Ocr\Constants\OcrConstants;
22
23
class AdminSettingsController extends Controller {
24
	
25
	/** @var IL10N */
26
	private $l10n;
27
	
28
	/** @var AppConfigService */
29
	private $appConfig;
30
	
31
	use Errors;
32
	
33
	/**
34
	 * Constructor
35
	 *
36
	 * @param string $appName        	
37
	 * @param IRequest $request        	
38
	 * @param IL10N $l10n        	
39
	 * @param AppConfigService $appConfig        	
40
	 * @param string $userId        	
41
	 */
42
	public function __construct($appName, IRequest $request, IL10N $l10n, AppConfigService $appConfig, $userId) {
43
		parent::__construct ( $appName, $request );
44
		$this->l10n = $l10n;
45
		$this->appConfig = $appConfig;
46
	}
47
	
48
	/**
49
	 * @NoAdminRequired
50
	 *
51
	 * @return DataResponse
52
	 */
53
	public function getLanguages() {
54
		return $this->handleNotFound ( function () {
55
			return [ 'languages' => $this->appConfig->getAppValue ( OcrConstants::LANGUAGES_CONFIG_KEY )];
56
		} );
57
	}
58
	
59
	/**
60
	 * @NoAdminRequired
61
	 *
62
	 * @return DataResponse
63
	 */
64
	public function evaluateRedisSettings() {
65
		return $this->handleNotFound ( function () {
66
			return [ 'set' => $this->appConfig->evaluateRedisSettings ()];
67
		} );
68
	}
69
	
70
	/**
71
	 * Sets the languages that are supported by the docker worker instance.
72
	 *
73
	 * @param string $languages
74
	 * @return DataResponse
75
	 */
76
	public function setLanguages($languages) {
77
		return $this->handleNotFound ( function () use ($languages) {
78
			if ($languages !== null) {
79
				$this->appConfig->setAppValue ( OcrConstants::LANGUAGES_CONFIG_KEY, $languages );
80
				return $this->l10n->t ( 'Saved' );
81
			} else {
82
				throw new NotFoundException ( $this->l10n->t ( 'The languages are not specified in the correct format.' ) );
83
			}
84
		} );
85
	}
86
	
87
	/**
88
	 * Sets the Redis settings.
89
	 *
90
	 * @param string $redisHost
91
	 * @param string $redisPort
92
	 * @param string $redisDb
93
	 * @return DataResponse
94
	 */
95
	public function setRedis($redisHost, $redisPort, $redisDb) {
96
		return $this->handleNotFound ( function () use ($redisHost, $redisPort, $redisDb) {
97
			if ($redisHost !== null && $redisPort !== null && $redisDb !== null) {
98
				$this->appConfig->setAppValue ( OcrConstants::REDIS_CONFIG_KEY_HOST, $redisHost );
99
				$this->appConfig->setAppValue ( OcrConstants::REDIS_CONFIG_KEY_PORT, $redisPort );
100
				$this->appConfig->setAppValue ( OcrConstants::REDIS_CONFIG_KEY_DB, $redisDb );
101
				return $this->l10n->t ( 'Saved' );
102
			} else {
103
				throw new NotFoundException ( $this->l10n->t ( 'The Redis settings do not have the right format.' ) );
104
			}
105
		} );
106
	}
107
}