Completed
Pull Request — master (#39)
by Olivier
08:23 queued 38s
created

ConfigValidator::isSortingSafe()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.2
cc 4
eloc 4
nc 3
nop 3
crap 4
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 2015
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
	 * @param string  will specify the key to check 'type' or 'order'
49
	 * @param array $parsedConfigItem the sorting configuration to analyse
50
	 * @param bool $safe whether the current config has been deemed safe to use so far
51
	 * @return bool
52
	 */
53 13
	private function isSortingSafe($key,$parsedConfigItem, $safe) {
54 13
		if ($safe && array_key_exists($key, $parsedConfigItem)) {
55 12
			$safe = $safe && $this->sortingValidator($key, $parsedConfigItem[ $key ]);
56 8
		}
57
58 13
		return $safe;
59
	}
60
61
	/**
62
	 * Determines if the background colour found in the config file is safe for web use
63
	 *
64
	 * @param array $parsedConfigItem the design configuration to analyse
65
	 * @param bool $safe whether the current config has been deemed safe to use so far
66
	 *
67
	 * @return bool
68
	 */
69 9
	private function isDesignColourSafe($parsedConfigItem, $safe) {
70 9
		if (array_key_exists('background', $parsedConfigItem)) {
71 8
			$background = $parsedConfigItem['background'];
72 8
			$safe = $safe && ctype_xdigit(substr($background, 1));
73 4
		}
74
75 9
		return $safe;
76
	}
77
78
	/**
79
	 * Validates the parsed sorting values against allowed values
80
	 *
81
	 * @param string $section the section in the sorting config to be analysed
82
	 * @param string $value the value found in that section
83
	 *
84
	 * @return bool
85
	 */
86 12
	private function sortingValidator($section, $value) {
87 12
		if ($section === 'type') {
88 11
			$validValues = ['date', 'name'];
89 11
		} else {
90 10
			$validValues = ['des', 'asc'];
91
		}
92
93 12
		return in_array($value, $validValues);
94
	}
95
96
}
97