Completed
Push — master ( b5fad3...ebe77a )
by Julius
45:12 queued 43:40
created

AppConfig   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 93
Duplicated Lines 12.9 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 12
loc 93
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAppNamespace() 0 6 2
A getAppValue() 0 7 2
A setAppValue() 0 3 1
B getAppSettings() 12 19 8
A getAppValueArray() 0 7 4

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
 * ownCloud - Richdocuments App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2015 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Richdocuments;
13
14
use OCA\Richdocuments\AppInfo\Application;
15
use \OCP\IConfig;
16
17
class AppConfig {
18
19
	private $defaults = [
20
		'wopi_url' => '',
21
		'watermark_text' => '{userId}',
22
		'watermark_allGroupsList' => [],
23
		'watermark_allTagsList' => [],
24
		'watermark_linkTagsList' => [],
25
26
	];
27
28
	const WATERMARK_APP_NAMESPACE = 'files';
29
30
	const APP_SETTING_TYPES = [
31
			'watermark_allGroupsList' => 'array',
32
			'watermark_allTagsList' => 'array',
33
			'watermark_linkTagsList' => 'array'
34
		];
35
36
	/** @var IConfig */
37
	private $config;
38
39
	public function __construct(IConfig $config) {
40
		$this->config = $config;
41
	}
42
43
	public function getAppNamespace($key) {
44
		if (strpos($key, 'watermark_') === 0) {
45
			return self::WATERMARK_APP_NAMESPACE;
46
		}
47
		return Application::APPNAME;
48
	}
49
50
	/**
51
	 * Get a value by key
52
	 * @param string $key
53
	 * @return string
54
	 */
55
	public function getAppValue($key) {
56
		$defaultValue = null;
57
		if (array_key_exists($key, $this->defaults)){
58
			$defaultValue = $this->defaults[$key];
59
		}
60
		return $this->config->getAppValue($this->getAppNamespace($key), $key, $defaultValue);
61
	}
62
63
	/**
64
	 * @param $key
65
	 * @return array
66
	 */
67
	public function getAppValueArray($key) {
68
		$value = $this->config->getAppValue($this->getAppNamespace($key), $key, []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
		if (array_key_exists($key, self::APP_SETTING_TYPES) && self::APP_SETTING_TYPES[$key] === 'array') {
70
			$value = $value !== '' ? explode(',', $value) : [];
71
		}
72
		return $value;
73
	}
74
75
	/**
76
	 * Set a value by key
77
	 * @param string $key
78
	 * @param string $value
79
	 * @return void
80
	 */
81
	public function setAppValue($key, $value) {
82
		$this->config->setAppValue($this->getAppNamespace($key), $key, $value);
83
	}
84
85
	/**
86
	 * Get all app settings
87
	 * @return array
88
	 */
89
	public function getAppSettings() {
90
		$result = [];
91
		$keys = $this->config->getAppKeys(Application::APPNAME);
92 View Code Duplication
		foreach ($keys as $key) {
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...
93
			$value = $this->getAppValueArray($key);
94
			$value = $value === 'yes' ? true : $value;
95
			$result[$key] = $value === 'no' ? false : $value;
96
		}
97
98
		$keys = $this->config->getAppKeys(self::WATERMARK_APP_NAMESPACE);
99 View Code Duplication
		foreach ($keys as $key) {
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...
100
			if (strpos($key, 'watermark_') === 0) {
101
				$value = $this->getAppValueArray($key);
102
				$value = $value === 'yes' ? true : $value;
103
				$result[$key] = $value === 'no' ? false : $value;
104
			}
105
		}
106
		return $result;
107
	}
108
109
 }
110