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

AppConfigService   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 85
Duplicated Lines 14.12 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 1
dl 12
loc 85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAppValue() 0 3 1
A evaluateRedisSettings() 0 8 4
D setAppValue() 12 25 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Service;
13
14
use \OCP\IConfig;
15
use OCP\IL10N;
16
use OCA\Ocr\Constants\OcrConstants;
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
	public function __construct(IConfig $config, IL10N $l10n) {
45
		$this->config = $config;
46
		$this->l10n = $l10n;
47
	}
48
	/**
49
	 * Get a value by key
50
	 *
51
	 * @param string $key        	
52
	 * @return string
53
	 */
54
	public function getAppValue($key) {
55
		return $this->config->getAppValue ( $this->appName, $key );
56
	}
57
	/**
58
	 * Evaluate if all redis related settings are set.
59
	 *
60
	 * @return boolean
61
	 */
62
	public function evaluateRedisSettings() {
63
		
64
		if ($this->config->getAppValue ( $this->appName, OcrConstants::REDIS_CONFIG_KEY_HOST ) !== '' && $this->config->getAppValue ( $this->appName, OcrConstants::REDIS_CONFIG_KEY_PORT ) !== '' && $this->config->getAppValue ( $this->appName, OcrConstants::REDIS_CONFIG_KEY_DB ) !== '') {
65
			return true;
66
		} else {
67
			return false;
68
		}
69
	}
70
	/**
71
	 * Set a value by key
72
	 *
73
	 * @param string $key        	
74
	 * @param string $value        	
75
	 * @return string
76
	 */
77
	public function setAppValue($key, $value) {
78
		if ($key === OcrConstants::LANGUAGES_CONFIG_KEY) {
79
			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 )) {
80
				throw new NotFoundException ( $this->l10n->t ( 'The languages are not specified in the correct format.' ) );
81
			}
82
		}
83
		if ($key === OcrConstants::REDIS_CONFIG_KEY_HOST) {
84
			if (! preg_match ( '/(^(([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])$)/', $value )) {
85
				throw new NotFoundException ( $this->l10n->t ( 'The redis host is not specified in the correct format.' ) );
86
			}
87
		}
88 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...
89
			$value = intval ( $value );
90
			if (! ($value > 0 && $value < 65535)) {
91
				throw new NotFoundException ( $this->l10n->t ( 'The redis port number is not specified in the correct format.' ) );
92
			}
93
		}
94 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...
95
			$value = intval ( $value );
96
			if (! ($value >= 0)) {
97
				throw new NotFoundException ( $this->l10n->t ( 'The redis db is not specified in the correct format.' ) );
98
			}
99
		}
100
		return $this->config->setAppValue ( $this->appName, $key, $value );
101
	}
102
}