Completed
Push — master ( 4fb284...9c981c )
by Joas
03:00
created

EndPoint::getDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Activity\Controller;
23
24
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\JSONResponse;
28
use OCP\IRequest;
29
30
class EndPoint extends Controller {
31
32
	/** @var OCSEndPoint */
33
	protected $ocsEndPoint;
34
35
	/**
36
	 * OCSEndPoint constructor.
37
	 *
38
	 * @param string $appName
39
	 * @param IRequest $request
40
	 * @param OCSEndPoint $ocsEndPoint
41
	 */
42
	public function __construct($appName,
43
								IRequest $request,
44
								OCSEndPoint $ocsEndPoint) {
45
		parent::__construct($appName, $request);
46
		$this->ocsEndPoint = $ocsEndPoint;
47
	}
48
49
	/**
50
	 * @NoAdminRequired
51
	 * @NoCSRFRequired
52
	 *
53
	 * @return JSONResponse
54
	 */
55
	public function getDefault() {
56
		$response = $this->ocsEndPoint->getDefault([]);
57
		return $this->ocsToJsonResponse($response);
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 * @NoCSRFRequired
63
	 *
64
	 * @param string $filter
65
	 * @return JSONResponse
66
	 */
67
	public function getFilter($filter) {
68
		$response = $this->ocsEndPoint->getFilter(['filter' => $filter]);
69
		return $this->ocsToJsonResponse($response);
70
	}
71
72
	/**
73
	 * @param \OC_OCS_Result $ocsResult
74
	 * @return JSONResponse
75
	 */
76
	protected function ocsToJsonResponse(\OC_OCS_Result $ocsResult) {
77
		$response = new JSONResponse(
78
			[
79
				'ocs' => [
80
					'meta' => [
81
						'status' => 'ok',
82
						'statuscode' => 100,
83
						'message' => null,
84
					],
85
					'data' => $ocsResult->getData(),
86
				],
87
			],
88
			$ocsResult->getStatusCode() === 100 ? Http::STATUS_OK : $ocsResult->getStatusCode()
89
		);
90
		$response->setHeaders(array_merge(
91
			$ocsResult->getHeaders(),
92
			[
93
				'Content-Type' => 'application/json; charset=utf-8'
94
			]
95
		));
96
		return $response;
97
	}
98
}
99