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

GSUpstreamService::getEventByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 2017
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\RequestResultSizeException;
36
use daita\MySmallPhpTools\Exceptions\RequestServerException;
37
use daita\MySmallPhpTools\Model\Request;
38
use daita\MySmallPhpTools\Traits\TRequest;
39
use OCA\Circles\Db\CirclesRequest;
40
use OCA\Circles\Db\GSEventsRequest;
41
use OCA\Circles\Exceptions\CircleDoesNotExistException;
42
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
43
use OCA\Circles\Exceptions\GSKeyException;
44
use OCA\Circles\Exceptions\JsonException;
45
use OCA\Circles\Exceptions\ModelException;
46
use OCA\Circles\Exceptions\TokenDoesNotExistException;
47
use OCA\Circles\Model\GlobalScale\GSEvent;
48
use OCA\Circles\Model\GlobalScale\GSWrapper;
49
use OCP\IURLGenerator;
50
51
52
/**
53
 * Class GSUpstreamService
54
 *
55
 * @package OCA\Circles\Service
56
 */
57
class GSUpstreamService {
58
59
60
	use TRequest;
61
62
63
	/** @var string */
64
	private $userId = '';
65
66
	/** @var IURLGenerator */
67
	private $urlGenerator;
68
69
	/** @var GSEventsRequest */
70
	private $gsEventsRequest;
71
72
	/** @var CirclesRequest */
73
	private $circlesRequest;
74
75
	/** @var ConfigService */
76
	private $configService;
77
78
	/** @var MiscService */
79
	private $miscService;
80
81
82
	/**
83
	 * GSUpstreamService constructor.
84
	 *
85
	 * @param $userId
86
	 * @param IURLGenerator $urlGenerator
87
	 * @param GSEventsRequest $gsEventsRequest
88
	 * @param CirclesRequest $circlesRequest
89
	 * @param ConfigService $configService
90
	 * @param MiscService $miscService
91
	 */
92
	public function __construct(
93
		$userId,
94
		IURLGenerator $urlGenerator,
95
		GSEventsRequest $gsEventsRequest,
96
		CirclesRequest $circlesRequest,
97
		ConfigService $configService,
98
		MiscService $miscService
99
	) {
100
		$this->userId = $userId;
101
		$this->urlGenerator = $urlGenerator;
102
		$this->gsEventsRequest = $gsEventsRequest;
103
		$this->circlesRequest = $circlesRequest;
104
		$this->configService = $configService;
105
		$this->miscService = $miscService;
106
	}
107
108
109
	/**
110
	 * @param GSEvent $event
111
	 *
112
	 * @throws CircleDoesNotExistException
113
	 * @throws ConfigNoCircleAvailableException
114
	 */
115
	public function newEvent(GSEvent $event) {
116
//		$this->fillEvent($event);
117
		if ($this->isLocalEvent($event)) {
118
			// TODO: manage the event locally - needed ?
119
			// TODO: confirm that action is ok for the front-end
120
			$this->asyncBroadcast($event);
121
		} else {
122
			// TODO: send the event to the right instance. based on the result, do something on the front-end
123
//			$path = $this->urlGenerator->linkToRouteAbsolute('circles.GlobalScale.event');
124
//			$request = new Request($path, Request::TYPE_POST);
125
		}
126
127
	}
128
129
130
	/**
131
	 * @param GSEvent $event
132
	 */
133
	public function asyncBroadcast(GSEvent $event): void {
134
		$wrapper = $this->gsEventsRequest->create($event, $this->getInstances());
135
		$path = $this->urlGenerator->linkToRoute(
136
			'circles.GlobalScale.asyncBroadcast', ['token' => $wrapper->getToken()]
137
		);
138
139
//		$url = $this->urlGenerator->getBaseUrl();
140
//		$path = parse_url($url, PHP_URL_PATH) . $path;
141
142
		$request = new Request($path, Request::TYPE_PUT);
143
		$request->setAddressFromUrl($this->urlGenerator->getBaseUrl());
144
		$request->setDataSerialize($event);
145
146
		try {
147
			$this->doRequest($request);
148
		} 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...
149
		}
150
	}
151
152
153
	/**
154
	 * @param GSEvent $event
155
	 */
156
	public function broadcastEvent(GSEvent $event): void {
157
		$this->signEvent($event);
158
159
		$path = $this->urlGenerator->linkToRoute('circles.GlobalScale.broadcast');
160
		$request = new Request($path, Request::TYPE_POST);
161
		$request->setDataSerialize($event);
162
163
		foreach ($this->getInstances() as $instance) {
164
			$request->setAddressFromUrl($instance);
165
166
			try {
167
				$this->doRequest($request);
168
			} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException $e) {
169
				// TODO: queue request
170
			}
171
		}
172
173
	}
174
175
176
	/**
177
	 * @param GSEvent $event
178
	 *
179
	 * @throws CircleDoesNotExistException
180
	 * @throws ConfigNoCircleAvailableException
181
	 */
182
	private function fillEvent(GSEvent $event): void {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
183
//		if (!$event->hasCircle()) {
184
//			$event->setCircle($this->circlesRequest->getCircle($event->getCircleId(), $this->userId, true));
185
//		}
186
	}
187
188
189
190
	/**
191
	 * @param bool $all
192
	 *
193
	 * @return array
194
	 */
195
	public function getInstances(bool $all = false): array {
196
		$instances = [
197
//			'test.artificial-owl.com',
198
//			'nc18.artificial-owl.com'
199
'http://nc18.local',
200
'http://nc18-001.local',
201
'http://nc18-002.local',
202
'http://nc18-003.local'
203
		];
204
205
		if ($all) {
206
			return $instances;
207
		}
208
209
//		$local = $this->configService->getAvailableHosts();
210
//		$local = ['test.artificial-owl.com'];
211
		$local = ['http://nc18.local'];
212
213
		return array_diff($instances, $local);
214
	}
215
216
217
	/**
218
	 * @return string
219
	 */
220
	public function getKey(): string {
221
		return 'abcd';
222
	}
223
224
225
	/**
226
	 * @param string $key
227
	 *
228
	 * @throws GSKeyException
229
	 */
230
	public function checkKey(string $key) {
231
		if ($key !== $this->getKey()) {
232
			throw new GSKeyException('invalid key');
233
		}
234
	}
235
236
237
	/**
238
	 * @param GSEvent $event
239
	 */
240
	private function signEvent(GSevent $event) {
241
		$event->setKey($this->getKey());
242
	}
243
244
245
	/**
246
	 * @param GSEvent $event
247
	 *
248
	 * @return bool
249
	 */
250
	private function isLocalEvent(GSEvent $event): bool {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
251
		return true;
252
253
	}
254
255
256
	/**
257
	 * @param string $token
258
	 *
259
	 * @return GSWrapper
260
	 * @throws JsonException
261
	 * @throws ModelException
262
	 * @throws TokenDoesNotExistException
263
	 */
264
	public function getEventByToken(string $token): GSWrapper {
265
		return $this->gsEventsRequest->getByToken($token);
266
	}
267
268
}
269
270