Completed
Pull Request — master (#362)
by Maxence
01:53
created

GlobalScaleService::asyncBroadcast()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.0328
c 0
b 0
f 0
cc 5
nc 9
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\Traits\TRequest;
40
use daita\MySmallPhpTools\Traits\TStringTools;
41
use OC;
42
use OCA\Circles\Db\GSEventsRequest;
43
use OCA\Circles\Exceptions\GlobalScaleEventException;
44
use OCA\Circles\Exceptions\GSKeyException;
45
use OCA\Circles\Exceptions\GSStatusException;
46
use OCA\Circles\GlobalScale\AGlobalScaleEvent;
47
use OCA\Circles\Model\GlobalScale\GSEvent;
48
use OCA\Circles\Model\GlobalScale\GSWrapper;
49
use OCP\AppFramework\QueryException;
50
use OCP\IURLGenerator;
51
52
53
/**
54
 * Class GlobalScaleService
55
 *
56
 * @package OCA\Circles\Service
57
 */
58
class GlobalScaleService {
59
60
61
	use TRequest;
62
	use TStringTools;
63
64
65
	/** @var IURLGenerator */
66
	private $urlGenerator;
67
68
	/** @var GSEventsRequest */
69
	private $gsEventsRequest;
70
71
	/** @var ConfigService */
72
	private $configService;
73
74
	/** @var MiscService */
75
	private $miscService;
76
77
78
	/**
79
	 * GlobalScaleService constructor.
80
	 *
81
	 * @param IURLGenerator $urlGenerator
82
	 * @param GSEventsRequest $gsEventsRequest
83
	 * @param ConfigService $configService
84
	 * @param MiscService $miscService
85
	 */
86
	public function __construct(
87
		IURLGenerator $urlGenerator,
88
		GSEventsRequest $gsEventsRequest,
89
		ConfigService $configService,
90
		MiscService $miscService
91
	) {
92
		$this->urlGenerator = $urlGenerator;
93
		$this->gsEventsRequest = $gsEventsRequest;
94
		$this->configService = $configService;
95
		$this->miscService = $miscService;
96
	}
97
98
99
	/**
100
	 * @param GSEvent $event
101
	 *
102
	 * @throws GSStatusException
103
	 */
104
	public function asyncBroadcast(GSEvent $event): void {
105
		if (!$this->configService->getGSStatus(ConfigService::GS_ENABLED)) {
106
			return;
107
		}
108
109
		$wrapper = new GSWrapper();
110
		$wrapper->setEvent($event);
111
		$wrapper->setToken($this->uuid());
112
		$wrapper->setCreation(time());
113
		$wrapper->setSeverity($event->getSeverity());
114
115
		foreach ($this->getInstances() as $instance) {
116
			$wrapper->setInstance($instance);
117
			$wrapper = $this->gsEventsRequest->create($wrapper);
118
		}
119
120
		$path = $this->urlGenerator->linkToRoute(
121
			'circles.GlobalScale.asyncBroadcast', ['token' => $wrapper->getToken()]
122
		);
123
124
		$request = new Request($path, Request::TYPE_PUT);
125
126
		$baseUrl = $this->urlGenerator->getBaseUrl();
127
		if (substr($baseUrl, 0, 16) === 'http://localhost') {
128
			$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...
129
			$request->setAddress($this->configService->getLocalCloudId());
130
			$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...
131
		} else {
132
			$request->setAddressFromUrl($baseUrl);
133
		}
134
135
		try {
136
			$this->doRequest($request);
137
		} 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...
138
		}
139
	}
140
141
142
	/**
143
	 * @param GSEvent $event
144
	 *
145
	 * @return AGlobalScaleEvent
146
	 * @throws GlobalScaleEventException
147
	 */
148
	public function getGlobalScaleEvent(GSEvent $event): AGlobalScaleEvent {
149
		$class = '\OCA\Circles\\' . $event->getType();
150
		try {
151
			$gs = OC::$server->query($class);
152
			if (!$gs instanceof AGlobalScaleEvent) {
153
				throw new GlobalScaleEventException($class . ' not an AGlobalScaleEvent');
154
			}
155
156
			return $gs;
157
		} 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...
158
			throw new GlobalScaleEventException('AGlobalScaleEvent ' . $class . ' not found');
159
		}
160
	}
161
162
163
	/**
164
	 * @return string
165
	 */
166
	public function getKey(): string {
167
		// TODO: sign event with real and temp key.
168
		return 'abcd';
169
	}
170
171
172
	/**
173
	 * @param string $key
174
	 *
175
	 * @throws GSKeyException
176
	 */
177
	public function checkKey(string $key) {
178
		if ($key !== $this->getKey()) {
179
			throw new GSKeyException('invalid key');
180
		}
181
	}
182
183
184
	/**
185
	 * @param GSEvent $event
186
	 *
187
	 * @throws GSKeyException
188
	 */
189
	public function checkEvent(GSEvent $event): void {
190
		$this->checkKey($event->getKey());
191
	}
192
193
194
	/**
195
	 * @param bool $all
196
	 *
197
	 * @return array
198
	 * @throws GSStatusException
199
	 */
200
	public function getInstances(bool $all = false): array {
201
		/** @var string $lookup */
202
		$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
203
204
		$request = new Request('/instances', Request::TYPE_GET);
205
		$request->setAddressFromUrl($lookup);
206
207
		try {
208
			$instances = $this->retrieveJson($request);
209
		} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) {
210
			$this->miscService->log('Issue while retrieving instances from lookup: ' . $e->getMessage());
211
212
			return [];
213
		}
214
215
		if ($all) {
216
			return $instances;
217
		}
218
219
		return array_diff($instances, $this->configService->getTrustedDomains());
220
	}
221
222
}
223
224