Completed
Pull Request — master (#93)
by Janis
04:18
created

AdminSettingsController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 94
ccs 32
cts 32
cp 1
rs 10
c 1
b 0
f 0

5 Methods

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