Completed
Push — master ( 2c4c56...fc98aa )
by Thomas
19:13
created

InfoParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Andreas Fischer <[email protected]>
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2016, ownCloud GmbH.
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\App;
27
28
class InfoParser {
29
30
	/**
31
	 * @param string $file the xml file to be loaded
32
	 * @return null|array where null is an indicator for an error
33
	 */
34
	public function parse($file) {
35
		if (!file_exists($file)) {
36
			return null;
37
		}
38
39
		libxml_use_internal_errors(true);
40
		$loadEntities = libxml_disable_entity_loader(false);
41
		$xml = simplexml_load_file($file);
42
43
		libxml_disable_entity_loader($loadEntities);
44
		if ($xml === false) {
45
			libxml_clear_errors();
46
			return null;
47
		}
48
		$array = $this->xmlToArray($xml);
49
50
		if (is_null($array)) {
51
			return null;
52
		}
53
54
		if (!array_key_exists('info', $array)) {
55
			$array['info'] = [];
56
		}
57
		if (!array_key_exists('remote', $array)) {
58
			$array['remote'] = [];
59
		}
60
		if (!array_key_exists('public', $array)) {
61
			$array['public'] = [];
62
		}
63
		if (!array_key_exists('types', $array)) {
64
			$array['types'] = [];
65
		}
66
		if (!array_key_exists('repair-steps', $array)) {
67
			$array['repair-steps'] = [];
68
		}
69
		if (!array_key_exists('install', $array['repair-steps'])) {
70
			$array['repair-steps']['install'] = [];
71
		}
72
		if (!array_key_exists('pre-migration', $array['repair-steps'])) {
73
			$array['repair-steps']['pre-migration'] = [];
74
		}
75
		if (!array_key_exists('post-migration', $array['repair-steps'])) {
76
			$array['repair-steps']['post-migration'] = [];
77
		}
78
		if (!array_key_exists('live-migration', $array['repair-steps'])) {
79
			$array['repair-steps']['live-migration'] = [];
80
		}
81
		if (!array_key_exists('uninstall', $array['repair-steps'])) {
82
			$array['repair-steps']['uninstall'] = [];
83
		}
84
		if (!array_key_exists('background-jobs', $array)) {
85
			$array['background-jobs'] = [];
86
		}
87
		if (!array_key_exists('two-factor-providers', $array)) {
88
			$array['two-factor-providers'] = [];
89
		}
90
91
		if (array_key_exists('types', $array)) {
92
			if (is_array($array['types'])) {
93
				foreach ($array['types'] as $type => $v) {
94
					unset($array['types'][$type]);
95
					if (is_string($type)) {
96
						$array['types'][] = $type;
97
					}
98
				}
99
			} else {
100
				$array['types'] = [];
101
			}
102
		}
103
		if (isset($array['repair-steps']['install']['step']) && is_array($array['repair-steps']['install']['step'])) {
104
			$array['repair-steps']['install'] = $array['repair-steps']['install']['step'];
105
		}
106 View Code Duplication
		if (isset($array['repair-steps']['pre-migration']['step']) && is_array($array['repair-steps']['pre-migration']['step'])) {
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...
107
			$array['repair-steps']['pre-migration'] = $array['repair-steps']['pre-migration']['step'];
108
		}
109 View Code Duplication
		if (isset($array['repair-steps']['post-migration']['step']) && is_array($array['repair-steps']['post-migration']['step'])) {
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...
110
			$array['repair-steps']['post-migration'] = $array['repair-steps']['post-migration']['step'];
111
		}
112
		if (isset($array['repair-steps']['live-migration']['step']) && is_array($array['repair-steps']['live-migration']['step'])) {
113
			$array['repair-steps']['live-migration'] = $array['repair-steps']['live-migration']['step'];
114
		}
115
		if (isset($array['repair-steps']['uninstall']['step']) && is_array($array['repair-steps']['uninstall']['step'])) {
116
			$array['repair-steps']['uninstall'] = $array['repair-steps']['uninstall']['step'];
117
		}
118
		if (isset($array['background-jobs']['job']) && is_array($array['background-jobs']['job'])) {
119
			$array['background-jobs'] = $array['background-jobs']['job'];
120
		}
121
		return $array;
122
	}
123
124
	/**
125
	 * @param \SimpleXMLElement $xml
126
	 * @return array
127
	 */
128
	function xmlToArray($xml) {
129
		if (!$xml->children()) {
130
			return (string)$xml;
131
		}
132
133
		$array = [];
134
		foreach ($xml->children() as $element => $node) {
135
			$totalElement = count($xml->{$element});
136
137
			if (!isset($array[$element])) {
138
				$array[$element] = "";
139
			}
140
			/** @var \SimpleXMLElement $node */
141
			// Has attributes
142
			if ($attributes = $node->attributes()) {
143
				$data = [
144
					'@attributes' => [],
145
				];
146
				if (!count($node->children())){
147
					$value = (string)$node;
148
					if (!empty($value)) {
149
						$data['@value'] = (string)$node;
150
					}
151
				} else {
152
					$data = array_merge($data, $this->xmlToArray($node));
153
				}
154
				foreach ($attributes as $attr => $value) {
155
					$data['@attributes'][$attr] = (string)$value;
156
				}
157
158 View Code Duplication
				if ($totalElement > 1) {
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...
159
					$array[$element][] = $data;
160
				} else {
161
					$array[$element] = $data;
162
				}
163
				// Just a value
164 View Code Duplication
			} else {
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...
165
				if ($totalElement > 1) {
166
					$array[$element][] = $this->xmlToArray($node);
167
				} else {
168
					$array[$element] = $this->xmlToArray($node);
169
				}
170
			}
171
		}
172
173
		return $array;
174
	}
175
}
176