Completed
Pull Request — master (#129)
by Jan-Christoph
52:33 queued 50:39
created

EndPoint   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 15.38%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 69
ccs 4
cts 26
cp 0.1538
rs 10
c 0
b 0
f 0

4 Methods

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