Completed
Push — master ( c7d566...e2979b )
by Maxence
03:33 queued 01:09
created

InterfaceService::getCloudPath()   B

Complexity

Conditions 10
Paths 24

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 7.6666
c 0
b 0
f 0
cc 10
nc 24
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Service;
33
34
35
use daita\MySmallPhpTools\Traits\TArrayTools;
36
use daita\MySmallPhpTools\Traits\TStringTools;
37
use OCA\Circles\Db\RemoteRequest;
38
use OCA\Circles\Exceptions\RemoteNotFoundException;
39
use OCA\Circles\Exceptions\UnknownInterfaceException;
40
use OCP\IRequest;
41
use OCP\IURLGenerator;
42
43
44
/**
45
 * Class InterfaceService
46
 *
47
 * @package OCA\Circles\Service
48
 */
49
class InterfaceService {
50
51
	const IFACE0 = 1;
52
	const IFACE1 = 2;
53
	const IFACE2 = 3;
54
	const IFACE3 = 4;
55
	const IFACE4 = 5;
56
	const IFACE_INTERNAL = 6;
57
	const IFACE_FRONTAL = 7;
58
59
	public static $LIST_IFACE = [
60
		self::IFACE_INTERNAL => 'internal',
61
		self::IFACE_FRONTAL  => 'frontal',
62
		self::IFACE0         => 'iface0',
63
		self::IFACE1         => 'iface1',
64
		self::IFACE2         => 'iface2',
65
		self::IFACE3         => 'iface3',
66
		self::IFACE4         => 'iface4',
67
	];
68
69
70
	use TStringTools;
71
	use TArrayTools;
72
73
74
	/** @var IURLGenerator */
75
	private $urlGenerator;
76
77
	/** @var RemoteRequest */
78
	private $remoteRequest;
79
80
	/** @var ConfigService */
81
	private $configService;
82
83
84
	/** @var int */
85
	private $currentInterface = 0;
86
87
	/** @var int */
88
	private $outgoingInterface = 0;
89
90
91
	/**
92
	 * InterfaceService constructor.
93
	 *
94
	 * @param IURLGenerator $urlGenerator
95
	 * @param RemoteRequest $remoteRequest
96
	 * @param ConfigService $configService
97
	 */
98
	public function __construct(
99
		IURLGenerator $urlGenerator,
100
		RemoteRequest $remoteRequest,
101
		ConfigService $configService
102
	) {
103
		$this->urlGenerator = $urlGenerator;
104
		$this->remoteRequest = $remoteRequest;
105
		$this->configService = $configService;
106
	}
107
108
109
	/**
110
	 * @param int $interface
111
	 */
112
	public function setCurrentInterface(int $interface): void {
113
		$this->currentInterface = $interface;
114
	}
115
116
	/**
117
	 * @return int
118
	 * @throws UnknownInterfaceException
119
	 */
120
	public function getCurrentInterface(): int {
121
		if ($this->currentInterface === 0) {
122
			throw new UnknownInterfaceException('interface not initialized');
123
		}
124
125
		return $this->currentInterface;
126
	}
127
128
	/**
129
	 * @return bool
130
	 */
131
	public function hasCurrentInterface(): bool {
132
		return ($this->currentInterface !== 0);
133
	}
134
135
136
	/**
137
	 * @param IRequest $request
138
	 */
139
	public function setCurrentInterfaceFromRequest(IRequest $request): void {
140
		$testing = [
141
			self::IFACE_INTERNAL => $this->configService->getInternalInstance(),
142
			self::IFACE_FRONTAL  => $this->configService->getFrontalInstance(),
143
			self::IFACE0         => $this->configService->getIfaceInstance(self::IFACE0),
144
			self::IFACE1         => $this->configService->getIfaceInstance(self::IFACE1),
145
			self::IFACE2         => $this->configService->getIfaceInstance(self::IFACE2),
146
			self::IFACE3         => $this->configService->getIfaceInstance(self::IFACE3),
147
			self::IFACE4         => $this->configService->getIfaceInstance(self::IFACE4),
148
		];
149
150
		$serverHost = strtolower($request->getServerHost());
151
		if ($serverHost === '') {
152
			return;
153
		}
154
155
		foreach ($testing as $iface => $instance) {
156
			if ($serverHost === strtolower($instance)) {
157
				$this->setCurrentInterface($iface);
158
159
				return;
160
			}
161
		}
162
	}
163
164
165
	/**
166
	 *
167
	 */
168
	public function setCurrentInterfaceFromInstance(string $instance): void {
169
		try {
170
			$remoteInstance = $this->remoteRequest->getFromInstance($instance);
171
			$this->setCurrentInterface($remoteInstance->getInterface());
172
		} catch (RemoteNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
173
		}
174
	}
175
176
177
	/**
178
	 * use this only if interface must be defined. If not, use getLocalInstance()
179
	 *
180
	 * @throws UnknownInterfaceException
181
	 */
182
	public function getCloudInstance(): string {
183
		switch ($this->getCurrentInterface()) {
184
			case self::IFACE_INTERNAL:
185
				return $this->configService->getInternalInstance();
186
			case self::IFACE_FRONTAL:
187
				return $this->configService->getFrontalInstance();
188
			case self::IFACE0:
189
			case self::IFACE1:
190
			case self::IFACE2:
191
			case self::IFACE3:
192
			case self::IFACE4:
193
				return $this->configService->getIfaceInstance($this->getCurrentInterface());
194
		}
195
196
		throw new UnknownInterfaceException('unknown configured interface');
197
	}
198
199
200
	/**
201
	 * @throws UnknownInterfaceException
202
	 */
203
	public function getCloudPath(string $route = '', array $args = []): string {
204
		$scheme = '';
205
		switch ($this->getCurrentInterface()) {
206
			case self::IFACE_INTERNAL:
207
				$scheme = $this->configService->getAppValue(ConfigService::INTERNAL_CLOUD_SCHEME);
208
				break;
209
			case self::IFACE_FRONTAL:
210
				$scheme = $this->configService->getAppValue(ConfigService::FRONTAL_CLOUD_SCHEME);
211
				break;
212
			case self::IFACE0:
213
				$scheme = $this->configService->getAppValue(ConfigService::IFACE0_CLOUD_SCHEME);
214
				break;
215
			case self::IFACE1:
216
				$scheme = $this->configService->getAppValue(ConfigService::IFACE1_CLOUD_SCHEME);
217
				break;
218
			case self::IFACE2:
219
				$scheme = $this->configService->getAppValue(ConfigService::IFACE2_CLOUD_SCHEME);
220
				break;
221
			case self::IFACE3:
222
				$scheme = $this->configService->getAppValue(ConfigService::IFACE3_CLOUD_SCHEME);
223
				break;
224
			case self::IFACE4:
225
				$scheme = $this->configService->getAppValue(ConfigService::IFACE4_CLOUD_SCHEME);
226
				break;
227
		}
228
229
		if ($scheme === '') {
230
			throw new UnknownInterfaceException('misconfigured scheme');
231
		}
232
233
		$base = $scheme . '://' . $this->getCloudInstance();
234
235
		if ($route === '') {
236
			return $base;
237
		}
238
239
		return $base . $this->urlGenerator->linkToRoute($route, $args);
240
	}
241
242
243
	/**
244
	 * should be used when unsure about the used Interface
245
	 *
246
	 * @return string
247
	 */
248
	public function getLocalInstance(): string {
249
		if ($this->hasCurrentInterface()) {
250
			try {
251
				return $this->getCloudInstance();
252
			} catch (UnknownInterfaceException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
253
			}
254
		}
255
256
		if ($this->configService->getFrontalInstance() !== '') {
257
			return $this->configService->getFrontalInstance();
258
		}
259
260
		if ($this->configService->getInternalInstance() !== '') {
261
			return $this->configService->getInternalInstance();
262
		}
263
264
		return $this->configService->getLoopbackInstance();
265
	}
266
267
}
268
269