Completed
Pull Request — master (#362)
by Maxence
02:14
created

GlobalScaleService::getGlobalScaleEvent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
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\RequestContentException;
34
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
35
use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException;
36
use daita\MySmallPhpTools\Exceptions\RequestResultSizeException;
37
use daita\MySmallPhpTools\Exceptions\RequestServerException;
38
use daita\MySmallPhpTools\Model\Request;
39
use daita\MySmallPhpTools\Model\SimpleDataStore;
40
use daita\MySmallPhpTools\Traits\TRequest;
41
use daita\MySmallPhpTools\Traits\TStringTools;
42
use OC;
43
use OCA\Circles\Db\GSEventsRequest;
44
use OCA\Circles\Exceptions\GlobalScaleEventException;
45
use OCA\Circles\Exceptions\GSKeyException;
46
use OCA\Circles\Exceptions\GSStatusException;
47
use OCA\Circles\GlobalScale\AGlobalScaleEvent;
48
use OCA\Circles\Model\Circle;
49
use OCA\Circles\Model\GlobalScale\GSEvent;
50
use OCA\Circles\Model\GlobalScale\GSWrapper;
51
use OCP\AppFramework\QueryException;
52
use OCP\IURLGenerator;
53
54
55
/**
56
 * Class GlobalScaleService
57
 *
58
 * @package OCA\Circles\Service
59
 */
60
class GlobalScaleService {
61
62
63
	use TRequest;
64
	use TStringTools;
65
66
67
	/** @var IURLGenerator */
68
	private $urlGenerator;
69
70
	/** @var GSEventsRequest */
71
	private $gsEventsRequest;
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 GSEventsRequest $gsEventsRequest
85
	 * @param ConfigService $configService
86
	 * @param MiscService $miscService
87
	 */
88
	public function __construct(
89
		IURLGenerator $urlGenerator,
90
		GSEventsRequest $gsEventsRequest,
91
		ConfigService $configService,
92
		MiscService $miscService
93
	) {
94
		$this->urlGenerator = $urlGenerator;
95
		$this->gsEventsRequest = $gsEventsRequest;
96
		$this->configService = $configService;
97
		$this->miscService = $miscService;
98
	}
99
100
101
	/**
102
	 * @param GSEvent $event
103
	 *
104
	 * @throws GSStatusException
105
	 */
106
	public function asyncBroadcast(GSEvent $event): void {
107
		if (!$this->configService->getGSStatus(ConfigService::GS_ENABLED)) {
108
			return;
109
		}
110
111
		$wrapper = new GSWrapper();
112
		$wrapper->setEvent($event);
113
		$wrapper->setToken($this->uuid());
114
		$wrapper->setCreation(time());
115
		$wrapper->setSeverity($event->getSeverity());
116
117
		foreach ($this->getInstances() as $instance) {
118
			$wrapper->setInstance($instance);
119
			$wrapper = $this->gsEventsRequest->create($wrapper);
120
		}
121
122
		$path = $this->urlGenerator->linkToRoute(
123
			'circles.GlobalScale.asyncBroadcast', ['token' => $wrapper->getToken()]
124
		);
125
126
		$request = new Request($path, Request::TYPE_PUT);
127
128
		$baseUrl = $this->urlGenerator->getBaseUrl();
129
		if (substr($baseUrl, 0, 16) === 'http://localhost') {
130
			$request->setBaseUrl(substr($baseUrl, 16));
0 ignored issues
show
Bug introduced by
The method setBaseUrl() does not seem to exist on object<daita\MySmallPhpTools\Model\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
			$request->setAddress($this->configService->getLocalCloudId());
132
			$request->setProtocols(['https', 'http']);
0 ignored issues
show
Bug introduced by
The method setProtocols() does not exist on daita\MySmallPhpTools\Model\Request. Did you maybe mean setProtocol()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
133
		} else {
134
			$request->setAddressFromUrl($baseUrl);
135
		}
136
137
		try {
138
			$this->doRequest($request);
139
		} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
140
		}
141
	}
142
143
144
	/**
145
	 * @param GSEvent $event
146
	 *
147
	 * @return AGlobalScaleEvent
148
	 * @throws GlobalScaleEventException
149
	 */
150
	public function getGlobalScaleEvent(GSEvent $event): AGlobalScaleEvent {
151
		$class = '\OCA\Circles\\' . $event->getType();
152
		try {
153
			$gs = OC::$server->query($class);
154
			if (!$gs instanceof AGlobalScaleEvent) {
155
				throw new GlobalScaleEventException($class . ' not an AGlobalScaleEvent');
156
			}
157
158
			return $gs;
159
		} catch (QueryException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\QueryException 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...
160
			throw new GlobalScaleEventException('AGlobalScaleEvent ' . $class . ' not found');
161
		}
162
	}
163
164
165
	/**
166
	 * @return string
167
	 */
168
	public function getKey(): string {
169
		// TODO: sign event with real and temp key.
170
		return 'abcd';
171
	}
172
173
174
	/**
175
	 * @param string $key
176
	 *
177
	 * @throws GSKeyException
178
	 */
179
	public function checkKey(string $key) {
180
		if ($key !== $this->getKey()) {
181
			throw new GSKeyException('invalid key');
182
		}
183
	}
184
185
186
	/**
187
	 * @param GSEvent $event
188
	 *
189
	 * @throws GSKeyException
190
	 */
191
	public function checkEvent(GSEvent $event): void {
192
		$this->checkKey($event->getKey());
193
	}
194
195
196
	/**
197
	 * @param bool $all
198
	 *
199
	 * @return array
200
	 * @throws GSStatusException
201
	 */
202
	public function getInstances(bool $all = false): array {
203
		/** @var string $lookup */
204
		$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
205
206
		$request = new Request('/instances', Request::TYPE_GET);
207
		$request->setAddressFromUrl($lookup);
208
209
		try {
210
			$instances = $this->retrieveJson($request);
211
		} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) {
212
			$this->miscService->log('Issue while retrieving instances from lookup: ' . $e->getMessage());
213
214
			return [];
215
		}
216
217
		if ($all) {
218
			return $instances;
219
		}
220
221
		return array_diff($instances, $this->configService->getTrustedDomains());
222
	}
223
224
225
226
}
227