Passed
Push — master ( c2cc3a...e764ee )
by Blizzz
17:05 queued 12s
created

Controller::__construct()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 34
rs 8.9617
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Donquixote <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 * @author Thomas Tanghus <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
namespace OCP\AppFramework;
30
31
use OCP\AppFramework\Http\DataResponse;
32
use OCP\AppFramework\Http\JSONResponse;
33
use OCP\AppFramework\Http\Response;
34
use OCP\IRequest;
35
36
/**
37
 * Base class to inherit your controllers from
38
 * @since 6.0.0
39
 */
40
abstract class Controller {
41
	/**
42
	 * app name
43
	 * @var string
44
	 * @since 7.0.0
45
	 */
46
	protected $appName;
47
48
	/**
49
	 * current request
50
	 * @var \OCP\IRequest
51
	 * @since 6.0.0
52
	 */
53
	protected $request;
54
55
	/**
56
	 * @var array
57
	 * @since 7.0.0
58
	 */
59
	private $responders;
60
61
	/**
62
	 * constructor of the controller
63
	 * @param string $appName the name of the app
64
	 * @param IRequest $request an instance of the request
65
	 * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0
66
	 */
67
	public function __construct($appName,
68
								IRequest $request) {
69
		$this->appName = $appName;
70
		$this->request = $request;
71
72
		// default responders
73
		$this->responders = [
74
			'json' => function ($data) {
75
				if ($data instanceof DataResponse) {
76
					$response = new JSONResponse(
77
						$data->getData(),
78
						$data->getStatus()
79
					);
80
					$dataHeaders = $data->getHeaders();
81
					$headers = $response->getHeaders();
82
					// do not overwrite Content-Type if it already exists
83
					if (isset($dataHeaders['Content-Type'])) {
84
						unset($headers['Content-Type']);
85
					}
86
					$response->setHeaders(array_merge($dataHeaders, $headers));
87
88
					if ($data->getETag() !== null) {
0 ignored issues
show
introduced by
The condition $data->getETag() !== null is always true.
Loading history...
89
						$response->setETag($data->getETag());
90
					}
91
					if ($data->getLastModified() !== null) {
92
						$response->setLastModified($data->getLastModified());
93
					}
94
					if ($data->isThrottled()) {
95
						$response->throttle($data->getThrottleMetadata());
96
					}
97
98
					return $response;
99
				}
100
				return new JSONResponse($data);
101
			}
102
		];
103
	}
104
105
106
	/**
107
	 * Parses an HTTP accept header and returns the supported responder type
108
	 * @param string $acceptHeader
109
	 * @param string $default
110
	 * @return string the responder type
111
	 * @since 7.0.0
112
	 * @since 9.1.0 Added default parameter
113
	 */
114
	public function getResponderByHTTPHeader($acceptHeader, $default = 'json') {
115
		$headers = explode(',', $acceptHeader);
116
117
		// return the first matching responder
118
		foreach ($headers as $header) {
119
			$header = strtolower(trim($header));
120
121
			$responder = str_replace('application/', '', $header);
122
123
			if (array_key_exists($responder, $this->responders)) {
124
				return $responder;
125
			}
126
		}
127
128
		// no matching header return default
129
		return $default;
130
	}
131
132
133
	/**
134
	 * Registers a formatter for a type
135
	 * @param string $format
136
	 * @param \Closure $responder
137
	 * @since 7.0.0
138
	 */
139
	protected function registerResponder($format, \Closure $responder) {
140
		$this->responders[$format] = $responder;
141
	}
142
143
144
	/**
145
	 * Serializes and formats a response
146
	 * @param mixed $response the value that was returned from a controller and
147
	 * is not a Response instance
148
	 * @param string $format the format for which a formatter has been registered
149
	 * @throws \DomainException if format does not match a registered formatter
150
	 * @return Response
151
	 * @since 7.0.0
152
	 */
153
	public function buildResponse($response, $format = 'json') {
154
		if (array_key_exists($format, $this->responders)) {
155
			$responder = $this->responders[$format];
156
157
			return $responder($response);
158
		}
159
		throw new \DomainException('No responder registered for format '.
160
			$format . '!');
161
	}
162
}
163