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

ApiBase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
use OCP\Http\Client\IClientService;
25
use OCP\Remote\ICredentials;
26
use OCP\Remote\IInstance;
27
28
class ApiBase {
29
	/** @var IInstance */
30
	private $instance;
31
	/** @var ICredentials */
32
	private $credentials;
33
	/** @var IClientService */
34
	private $clientService;
35
36
	public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) {
37
		$this->instance = $instance;
38
		$this->credentials = $credentials;
39
		$this->clientService = $clientService;
40
	}
41
42
	protected function getHttpClient() {
43
		return $this->clientService->newClient();
44
	}
45
46
	protected function addDefaultHeaders(array $headers) {
47
		return array_merge([
48
			'OCS-APIREQUEST' => 'true',
49
			'Accept' => 'application/json'
50
		], $headers);
51
	}
52
53
	/**
54
	 * @param string $method
55
	 * @param string $url
56
	 * @param array $body
57
	 * @param array $query
58
	 * @param array $headers
59
	 * @return resource|string
60
	 * @throws \InvalidArgumentException
61
	 */
62
	protected function request($method, $url, array $body = [], array $query = [], array $headers = []) {
63
		$fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url;
64
		$options = [
65
			'query' => $query,
66
			'headers' => $this->addDefaultHeaders($headers),
67
			'auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()]
68
		];
69
		if ($body) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $body of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
70
			$options['body'] = $body;
71
		}
72
73
		$client = $this->getHttpClient();
74
75
		switch ($method) {
76
			case 'get':
77
				$response = $client->get($fullUrl, $options);
78
				break;
79
			case 'post':
80
				$response = $client->post($fullUrl, $options);
81
				break;
82
			case 'put':
83
				$response = $client->put($fullUrl, $options);
84
				break;
85
			case 'delete':
86
				$response = $client->delete($fullUrl, $options);
87
				break;
88
			case 'options':
89
				$response = $client->options($fullUrl, $options);
90
				break;
91
			default:
92
				throw new \InvalidArgumentException('Invalid method ' . $method);
93
		}
94
95
		return $response->getBody();
96
	}
97
}
98