Completed
Pull Request — master (#609)
by Maxence
02:33
created

ModelManager::importBasedOnPath()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.9617
c 0
b 0
f 0
cc 6
nc 32
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Model;
33
34
35
use OCA\Circles\Db\CoreQueryBuilder;
36
use OCA\Circles\Db\MemberRequest;
37
use OCA\Circles\Db\MembershipRequest;
38
use OCA\Circles\Exceptions\CircleNotFoundException;
39
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
40
use OCA\Circles\Exceptions\FileCacheNotFoundException;
41
use OCA\Circles\Exceptions\MemberNotFoundException;
42
use OCA\Circles\Exceptions\MembershipNotFoundException;
43
use OCA\Circles\Exceptions\OwnerNotFoundException;
44
use OCA\Circles\Exceptions\RemoteNotFoundException;
45
use OCA\Circles\Exceptions\RequestBuilderException;
46
use OCA\Circles\Exceptions\UnknownInterfaceException;
47
use OCA\Circles\IMemberships;
48
use OCA\Circles\Model\Federated\RemoteInstance;
49
use OCA\Circles\Service\ConfigService;
50
use OCA\Circles\Service\InterfaceService;
51
use OCP\IURLGenerator;
52
53
/**
54
 * Class ModelManager
55
 *
56
 * @package OCA\Circles\Model
57
 */
58
class ModelManager {
59
60
61
	/** @var IURLGenerator */
62
	private $urlGenerator;
63
64
	/** @var CoreQueryBuilder */
65
	private $coreRequestBuilder;
66
67
	/** @var MemberRequest */
68
	private $memberRequest;
69
70
	/** @var MembershipRequest */
71
	private $membershipRequest;
72
73
	/** @var InterfaceService */
74
	private $interfaceService;
75
76
	/** @var ConfigService */
77
	private $configService;
78
79
80
	/** @var bool */
81
	private $fullDetails = false;
82
83
84
	/**
85
	 * ModelManager constructor.
86
	 *
87
	 * @param IURLGenerator $urlGenerator
88
	 * @param CoreQueryBuilder $coreRequestBuilder
89
	 * @param MemberRequest $memberRequest
90
	 * @param MembershipRequest $membershipRequest
91
	 * @param InterfaceService $interfaceService
92
	 * @param ConfigService $configService
93
	 */
94
	public function __construct(
95
		IURLGenerator $urlGenerator,
96
		CoreQueryBuilder $coreRequestBuilder,
97
		MemberRequest $memberRequest,
98
		MembershipRequest $membershipRequest,
99
		InterfaceService $interfaceService,
100
		ConfigService $configService
101
	) {
102
		$this->urlGenerator = $urlGenerator;
103
		$this->coreRequestBuilder = $coreRequestBuilder;
104
		$this->memberRequest = $memberRequest;
105
		$this->membershipRequest = $membershipRequest;
106
		$this->interfaceService = $interfaceService;
107
		$this->configService = $configService;
108
	}
109
110
111
	/**
112
	 * @return ConfigService
113
	 */
114
	public function getConfigService(): ConfigService {
115
		return $this->configService;
116
	}
117
118
119
	/**
120
	 * @param Circle $circle
121
	 */
122
	public function getMembers(Circle $circle): void {
123
		try {
124
			$circle->setMembers($this->memberRequest->getMembers($circle->getSingleId()));
125
		} catch (RequestBuilderException $e) {
126
			// TODO: debug log
127
		}
128
	}
129
130
131
	/**
132
	 * @param Circle $circle
133
	 * @param bool $detailed
134
	 */
135
	public function getInheritedMembers(Circle $circle, bool $detailed = false): void {
136
		try {
137
			$circle->setInheritedMembers(
138
				$this->memberRequest->getInheritedMembers($circle->getSingleId(), $detailed),
139
				$detailed
140
			);
141
		} catch (RequestBuilderException $e) {
142
			// TODO: debug log
143
		}
144
	}
145
146
147
	/**
148
	 * @param IMemberships $member
149
	 */
150
	public function getMemberships(IMemberships $member): void {
151
		$memberships = $this->membershipRequest->getMemberships($member->getSingleId());
152
		$member->setMemberships($memberships);
153
	}
154
155
156
	/**
157
	 * @param ManagedModel $model
158
	 * @param array $data
159
	 * @param string $base
160
	 */
161
	public function manageImportFromDatabase(ManagedModel $model, array $data, string $base): void {
162
		if ($model instanceof Circle) {
163
			if ($base === '') {
164
				$base = CoreQueryBuilder::CIRCLE;
165
			}
166
		}
167
168
		if ($model instanceof Member) {
169
			if ($base === '') {
170
				$base = CoreQueryBuilder::MEMBER;
171
			}
172
		}
173
174
		if ($model instanceof ShareWrapper) {
175
			if ($base === '') {
176
				$base = CoreQueryBuilder::SHARE;
177
			}
178
		}
179
180
		if ($model instanceof Mount) {
181
			if ($base === '') {
182
				$base = CoreQueryBuilder::MOUNT;
183
			}
184
		}
185
186
		foreach ($this->coreRequestBuilder->getAvailablePath($base) as $path => $prefix) {
187
			$this->importBasedOnPath($model, $data, $path, $prefix);
188
		}
189
	}
190
191
192
	private function importBasedOnPath(ManagedModel $model, array $data, string $path, string $prefix) {
193
		if ($model instanceof Circle) {
194
			$this->importIntoCircle($model, $data, $path, $prefix);
195
		}
196
197
		if ($model instanceof Member) {
198
			$this->importIntoMember($model, $data, $path, $prefix);
199
		}
200
201
		if ($model instanceof FederatedUser) {
202
			$this->importIntoFederatedUser($model, $data, $path, $prefix);
203
		}
204
205
		if ($model instanceof ShareWrapper) {
206
			$this->importIntoShareWrapper($model, $data, $path, $prefix);
207
		}
208
209
		if ($model instanceof Mount) {
210
			$this->importIntoMount($model, $data, $path, $prefix);
211
		}
212
	}
213
214
215
	/**
216
	 * @param Circle $circle
217
	 * @param array $data
218
	 * @param string $path
219
	 * @param string $prefix
220
	 */
221
	private function importIntoCircle(Circle $circle, array $data, string $path, string $prefix): void {
222
		switch ($path) {
223
			case CoreQueryBuilder::OWNER;
224
				try {
225
					$owner = new Member();
226
					$owner->importFromDatabase($data, $prefix);
227
					$circle->setOwner($owner);
228
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
229
				}
230
				break;
231
232
			case CoreQueryBuilder::INITIATOR;
233
				try {
234
					$initiator = new Member();
235
					$initiator->importFromDatabase($data, $prefix);
236
					$circle->setInitiator($initiator);
237
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
238
				}
239
				break;
240
		}
241
	}
242
243
244
	/**
245
	 * @param Member $member
246
	 * @param array $data
247
	 * @param string $path
248
	 * @param string $prefix
249
	 */
250
	private function importIntoMember(Member $member, array $data, string $path, string $prefix): void {
251
		switch ($path) {
252
			case CoreQueryBuilder::CIRCLE;
253
				try {
254
					$circle = new Circle();
255
					$circle->importFromDatabase($data, $prefix);
256
					$member->setCircle($circle);
257
				} catch (CircleNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
258
				}
259
				break;
260
261
			case CoreQueryBuilder::BASED_ON;
262
				try {
263
					$circle = new Circle();
264
					$circle->importFromDatabase($data, $prefix);
265
					$member->setBasedOn($circle);
266
				} catch (CircleNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
267
				}
268
				break;
269
270
			case CoreQueryBuilder::INHERITED_BY;
271
				try {
272
					$inheritedBy = new FederatedUser();
273
					$inheritedBy->importFromDatabase($data, $prefix);
274
					$member->setInheritedBy($inheritedBy);
275
				} catch (FederatedUserNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
276
				}
277
				break;
278
279
			case CoreQueryBuilder::INVITED_BY;
280
				try {
281
					$invitedByCircle = new Circle();
282
					$invitedByCircle->importFromDatabase($data, $prefix);
283
					$invitedBy = new FederatedUser();
284
					$invitedBy->importFromCircle($invitedByCircle);
285
					$member->setInvitedBy($invitedBy);
286
				} catch (CircleNotFoundException | OwnerNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
287
				}
288
				break;
289
290
			case CoreQueryBuilder::INHERITANCE_FROM;
291
				try {
292
					$inheritanceFrom = new Member();
293
					$inheritanceFrom->importFromDatabase($data, $prefix);
294
					$member->setInheritanceFrom($inheritanceFrom);
295
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
296
				}
297
				break;
298
299
			case CoreQueryBuilder::REMOTE;
300
				try {
301
					$remoteInstance = new RemoteInstance();
302
					$remoteInstance->importFromDatabase($data, $prefix);
303
					$member->setRemoteInstance($remoteInstance);
304
				} catch (RemoteNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
305
				}
306
				break;
307
		}
308
	}
309
310
311
	/**
312
	 * @param FederatedUser $federatedUser
313
	 * @param array $data
314
	 * @param string $path
315
	 * @param string $prefix
316
	 */
317
	private function importIntoFederatedUser(
318
		FederatedUser $federatedUser,
319
		array $data,
320
		string $path,
321
		string $prefix
322
	): void {
323
		switch ($path) {
324
			case CoreQueryBuilder::MEMBERSHIPS;
325
				try {
326
					$membership = new Membership();
327
					$membership->importFromDatabase($data, $prefix);
328
					$federatedUser->setLink($membership);
329
				} catch (MembershipNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
330
				}
331
				break;
332
		}
333
	}
334
335
336
	/**
337
	 * @param ShareWrapper $shareWrapper
338
	 * @param array $data
339
	 * @param string $path
340
	 * @param string $prefix
341
	 */
342
	private function importIntoShareWrapper(
343
		ShareWrapper $shareWrapper,
344
		array $data,
345
		string $path,
346
		string $prefix
347
	): void {
348
		switch ($path) {
349
			case CoreQueryBuilder::CIRCLE;
350
				try {
351
					$circle = new Circle();
352
					$circle->importFromDatabase($data, $prefix);
353
					$shareWrapper->setCircle($circle);
354
				} catch (CircleNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
355
				}
356
				break;
357
358
			case CoreQueryBuilder::INITIATOR;
359
				try {
360
					$initiator = new Member();
361
					$initiator->importFromDatabase($data, $prefix);
362
					$shareWrapper->setInitiator($initiator);
363
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
364
				}
365
				break;
366
367
			case CoreQueryBuilder::INHERITED_BY;
368
				try {
369
					$inheritedBy = new Member();
370
					$inheritedBy->importFromDatabase($data, $prefix);
371
					$shareWrapper->setInitiator($inheritedBy);
372
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
373
				}
374
				break;
375
376
			case CoreQueryBuilder::FILE_CACHE;
377
				try {
378
					$fileCache = new FileCacheWrapper();
379
					$fileCache->importFromDatabase($data, $prefix);
380
					$shareWrapper->setFileCache($fileCache);
381
				} catch (FileCacheNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
382
				}
383
				break;
384
		}
385
	}
386
387
388
	/**
389
	 * @param Mount $mount
390
	 * @param array $data
391
	 * @param string $path
392
	 * @param string $prefix
393
	 */
394
	private function importIntoMount(
395
		Mount $mount,
396
		array $data,
397
		string $path,
398
		string $prefix
399
	): void {
400
		switch ($path) {
401
			case CoreQueryBuilder::MEMBER;
402
				try {
403
					$member = new Member();
404
					$member->importFromDatabase($data, $prefix);
405
					$mount->setOwner($member);
406
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
407
				}
408
				break;
409
410
			case CoreQueryBuilder::INITIATOR;
411
				try {
412
					$initiator = new Member();
413
					$initiator->importFromDatabase($data, $prefix);
414
					$mount->setInitiator($initiator);
415
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
416
				}
417
				break;
418
		}
419
	}
420
421
422
	/**
423
	 * @return string
424
	 */
425
	public function getLocalInstance(): string {
426
		return $this->interfaceService->getLocalInstance();
427
	}
428
429
	/**
430
	 * @param string $instance
431
	 *
432
	 * @return bool
433
	 */
434
	public function isLocalInstance(string $instance): bool {
435
		return $this->configService->isLocalInstance($instance);
436
	}
437
438
439
	/**
440
	 * @param string $instance
441
	 *
442
	 * @return string
443
	 * @throws UnknownInterfaceException
444
	 */
445
	public function fixInstance(string $instance): string {
446
		if (!$this->interfaceService->hasCurrentInterface()) {
447
			return $instance;
448
		}
449
450
		if (!$this->configService->isLocalInstance($instance)) {
451
			return $instance;
452
		}
453
454
		return $this->interfaceService->getCloudInstance();
455
	}
456
457
458
	/**
459
	 * @param string $singleId
460
	 *
461
	 * @return string
462
	 */
463
	public function generateLinkToCircle(string $singleId): string {
464
		return $this->urlGenerator->linkToRoute(
465
			$this->configService->getAppValue(
466
				ConfigService::ROUTE_TO_CIRCLE
467
			),
468
			['singleId' => $singleId]
469
		);
470
	}
471
472
473
	/**
474
	 * @param bool $full
475
	 */
476
	public function setFullDetails(bool $full): void {
477
		$this->fullDetails = $full;
478
	}
479
480
	/**
481
	 * @return bool
482
	 */
483
	public function isFullDetails(): bool {
484
		return $this->fullDetails;
485
	}
486
487
}
488
489