Completed
Push — stable9 ( 34bd66...defdb6 )
by Olivier
11:26 queued 07:53
created

ConfigValidator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 12
c 4
b 1
f 0
lcom 1
cbo 0
dl 0
loc 79
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isConfigSafe() 0 15 3
A isSortingSafe() 0 7 4
A isDesignColourSafe() 0 8 3
A sortingValidator() 0 9 2
1
<?php
2
/**
3
 * ownCloud - galleryplus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Olivier Paroz <[email protected]>
9
 *
10
 * @copyright Olivier Paroz 2016
11
 */
12
13
namespace OCA\GalleryPlus\Config;
14
15
/**
16
 * Validates parsed configuration elements
17
 *
18
 * @package OCA\GalleryPlus\Config
19
 */
20
class ConfigValidator {
21
22
	/**
23
	 * Determines if the content of that sub-section is safe for web use
24
	 *
25
	 * @param string $key the configuration sub-section identifier
26
	 * @param array $parsedConfigItem the configuration for a sub-section
27
	 *
28
	 * @return bool
29
	 */
30 21
	public function isConfigSafe($key, $parsedConfigItem) {
31 21
		$safe = true;
32
33
		switch ($key) {
34 21
			case 'sorting':
35 13
				$safe = $this->isSortingSafe('type', $parsedConfigItem, $safe);
36 13
				$safe = $this->isSortingSafe('order', $parsedConfigItem, $safe);
37 13
				break;
38 14
			case 'design':
39 9
				$safe = $this->isDesignColourSafe($parsedConfigItem, $safe);
40 9
				break;
41
		}
42
43 21
		return $safe;
44
	}
45
46
	/**
47
	 * Determines if the sorting type found in the config file is safe for web use
48
	 *
49
	 * @param string $key will specify the key to check 'type' or 'order'
50
	 * @param array $parsedConfigItem the sorting configuration to analyse
51
	 * @param bool $safe whether the current config has been deemed safe to use so far
52
	 *
53
	 * @return bool
54
	 */
55 13
	private function isSortingSafe($key, $parsedConfigItem, $safe) {
56 13
		if ($safe && array_key_exists($key, $parsedConfigItem)) {
57 12
			$safe = $safe && $this->sortingValidator($key, $parsedConfigItem[$key]);
58
		}
59
60 13
		return $safe;
61
	}
62
63
	/**
64
	 * Determines if the background colour found in the config file is safe for web use
65
	 *
66
	 * @param array $parsedConfigItem the design configuration to analyse
67
	 * @param bool $safe whether the current config has been deemed safe to use so far
68
	 *
69
	 * @return bool
70
	 */
71 9
	private function isDesignColourSafe($parsedConfigItem, $safe) {
72 9
		if (array_key_exists('background', $parsedConfigItem)) {
73 8
			$background = $parsedConfigItem['background'];
74 8
			$safe = $safe && ctype_xdigit(substr($background, 1));
75
		}
76
77 9
		return $safe;
78
	}
79
80
	/**
81
	 * Validates the parsed sorting values against allowed values
82
	 *
83
	 * @param string $section the section in the sorting config to be analysed
84
	 * @param string $value the value found in that section
85
	 *
86
	 * @return bool
87
	 */
88 12
	private function sortingValidator($section, $value) {
89 12
		if ($section === 'type') {
90 11
			$validValues = ['date', 'name'];
91
		} else {
92 10
			$validValues = ['des', 'asc'];
93
		}
94
95 12
		return in_array($value, $validValues);
96
	}
97
98
}
99