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

AppConfigService::setAppValue()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 16
cts 16
cp 1
rs 8.8571
cc 6
eloc 17
nc 6
nop 2
crap 6
1
<?php
2
3
/**
4
 * Nextcloud - OCR
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later.
7
 * See the COPYING file.
8
 * 
9
 * @author Janis Koehr <[email protected]>
10
 * @copyright Janis Koehr 2017
11
 */
12
namespace OCA\Ocr\Service;
13
14
use \OCP\IConfig;
15
use OCP\IL10N;
16
use OCA\Ocr\Constants\OcrConstants;
17
18
19
class AppConfigService {
20
21
    /**
22
     *
23
     * @var string
24
     */
25
    private $appName = 'ocr';
26
27
    /**
28
     *
29
     * @var IConfig
30
     */
31
    private $config;
32
33
    /**
34
     *
35
     * @var IL10N
36
     */
37
    private $l10n;
38
39
    /**
40
     * Constructor
41
     * 
42
     * @param IConfig $config            
43
     * @param IL10N $l10n            
44
     */
45 30
    public function __construct(IConfig $config, IL10N $l10n) {
46 30
        $this->config = $config;
47 30
        $this->l10n = $l10n;
48 30
    }
49
50
    /**
51
     * Get a value by key
52
     * 
53
     * @param string $key            
54
     * @return string
55
     */
56 2
    public function getAppValue($key) {
57 2
        return $this->config->getAppValue($this->appName, $key);
58
    }
59
60
    /**
61
     * Evaluate if all redis related settings are set.
62
     * 
63
     * @return boolean
64
     */
65 4
    public function evaluateRedisSettings() {
66 4
        if ($this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_HOST) !== '' &&
67 4
                 $this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_PORT) !== '' &&
68 4
                 $this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_DB) !== '') {
69 1
            return true;
70
        } else {
71 3
            return false;
72
        }
73
    }
74
75
    /**
76
     * Set a value for the given key.
77
     * 
78
     * @param string $key            
79
     * @param string $value            
80
     * @return string
81
     * @throws NotFoundException
82
     */
83 19
    public function setAppValue($key, $value) {
84 19
        if(empty($key)) {
85 1
            throw new NotFoundException($this->l10n->t('The given settings key is empty.'));
86
        }
87
        switch ($key) {
88 18
            case OcrConstants::LANGUAGES_CONFIG_KEY :
89 4
                $this->checkLanguages($value);
90 1
                break;
91 14
            case OcrConstants::REDIS_CONFIG_KEY_HOST :
92 6
                $this->checkRedisHost($value);
93 3
                break;
94 8
            case OcrConstants::REDIS_CONFIG_KEY_PORT :
95 5
                $this->checkRedisPort($value);
96 1
                break;
97 3
            case OcrConstants::REDIS_CONFIG_KEY_DB :
98 3
                $this->checkRedisDb($value);
99 1
                break;
100
        }
101 6
        return $this->config->setAppValue($this->appName, $key, $value);
102
    }
103
104
    /**
105
     * Checks for the right languages format.
106
     * 
107
     * @param string $value            
108
     * @throws NotFoundException
109
     */
110 4
    private function checkLanguages($value) {
111 4 View Code Duplication
        if (empty($value) || !preg_match('/^(([a-z]{3,4}|[a-z]{3,4}\-[a-z]{3,4});)*([a-z]{3,4}|[a-z]{3,4}\-[a-z]{3,4})$/', $value)) {
112 3
            throw new NotFoundException($this->l10n->t('The languages are not specified in the correct format.'));
113
        }
114 1
    }
115
116
    /**
117
     * Checks for the right redis host format.
118
     * 
119
     * @param string $value            
120
     * @throws NotFoundException
121
     */
122 6
    private function checkRedisHost($value) {
123 6 View Code Duplication
        if (empty($value) || !preg_match(
124 6
                '/(^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$)/', 
125
                $value)) {
126 3
            throw new NotFoundException($this->l10n->t('The redis host is not specified in the correct format.'));
127
        }
128 3
    }
129
130
    /**
131
     * Checks for the right redis port format.
132
     * 
133
     * @param string $value            
134
     * @throws NotFoundException
135
     */
136 5
    private function checkRedisPort($value) {
137 5 View Code Duplication
        if (empty($value) || !($value > 0 && $value < 65535)) {
138 4
            throw new NotFoundException($this->l10n->t('The redis port number is not specified in the correct format.'));
139
        }
140 1
    }
141
142
    /**
143
     * Checks for the right redis db format.
144
     * 
145
     * @param string $value            
146
     * @throws NotFoundException
147
     */
148 3
    private function checkRedisDb($value) {
149 3 View Code Duplication
        if ($value === '' || !preg_match('/^\d*$/', $value)) {
150 2
            throw new NotFoundException($this->l10n->t('The redis db is not specified in the correct format.'));
151
        }
152
    }
153
}