Completed
Push — master ( 57ea46...72dc01 )
by Blizzz
14:28
created

BaseResponse::toXML()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 6
nop 2
dl 20
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2016 Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\AppFramework\OCS;
24
25
use OCP\AppFramework\Http;
26
use OCP\AppFramework\Http\DataResponse;
27
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
28
use OCP\AppFramework\Http\Response;
29
30
abstract class BaseResponse extends Response   {
31
	/** @var array */
32
	protected $data;
33
34
	/** @var string */
35
	protected $format;
36
37
	/** @var string */
38
	protected $statusMessage;
39
40
	/** @var int */
41
	protected $itemsCount;
42
43
	/** @var int */
44
	protected $itemsPerPage;
45
46
	/**
47
	 * BaseResponse constructor.
48
	 *
49
	 * @param DataResponse|null $dataResponse
50
	 * @param string $format
51
	 * @param string|null $statusMessage
52
	 * @param int|null $itemsCount
53
	 * @param int|null $itemsPerPage
54
	 */
55
	public function __construct(DataResponse $dataResponse,
56
								$format = 'xml',
57
								$statusMessage = null,
58
								$itemsCount = null,
59
								$itemsPerPage = null) {
60
		$this->format = $format;
61
		$this->statusMessage = $statusMessage;
62
		$this->itemsCount = $itemsCount;
63
		$this->itemsPerPage = $itemsPerPage;
64
65
		$this->data = $dataResponse->getData();
0 ignored issues
show
Documentation Bug introduced by
It seems like $dataResponse->getData() can also be of type object. However, the property $data is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
66
67
		$this->setHeaders($dataResponse->getHeaders());
68
		$this->setStatus($dataResponse->getStatus());
69
		$this->setETag($dataResponse->getETag());
70
		$this->setLastModified($dataResponse->getLastModified());
71
		$this->setCookies($dataResponse->getCookies());
72
		$this->setContentSecurityPolicy(new EmptyContentSecurityPolicy());
73
74
		if ($format === 'json') {
75
			$this->addHeader(
76
				'Content-Type', 'application/json; charset=utf-8'
77
			);
78
		} else {
79
			$this->addHeader(
80
				'Content-Type', 'application/xml; charset=utf-8'
81
			);
82
		}
83
	}
84
85
	/**
86
	 * @param string[] $meta
87
	 * @return string
88
	 */
89
	protected function renderResult(array $meta): string {
90
		$status = $this->getStatus();
91
		if ($status === Http::STATUS_NO_CONTENT ||
92
			$status === Http::STATUS_NOT_MODIFIED ||
93
			($status >= 100 && $status <= 199)) {
94
			// Those status codes are not supposed to have a body:
95
			// https://stackoverflow.com/q/8628725
96
			return '';
97
		}
98
99
		$response = [
100
			'ocs' => [
101
				'meta' => $meta,
102
				'data' => $this->data,
103
			],
104
		];
105
106
		if ($this->format === 'json') {
107
			return json_encode($response, JSON_HEX_TAG);
108
		}
109
110
		$writer = new \XMLWriter();
111
		$writer->openMemory();
112
		$writer->setIndent(true);
113
		$writer->startDocument();
114
		$this->toXML($response, $writer);
115
		$writer->endDocument();
116
		return $writer->outputMemory(true);
117
118
	}
119
120
	/**
121
	 * @param array $array
122
	 * @param \XMLWriter $writer
123
	 */
124 View Code Duplication
	protected function toXML(array $array, \XMLWriter $writer) {
125
		foreach ($array as $k => $v) {
126
			if ($k[0] === '@') {
127
				$writer->writeAttribute(substr($k, 1), $v);
128
				continue;
129
			}
130
131
			if (\is_numeric($k)) {
132
				$k = 'element';
133
			}
134
135
			if (\is_array($v)) {
136
				$writer->startElement($k);
137
				$this->toXML($v, $writer);
138
				$writer->endElement();
139
			} else {
140
				$writer->writeElement($k, $v);
141
			}
142
		}
143
	}
144
145
	public function getOCSStatus() {
146
		return parent::getStatus();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getStatus() instead of getOCSStatus()). Are you sure this is correct? If so, you might want to change this to $this->getStatus().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
147
	}
148
}
149