Passed
Push — feature/329_Subsonic_API ( d9d298 )
by Pauli
10:35
created

XMLResponse::addChildElement()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 16
nc 9
nop 3
dl 0
loc 27
rs 7.6666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2019
11
 */
12
13
namespace OCA\Music\Http;
14
15
use OCP\AppFramework\Http\Response;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
/**
18
 * This class creates an XML response out of a passed in associative array,
19
 * similarly how the class JSONResponse works. The content is described with
20
 * a recursive array structure, where arrays may have string or integer keys.
21
 * One array should not mix string and integer keys, that will lead to undefined
22
 * outcome. Furthermore, array with integer keys is supported only as payload of
23
 * an array with string keys.
24
 * 
25
 * Note that this response type has been created to fulfill the needs of the
26
 * SubsonicController and may not be suitable for all other purposes.
27
 */
28
class XMLResponse extends Response {
29
30
	private $content;
31
32
	public function __construct(array $content) {
33
		$this->addHeader('Content-Type', 'application/xml');
34
35
		// The content must have exactly one root element, add one if necessary
36
		if (\count($content) != 1) {
37
			$content = ['root' => $content];
38
		}
39
		$this->content = $content;
40
	}
41
42
	public function render() {
43
		$rootName = \array_keys($this->content)[0];
44
45
		$xmlTree = new \SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><$rootName/>");
46
		foreach ($this->content[$rootName] as $key => $value) {
47
			self::addChildElement($xmlTree, $key, $value);
48
		}
49
50
		return $xmlTree->asXML();
51
	}
52
53
	private static function addChildElement($parentNode, $key, $value) {
54
		if (\is_bool($value)) {
55
			$value = $value ? 'true' : 'false';
56
			$parentNode->addAttribute($key, $value);
57
		}
58
		elseif (\is_string($value) || \is_numeric($value)) {
59
			$parentNode->addAttribute($key, $value);
60
		}
61
		elseif (\is_array($value)) {
62
			if (self::arrayIsIndexed($value)) {
63
				foreach ($value as $child) {
64
					self::addChildElement($parentNode, $key, $child);
65
				}
66
			}
67
			else {
68
				// associative array
69
				$element = $parentNode->addChild($key);
70
				foreach ($value as $childKey => $childValue) {
71
					self::addChildElement($element, $childKey, $childValue);
72
				}
73
			}
74
		}
75
		elseif ($value === null) {
76
			// skip
77
		}
78
		else {
79
			throw new \Exception("Unexpected value type for key $key");
80
		}
81
	}
82
83
	/**
84
	 * Array is considered to be "indexed" if its first element has numerical key.
85
	 * @param array $array
86
	 */
87
	private static function arrayIsIndexed(array $array) {
88
		reset($array);
89
		return empty($array) || \is_int(key($array));
90
	}
91
}