Completed
Push — master ( 5bc8c9...07e638 )
by Morris
52:34 queued 36:57
created

OCS   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
D request() 0 27 10
A checkResponseArray() 0 7 3
A getUser() 0 5 1
A getCapabilities() 0 4 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Remote\Api;
23
24
25
use GuzzleHttp\Exception\ClientException;
26
use OC\ForbiddenException;
27
use OC\Remote\User;
28
use OCP\API;
29
use OCP\Remote\Api\ICapabilitiesApi;
30
use OCP\Remote\Api\IUserApi;
31
32
class OCS extends ApiBase implements ICapabilitiesApi, IUserApi {
33
	/**
34
	 * @param string $method
35
	 * @param string $url
36
	 * @param array $body
37
	 * @param array $query
38
	 * @param array $headers
39
	 * @return array
40
	 * @throws ForbiddenException
41
	 * @throws NotFoundException
42
	 * @throws \Exception
43
	 */
44
	protected function request($method, $url, array $body = [], array $query = [], array $headers = []) {
45
		try {
46
			$response = json_decode(parent::request($method, 'ocs/v2.php/' . $url, $body, $query, $headers), true);
47
		} catch (ClientException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\ClientException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
48
			if ($e->getResponse()->getStatusCode() === 404) {
49
				throw new NotFoundException();
50
			} else if ($e->getResponse()->getStatusCode() === 403 || $e->getResponse()->getStatusCode() === 401) {
51
				throw new ForbiddenException();
52
			} else {
53
				throw $e;
54
			}
55
		}
56
		if (!isset($response['ocs']) || !isset($response['ocs']['meta'])) {
57
			throw new \Exception('Invalid ocs response');
58
		}
59
		if ($response['ocs']['meta']['statuscode'] === API::RESPOND_UNAUTHORISED) {
60
			throw new ForbiddenException();
61
		}
62
		if ($response['ocs']['meta']['statuscode'] === API::RESPOND_NOT_FOUND) {
63
			throw new NotFoundException();
64
		}
65
		if ($response['ocs']['meta']['status'] !== 'ok') {
66
			throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']);
67
		}
68
69
		return $response['ocs']['data'];
70
	}
71
72
	/**
73
	 * @param array $data
74
	 * @param string $type
75
	 * @param string[] $keys
76
	 * @throws \Exception
77
	 */
78
	private function checkResponseArray(array $data, $type, array $keys) {
79
		foreach ($keys as $key) {
80
			if (!array_key_exists($key, $data)) {
81
				throw new \Exception('Invalid ' . $type . ' response, expected field ' . $key . ' not found');
82
			}
83
		}
84
	}
85
86
	public function getUser($userId) {
87
		$result = $this->request('get', 'cloud/users/' . $userId);
88
		$this->checkResponseArray($result, 'user', User::EXPECTED_KEYS);
89
		return new User($result);
90
	}
91
92
	/**
93
	 * @return array The capabilities in the form of [$appId => [$capability => $value]]
94
	 */
95
	public function getCapabilities() {
96
		$result = $this->request('get', 'cloud/capabilities');
97
		return $result['capabilities'];
98
	}
99
}
100