Completed
Push — master ( 155fb4...47e6b0 )
by Thomas
09:19
created

OCSController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 105
rs 10
wmc 11
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A buildResponse() 0 21 4
B buildOCSResponse() 0 35 6
1
<?php
2
/**
3
 * @author Bernhard Posselt <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Roeland Jago Douma <[email protected]>
6
 * @author Sergio Bertolín <[email protected]>
7
 * @author Stefan Weil <[email protected]>
8
 * @author Thomas Müller <[email protected]>
9
 * @author Vincent Petry <[email protected]>
10
 *
11
 * @copyright Copyright (c) 2017, ownCloud GmbH
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
/**
29
 * Public interface of ownCloud for apps to use.
30
 * AppFramework\Controller class
31
 */
32
33
namespace OCP\AppFramework;
34
35
use OC\OCS\Result;
36
use OCP\AppFramework\Http\DataResponse;
37
use OCP\AppFramework\Http\OCSResponse;
38
use OCP\AppFramework\Http\Response;
39
use OCP\IRequest;
40
41
42
/**
43
 * Base class to inherit your controllers from that are used for RESTful APIs
44
 * @since 8.1.0
45
 */
46
abstract class OCSController extends ApiController {
47
48
	/**
49
	 * constructor of the controller
50
	 * @param string $appName the name of the app
51
	 * @param IRequest $request an instance of the request
52
	 * @param string $corsMethods comma separated string of HTTP verbs which
53
	 * should be allowed for websites or webapps when calling your API, defaults to
54
	 * 'PUT, POST, GET, DELETE, PATCH'
55
	 * @param string $corsAllowedHeaders comma separated string of HTTP headers
56
	 * which should be allowed for websites or webapps when calling your API,
57
	 * defaults to 'Authorization, Content-Type, Accept'
58
	 * @param int $corsMaxAge number in seconds how long a preflighted OPTIONS
59
	 * request should be cached, defaults to 1728000 seconds
60
	 * @since 8.1.0
61
	 */
62
	public function __construct($appName,
63
								IRequest $request,
64
								$corsMethods='PUT, POST, GET, DELETE, PATCH',
65
								$corsAllowedHeaders='Authorization, Content-Type, Accept',
66
								$corsMaxAge=1728000){
67
		parent::__construct($appName, $request, $corsMethods,
68
							$corsAllowedHeaders, $corsMaxAge);
69
		$this->registerResponder('json', function ($data) {
70
			return $this->buildOCSResponse('json', $data);
71
		});
72
		$this->registerResponder('xml', function ($data) {
73
			return $this->buildOCSResponse('xml', $data);
74
		});
75
	}
76
77
78
	/**
79
	 * Unwrap data and build ocs response
80
	 * @param string $format json or xml
81
	 * @param array|DataResponse $data the data which should be transformed
82
	 * @since 8.1.0
83
	 */
84
	private function buildOCSResponse($format, $data) {
85
		if ($data instanceof Result) {
86
			$headers = $data->getHeaders();
87
			$data = $data->getMeta();
88
			$data['headers'] = $headers;
89
		}
90
		if ($data instanceof DataResponse) {
91
			$data = $data->getData();
92
		}
93
94
		$params = [
95
			'statuscode' => 100,
96
			'message' => 'OK',
97
			'data' => [],
98
			'itemscount' => '',
99
			'itemsperpage' => ''
100
		];
101
102
		foreach ($data as $key => $value) {
103
			$params[$key] = $value;
104
		}
105
106
		$resp = new OCSResponse(
107
			$format, $params['statuscode'],
108
			$params['message'], $params['data'],
109
			$params['itemscount'], $params['itemsperpage']
110
		);
111
		if (isset($data['headers'])) {
112
			foreach ($data['headers'] as $key => $value) {
113
				$resp->addHeader($key, $value);
114
			}
115
		}
116
117
		return $resp;
118
	}
119
120
	/**
121
	 * Serializes and formats a response
122
	 * @param mixed $response the value that was returned from a controller and
123
	 * is not a Response instance
124
	 * @param string $format the format for which a formatter has been registered
125
	 * @throws \DomainException if format does not match a registered formatter
126
	 * @return Response
127
	 * @since 7.0.0
128
	 */
129
	function buildResponse($response, $format = 'json') {
130
		$format = $this->request->getParam('format');
131
		if (is_null($format)) {
132
			$format = 'xml';
133
		}
134
		/** @var OCSResponse $resp */
135
		$resp = parent::buildResponse($response, $format);
136
		$script = $this->request->getScriptName();
137
138
		if (substr($script, -11) === '/ocs/v2.php') {
139
			$statusCode = \OC_API::mapStatusCodes($resp->getStatusCode());
140
			if (!is_null($statusCode)) {
141
				// HTTP code
142
				$resp->setStatus($statusCode);
143
				// OCS code
144
				$resp->setStatusCode($statusCode);
145
			}
146
		}
147
148
		return $resp;
149
	}
150
}
151