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

AppConfigService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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\Service;
12
13
use \OCP\IConfig;
14
use OCP\IL10N;
15
use OCA\Ocr\Constants\OcrConstants;
16
17
18
class AppConfigService {
19
20
    /**
21
     *
22
     * @var string
23
     */
24
    private $appName = 'ocr';
25
26
    /**
27
     *
28
     * @var IConfig
29
     */
30
    private $config;
31
32
    /**
33
     *
34
     * @var IL10N
35
     */
36
    private $l10n;
37
38
    /**
39
     * Constructor
40
     * 
41
     * @param IConfig $config            
42
     * @param IL10N $l10n            
43
     */
44 5
    public function __construct(IConfig $config, IL10N $l10n) {
45 5
        $this->config = $config;
46 5
        $this->l10n = $l10n;
47 5
    }
48
49
    /**
50
     * Get a value by key
51
     * 
52
     * @param string $key            
53
     * @return string
54
     */
55
    public function getAppValue($key) {
56
        return $this->config->getAppValue($this->appName, $key);
57
    }
58
59
    /**
60
     * Evaluate if all redis related settings are set.
61
     * 
62
     * @return boolean
63
     */
64
    public function evaluateRedisSettings() {
65
        if ($this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_HOST) !== '' &&
66
                 $this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_PORT) !== '' &&
67
                 $this->config->getAppValue($this->appName, OcrConstants::REDIS_CONFIG_KEY_DB) !== '') {
68
            return true;
69
        } else {
70
            return false;
71
        }
72
    }
73
74
    /**
75
     * Set a value by key
76
     * TODO: switch case and too much if statements
77
     * @param string $key            
78
     * @param string $value            
79
     * @return string
80
     */
81
    public function setAppValue($key, $value) {
82
        if ($key === OcrConstants::LANGUAGES_CONFIG_KEY) {
83
            if (!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)) {
84
                throw new NotFoundException($this->l10n->t('The languages are not specified in the correct format.'));
85
            }
86
        }
87
        if ($key === OcrConstants::REDIS_CONFIG_KEY_HOST) {
88
            if (!preg_match(
89
                    '/(^(([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])$)/', 
90
                    $value)) {
91
                throw new NotFoundException($this->l10n->t('The redis host is not specified in the correct format.'));
92
            }
93
        }
94 View Code Duplication
        if ($key === OcrConstants::REDIS_CONFIG_KEY_PORT) {
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...
95
            $value = intval($value);
96
            if (!($value > 0 && $value < 65535)) {
97
                throw new NotFoundException(
98
                        $this->l10n->t('The redis port number is not specified in the correct format.'));
99
            }
100
        }
101 View Code Duplication
        if ($key === OcrConstants::REDIS_CONFIG_KEY_DB) {
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...
102
            $value = intval($value);
103
            if (!($value >= 0)) {
104
                throw new NotFoundException($this->l10n->t('The redis db is not specified in the correct format.'));
105
            }
106
        }
107
        return $this->config->setAppValue($this->appName, $key, $value);
108
    }
109
}