Completed
Push — master ( 8ec019...562e63 )
by Lukas
09:36
created

OCSController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 5
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Bernhard Posselt <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Stefan Weil <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2016, ownCloud, Inc.
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
/**
26
 * Public interface of ownCloud for apps to use.
27
 * AppFramework\Controller class
28
 */
29
30
namespace OCP\AppFramework;
31
32
use OCP\AppFramework\Http\DataResponse;
33
use OCP\AppFramework\Http\OCSResponse;
34
use OCP\AppFramework\Http\Response;
35
use OCP\IRequest;
36
37
38
/**
39
 * Base class to inherit your controllers from that are used for RESTful APIs
40
 * @since 8.1.0
41
 */
42
abstract class OCSController extends ApiController {
43
44
	/**
45
	 * constructor of the controller
46
	 * @param string $appName the name of the app
47
	 * @param IRequest $request an instance of the request
48
	 * @param string $corsMethods comma separated string of HTTP verbs which
49
	 * should be allowed for websites or webapps when calling your API, defaults to
50
	 * 'PUT, POST, GET, DELETE, PATCH'
51
	 * @param string $corsAllowedHeaders comma separated string of HTTP headers
52
	 * which should be allowed for websites or webapps when calling your API,
53
	 * defaults to 'Authorization, Content-Type, Accept'
54
	 * @param int $corsMaxAge number in seconds how long a preflighted OPTIONS
55
	 * request should be cached, defaults to 1728000 seconds
56
	 * @since 8.1.0
57
	 */
58
	public function __construct($appName,
59
								IRequest $request,
60
								$corsMethods='PUT, POST, GET, DELETE, PATCH',
61
								$corsAllowedHeaders='Authorization, Content-Type, Accept',
62
								$corsMaxAge=1728000){
63
		parent::__construct($appName, $request, $corsMethods,
64
							$corsAllowedHeaders, $corsMaxAge);
65
		$this->registerResponder('json', function ($data) {
66
			return $this->buildOCSResponse('json', $data);
67
		});
68
		$this->registerResponder('xml', function ($data) {
69
			return $this->buildOCSResponse('xml', $data);
70
		});
71
	}
72
73
	/**
74
	 * Since the OCS endpoints default to XML we need to find out the format
75
	 * again
76
	 * @param mixed $response the value that was returned from a controller and
77
	 * is not a Response instance
78
	 * @param string $format the format for which a formatter has been registered
79
	 * @throws \DomainException if format does not match a registered formatter
80
	 * @return Response
81
	 * @since 9.1.0
82
	 */
83
	public function buildResponse($response, $format = 'xml') {
84
		return parent::buildResponse($response, $format);
85
	}
86
87
	/**
88
	 * Unwrap data and build ocs response
89
	 * @param string $format json or xml
90
	 * @param array|DataResponse $data the data which should be transformed
91
	 * @since 8.1.0
92
	 */
93
	private function buildOCSResponse($format, $data) {
94
		if ($data instanceof DataResponse) {
95
			$data = $data->getData();
96
		}
97
98
		$params = [
99
			'statuscode' => 100,
100
			'message' => 'OK',
101
			'data' => [],
102
			'itemscount' => '',
103
			'itemsperpage' => ''
104
		];
105
106
		foreach ($data as $key => $value) {
107
			$params[$key] = $value;
108
		}
109
110
		return new OCSResponse(
111
			$format, $params['statuscode'],
112
			$params['message'], $params['data'],
113
			$params['itemscount'], $params['itemsperpage']
114
		);
115
	}
116
117
}
118