Completed
Pull Request — master (#551)
by Maxence
02:08
created

GlobalScaleService::getInstances()   A

Complexity

Conditions 5
Paths 17

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.0968
c 0
b 0
f 0
cc 5
nc 17
nop 1
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2019
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Service;
31
32
33
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
34
use daita\MySmallPhpTools\Model\Nextcloud\nc21\NC21Request;
35
use daita\MySmallPhpTools\Model\Request;
36
use daita\MySmallPhpTools\Traits\Nextcloud\nc21\TNC21Request;
37
use daita\MySmallPhpTools\Traits\TStringTools;
38
use OC\Security\IdentityProof\Signer;
39
use OCA\Circles\Db\RemoteWrapperRequest;
40
use OCA\Circles\Exceptions\GSStatusException;
41
use OCP\IURLGenerator;
42
use OCP\IUserManager;
43
use OCP\IUserSession;
44
45
46
/**
47
 * Class GlobalScaleService
48
 *
49
 * @package OCA\Circles\Service
50
 */
51
class GlobalScaleService {
52
53
54
	use TNC21Request;
55
	use TStringTools;
56
57
58
	/** @var IURLGenerator */
59
	private $urlGenerator;
60
61
	/** @var IUserManager */
62
	private $userManager;
63
64
	/** @var IUserSession */
65
	private $userSession;
66
67
	/** @var Signer */
68
	private $signer;
69
70
	/** @var RemoteWrapperRequest */
71
	private $remoteWrapperRequest;
72
73
	/** @var ConfigService */
74
	private $configService;
75
76
	/** @var MiscService */
77
	private $miscService;
78
79
80
	/**
81
	 * GlobalScaleService constructor.
82
	 *
83
	 * @param IURLGenerator $urlGenerator
84
	 * @param IUserManager $userManager
85
	 * @param IUserSession $userSession
86
	 * @param Signer $signer
87
	 * @param RemoteWrapperRequest $remoteWrapperRequest
88
	 * @param ConfigService $configService
89
	 * @param MiscService $miscService
90
	 */
91 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
		IURLGenerator $urlGenerator,
93
		IUserManager $userManager,
94
		IUserSession $userSession,
95
		Signer $signer,
96
		RemoteWrapperRequest $remoteWrapperRequest,
97
		ConfigService $configService,
98
		MiscService $miscService
99
	) {
100
		$this->urlGenerator = $urlGenerator;
101
		$this->userManager = $userManager;
102
		$this->userSession = $userSession;
103
		$this->signer = $signer;
104
		$this->remoteWrapperRequest = $remoteWrapperRequest;
105
		$this->configService = $configService;
106
		$this->miscService = $miscService;
107
	}
108
109
110
	/**
111
	 * @return array
112
	 * @throws GSStatusException
113
	 */
114
	public function getGlobalScaleInstances(): array {
115
		try {
116
			$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
117
			$request = new NC21Request(ConfigService::GS_LOOKUP_INSTANCES, Request::TYPE_POST);
118
			$this->configService->configureRequest($request);
119
			$request->basedOnUrl($lookup);
120
			$request->addData('authKey', $this->configService->getGSStatus(ConfigService::GS_KEY));
121
122
			try {
123
				return $this->retrieveJson($request);
124
			} catch (RequestNetworkException $e) {
125
				$this->e($e, ['request' => $request]);
126
			}
127
		} catch (GSStatusException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
128
		}
129
130
		return $this->configService->getGSStatus(ConfigService::GS_MOCKUP);
131
	}
132
133
134
135
//	/**
136
//	 * @param GSEvent $event
137
//	 *
138
//	 * @return string
139
//	 */
140
//	public function asyncBroadcast(GSEvent $event): string {
141
//		$wrapper = new GSWrapper();
142
//		$wrapper->setEvent($event);
143
//		$wrapper->setToken($this->uuid());
144
//		$wrapper->setCreation(time());
145
//		$wrapper->setSeverity($event->getSeverity());
146
//
147
//		foreach ($this->getInstances($event->isAsync()) as $instance) {
148
//			$wrapper->setInstance($instance);
149
//			$wrapper = $this->remoteWrapperRequest->create($wrapper);
150
//		}
151
//
152
//		$request = new NC21Request('', Request::TYPE_POST);
153
//		$this->configService->configureRequest(
154
//			$request, 'circles.RemoteWrapper.asyncBroadcast', ['token' => $wrapper->getToken()]
155
//		);
156
//
157
//		try {
158
//			$this->doRequest($request);
159
//		} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException $e) {
160
//			$this->miscService->e($e);
161
//		}
162
//
163
//		return $wrapper->getToken();
164
//	}
165
//
166
//
167
//	/**
168
//	 * @param GSEvent $event
169
//	 *
170
//	 * @return AGlobalScaleEvent
171
//	 * @throws GlobalScaleEventException
172
//	 */
173
//	public function getGlobalScaleEvent(GSEvent $event): AGlobalScaleEvent {
174
//		$class = $this->getClassNameFromEvent($event);
175
//		try {
176
//			$gs = OC::$server->query($class);
177
//			if (!$gs instanceof AGlobalScaleEvent) {
178
//				throw new GlobalScaleEventException($class . ' not an AGlobalScaleEvent');
179
//			}
180
//
181
//			return $gs;
182
//		} catch (QueryException $e) {
183
//			throw new GlobalScaleEventException('AGlobalScaleEvent ' . $class . ' not found');
184
//		}
185
//	}
186
//
187
//
188
//	/**
189
//	 * @return string
190
//	 */
191
//	public function getKey(): string {
192
//		try {
193
//			$key = $this->configService->getGSStatus(ConfigService::GS_KEY);
194
//		} catch (GSStatusException $e) {
195
//			$key = $this->configService->getAppValue(ConfigService::CIRCLES_LOCAL_GSKEY);
196
//			if ($key === '') {
197
//				$key = $this->token(31);
198
//				$this->configService->setAppValue(ConfigService::CIRCLES_LOCAL_GSKEY, $key);
199
//			}
200
//		}
201
//
202
//		return md5('gskey:' . $key);
203
//	}
204
//
205
//	/**
206
//	 * @param string $key
207
//	 *
208
//	 * @throws GSKeyException
209
//	 */
210
//	public function checkKey(string $key) {
211
//		if ($key !== $this->getKey()) {
212
//			throw new GSKeyException('invalid key');
213
//		}
214
//	}
215
//
216
//
217
//	/**
218
//	 * @param GSEvent $event
219
//	 *
220
//	 * @throws GSKeyException
221
//	 */
222
//	public function checkEvent(GSEvent $event): void {
223
//		$this->checkKey($event->getKey());
224
//	}
225
//
226
//
227
//	/**
228
//	 * @param GSEvent $event
229
//	 *
230
//	 * @return string
231
//	 * @throws GlobalScaleEventException
232
//	 */
233
//	private function getClassNameFromEvent(GSEvent $event): string {
234
//		$className = $event->getType();
235
//		if (substr($className, 0, 25) !== '\OCA\Circles\GlobalScale\\' || strpos($className, '.')) {
236
//			throw new GlobalScaleEventException(
237
//				$className . ' does not seems to be a secured AGlobalScaleEvent'
238
//			);
239
//		}
240
//
241
//		return $className;
242
//	}
243
//
244
//
245
//	/**
246
//	 * @return IUser
247
//	 * @throws NoUserException
248
//	 */
249
//	private function getRandomUser(): IUser {
250
//		$user = $this->userSession->getUser();
251
//		if ($user !== null) {
252
//			return $user;
253
//		}
254
//
255
//		$random = $this->userManager->search('', 1);
256
//		if (sizeof($random) > 0) {
257
//			return array_shift($random);
258
//		}
259
//
260
//		throw new NoUserException();
261
//	}
262
263
}
264
265