Completed
Pull Request — master (#362)
by Maxence
01:56
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\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
		$request->setAddressFromUrl($this->urlGenerator->getBaseUrl());
126
		$request->setDataSerialize($event);
127
128
		try {
129
			$this->doRequest($request);
130
		} 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...
131
		}
132
	}
133
134
135
	/**
136
	 * @param GSEvent $event
137
	 *
138
	 * @return AGlobalScaleEvent
139
	 * @throws GlobalScaleEventException
140
	 */
141
	public function getGlobalScaleEvent(GSEvent $event): AGlobalScaleEvent {
142
		$class = '\OCA\Circles\\' . $event->getType();
143
		try {
144
			$gs = OC::$server->query($class);
145
			if (!$gs instanceof AGlobalScaleEvent) {
146
				throw new GlobalScaleEventException($class . ' not an AGlobalScaleEvent');
147
			}
148
149
			return $gs;
150
		} 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...
151
			throw new GlobalScaleEventException('AGlobalScaleEvent ' . $class . ' not found');
152
		}
153
	}
154
155
156
	/**
157
	 * @return string
158
	 */
159
	public function getKey(): string {
160
		// TODO: sign event with real and temp key.
161
		return 'abcd';
162
	}
163
164
165
	/**
166
	 * @param string $key
167
	 *
168
	 * @throws GSKeyException
169
	 */
170
	public function checkKey(string $key) {
171
		if ($key !== $this->getKey()) {
172
			throw new GSKeyException('invalid key');
173
		}
174
	}
175
176
177
	/**
178
	 * @param GSEvent $event
179
	 *
180
	 * @throws GSKeyException
181
	 */
182
	public function checkEvent(GSEvent $event): void {
183
		$this->checkKey($event->getKey());
184
	}
185
186
187
	/**
188
	 * @param bool $all
189
	 *
190
	 * @return array
191
	 * @throws GSStatusException
192
	 */
193
	public function getInstances(bool $all = false): array {
194
		/** @var string $lookup */
195
		$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
196
197
		$request = new Request('/instances', Request::TYPE_GET);
198
		$request->setAddressFromUrl($lookup);
199
200
		try {
201
			$instances = $this->retrieveJson($request);
202
		} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) {
203
			$this->miscService->log('Issue while retrieving instances from lookup: ' . $e->getMessage());
204
205
			return [];
206
		}
207
208
		if ($all) {
209
			return $instances;
210
		}
211
212
		return array_diff($instances, $this->configService->getTrustedDomains());
213
	}
214
215
}
216
217