AppConfigService::checkRedisPort()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 5

Duplication

Lines 3
Ratio 60 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 3
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 4
nc 2
nop 1
crap 4
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
use OCA\Ocr\Util\RedisUtil;
18
use OCP\ILogger;
19
20
21
class AppConfigService {
22
23
    /**
24
     *
25
     * @var IConfig
26
     */
27
    private $config;
28
29
    /**
30
     *
31
     * @var IL10N
32
     */
33
    private $l10n;
34
35
    /**
36
     *
37
     * @var RedisUtil
38
     */
39
    private $redisUtil;
40
41
    /**
42
     *
43
     * @var ILogger
44
     */
45
    private $logger;
46
47
    /**
48
     * Constructor
49
     * 
50
     * @param IConfig $config            
51
     * @param IL10N $l10n            
52
     * @param RedisUtil $redisUtil            
53
     * @param ILogger $logger            
54
     */
55 32
    public function __construct(IConfig $config, IL10N $l10n, RedisUtil $redisUtil, ILogger $logger) {
56 32
        $this->config = $config;
57 32
        $this->l10n = $l10n;
58 32
        $this->redisUtil = $redisUtil;
59 32
        $this->logger = $logger;
60 32
    }
61
62
    /**
63
     * Get a value by key
64
     * 
65
     * @param string $key            
66
     * @return string
67
     */
68 2
    public function getAppValue($key) {
69 2
        return $this->config->getAppValue(OcrConstants::APP_NAME, $key);
70
    }
71
72
    /**
73
     * Evaluate if all needed redis related settings are set.
74
     * 
75
     * @return boolean
76
     */
77 4
    public function evaluateRedisSettings() {
78 4
        if ($this->config->getAppValue(OcrConstants::APP_NAME, OcrConstants::REDIS_CONFIG_KEY_HOST) !== '' &&
79 4
                $this->config->getAppValue(OcrConstants::APP_NAME, OcrConstants::REDIS_CONFIG_KEY_PORT) !== '' &&
80 4
                        $this->config->getAppValue(OcrConstants::APP_NAME, OcrConstants::REDIS_CONFIG_KEY_DB) !== '') {
81
            // test the connection and authenticate
82 1
            $this->redisUtil->setupRedisInstance();
83 1
            return true;
84
        } else {
85 3
            throw new NotFoundException($this->l10n->t('Please setup Redis in the administration settings first.'));
86
        }
87
    }
88
89
    /**
90
     * Set a value for the given key.
91
     * 
92
     * @param string $key            
93
     * @param string $value            
94
     * @return string
95
     * @throws NotFoundException
96
     */
97 21
    public function setAppValue($key, $value) {
98 21
        if (empty($key)) {
99 1
            throw new NotFoundException($this->l10n->t('The given settings key is empty.'));
100
        }
101
        switch ($key) {
102 20
            case OcrConstants::LANGUAGES_CONFIG_KEY :
103 5
                $this->checkLanguages($value);
104 3
                break;
105 15
            case OcrConstants::REDIS_CONFIG_KEY_HOST :
106 6
                $this->checkRedisHost($value);
107 3
                break;
108 9
            case OcrConstants::REDIS_CONFIG_KEY_PORT :
109 5
                $this->checkRedisPort($value);
110 1
                break;
111 4
            case OcrConstants::REDIS_CONFIG_KEY_DB :
112 3
                $this->checkRedisDb($value);
113 1
                break;
114
        }
115 9
        return $this->config->setAppValue(OcrConstants::APP_NAME, $key, $value);
116
    }
117
118
    /**
119
     * Checks for the right languages format.
120
     * 
121
     * @param string $value            
122
     * @throws NotFoundException
123
     */
124 5
    private function checkLanguages($value) {
125 5 View Code Duplication
        if (empty($value) ||
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126 5
                 !preg_match('/^[a-zA-Z\_\-\/]+(\;[a-zA-Z\_\-\/]+)*$/', $value)) {
127 2
            throw new NotFoundException($this->l10n->t('The languages are not specified in the correct format.'));
128
        }
129 3
    }
130
131
    /**
132
     * Checks for the right Redis host format.
133
     * 
134
     * @param string $value            
135
     * @throws NotFoundException
136
     */
137 6
    private function checkRedisHost($value) {
138 6 View Code Duplication
        if (empty($value) || !preg_match(
139 5
                '/(^(([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])$)/', 
140 6
                $value)) {
141 3
            throw new NotFoundException($this->l10n->t('The Redis host is not specified in the correct format.'));
142
        }
143 3
    }
144
145
    /**
146
     * Checks for the right Redis port format.
147
     * 
148
     * @param string $value            
149
     * @throws NotFoundException
150
     */
151 5
    private function checkRedisPort($value) {
152 5 View Code Duplication
        if (empty($value) || !($value > 0 && $value < 65535)) {
153 4
            throw new NotFoundException($this->l10n->t('The Redis port number is not specified in the correct format.'));
154
        }
155 1
    }
156
157
    /**
158
     * Checks for the right Redis DB format.
159
     * 
160
     * @param string $value            
161
     * @throws NotFoundException
162
     */
163 3
    private function checkRedisDb($value) {
164 3 View Code Duplication
        if ($value === '' || !preg_match('/^\d*$/', $value)) {
165 2
            throw new NotFoundException($this->l10n->t('The Redis DB is not specified in the correct format.'));
166
        }
167
    }
168
}