Completed
Push — master ( 3ddca8...414a73 )
by Maxence
02:57
created

ModelManager::generateLinkToCircle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\RemoteNotFoundException;
44
use OCA\Circles\Exceptions\RequestBuilderException;
45
use OCA\Circles\Exceptions\UnknownInterfaceException;
46
use OCA\Circles\IMemberships;
47
use OCA\Circles\Model\Federated\RemoteInstance;
48
use OCA\Circles\Service\ConfigService;
49
use OCA\Circles\Service\InterfaceService;
50
use OCP\IURLGenerator;
51
52
/**
53
 * Class ModelManager
54
 *
55
 * @package OCA\Circles\Model
56
 */
57
class ModelManager {
58
59
60
	/** @var IURLGenerator */
61
	private $urlGenerator;
62
63
	/** @var CoreQueryBuilder */
64
	private $coreRequestBuilder;
65
66
	/** @var MemberRequest */
67
	private $memberRequest;
68
69
	/** @var MembershipRequest */
70
	private $membershipRequest;
71
72
	/** @var InterfaceService */
73
	private $interfaceService;
74
75
	/** @var ConfigService */
76
	private $configService;
77
78
79
	/** @var bool */
80
	private $fullDetails = false;
81
82
83
	/**
84
	 * ModelManager constructor.
85
	 *
86
	 * @param IURLGenerator $urlGenerator
87
	 * @param CoreQueryBuilder $coreRequestBuilder
88
	 * @param MemberRequest $memberRequest
89
	 * @param MembershipRequest $membershipRequest
90
	 * @param InterfaceService $interfaceService
91
	 * @param ConfigService $configService
92
	 */
93
	public function __construct(
94
		IURLGenerator $urlGenerator,
95
		CoreQueryBuilder $coreRequestBuilder,
96
		MemberRequest $memberRequest,
97
		MembershipRequest $membershipRequest,
98
		InterfaceService $interfaceService,
99
		ConfigService $configService
100
	) {
101
		$this->urlGenerator = $urlGenerator;
102
		$this->coreRequestBuilder = $coreRequestBuilder;
103
		$this->memberRequest = $memberRequest;
104
		$this->membershipRequest = $membershipRequest;
105
		$this->interfaceService = $interfaceService;
106
		$this->configService = $configService;
107
	}
108
109
110
	/**
111
	 * @return ConfigService
112
	 */
113
	public function getConfigService(): ConfigService {
114
		return $this->configService;
115
	}
116
117
118
	/**
119
	 * @param Circle $circle
120
	 */
121
	public function getMembers(Circle $circle): void {
122
		try {
123
			$circle->setMembers($this->memberRequest->getMembers($circle->getSingleId()));
124
		} catch (RequestBuilderException $e) {
125
			// TODO: debug log
126
		}
127
	}
128
129
130
	/**
131
	 * @param Circle $circle
132
	 * @param bool $detailed
133
	 */
134
	public function getInheritedMembers(Circle $circle, bool $detailed = false): void {
135
		try {
136
			$circle->setInheritedMembers(
137
				$this->memberRequest->getInheritedMembers($circle->getSingleId(), $detailed),
138
				$detailed
139
			);
140
		} catch (RequestBuilderException $e) {
141
			// TODO: debug log
142
		}
143
	}
144
145
146
	/**
147
	 * @param IMemberships $member
148
	 */
149
	public function getMemberships(IMemberships $member): void {
150
		$memberships = $this->membershipRequest->getMemberships($member->getSingleId());
151
		$member->setMemberships($memberships);
152
	}
153
154
155
	/**
156
	 * @param ManagedModel $model
157
	 * @param array $data
158
	 * @param string $base
159
	 */
160
	public function manageImportFromDatabase(ManagedModel $model, array $data, string $base): void {
161
		if ($model instanceof Circle) {
162
			if ($base === '') {
163
				$base = CoreQueryBuilder::CIRCLE;
164
			}
165
		}
166
167
		if ($model instanceof Member) {
168
			if ($base === '') {
169
				$base = CoreQueryBuilder::MEMBER;
170
			}
171
		}
172
173
		if ($model instanceof ShareWrapper) {
174
			if ($base === '') {
175
				$base = CoreQueryBuilder::SHARE;
176
			}
177
		}
178
179
		if ($model instanceof Mount) {
180
			if ($base === '') {
181
				$base = CoreQueryBuilder::MOUNT;
182
			}
183
		}
184
185
		foreach ($this->coreRequestBuilder->getAvailablePath($base) as $path => $prefix) {
186
			$this->importBasedOnPath($model, $data, $path, $prefix);
187
		}
188
	}
189
190
191
	private function importBasedOnPath(ManagedModel $model, array $data, string $path, string $prefix) {
192
		if ($model instanceof Circle) {
193
			$this->importIntoCircle($model, $data, $path, $prefix);
194
		}
195
196
		if ($model instanceof Member) {
197
			$this->importIntoMember($model, $data, $path, $prefix);
198
		}
199
200
		if ($model instanceof FederatedUser) {
201
			$this->importIntoFederatedUser($model, $data, $path, $prefix);
202
		}
203
204
		if ($model instanceof ShareWrapper) {
205
			$this->importIntoShareWrapper($model, $data, $path, $prefix);
206
		}
207
208
		if ($model instanceof Mount) {
209
			$this->importIntoMount($model, $data, $path, $prefix);
210
		}
211
	}
212
213
214
	/**
215
	 * @param Circle $circle
216
	 * @param array $data
217
	 * @param string $path
218
	 * @param string $prefix
219
	 */
220 View Code Duplication
	private function importIntoCircle(Circle $circle, array $data, string $path, string $prefix): void {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
		switch ($path) {
222
			case CoreQueryBuilder::OWNER;
223
				try {
224
					$owner = new Member();
225
					$owner->importFromDatabase($data, $prefix);
226
					$circle->setOwner($owner);
227
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
228
				}
229
				break;
230
231
			case CoreQueryBuilder::INITIATOR;
232
				try {
233
					$initiator = new Member();
234
					$initiator->importFromDatabase($data, $prefix);
235
					$circle->setInitiator($initiator);
236
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
237
				}
238
				break;
239
		}
240
	}
241
242
243
	/**
244
	 * @param Member $member
245
	 * @param array $data
246
	 * @param string $path
247
	 * @param string $prefix
248
	 */
249
	private function importIntoMember(Member $member, array $data, string $path, string $prefix): void {
250
		switch ($path) {
251 View Code Duplication
			case CoreQueryBuilder::CIRCLE;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
252
				try {
253
					$circle = new Circle();
254
					$circle->importFromDatabase($data, $prefix);
255
					$member->setCircle($circle);
256
				} catch (CircleNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
257
				}
258
				break;
259
260 View Code Duplication
			case CoreQueryBuilder::BASED_ON;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
				try {
262
					$circle = new Circle();
263
					$circle->importFromDatabase($data, $prefix);
264
					$member->setBasedOn($circle);
265
				} catch (CircleNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
266
				}
267
				break;
268
269
			case CoreQueryBuilder::INHERITED_BY;
270
				try {
271
					$inheritedBy = new FederatedUser();
272
					$inheritedBy->importFromDatabase($data, $prefix);
273
					$member->setInheritedBy($inheritedBy);
274
				} catch (FederatedUserNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
275
				}
276
				break;
277
278
			case CoreQueryBuilder::INHERITANCE_FROM;
279
				try {
280
					$inheritanceFrom = new Member();
281
					$inheritanceFrom->importFromDatabase($data, $prefix);
282
					$member->setInheritanceFrom($inheritanceFrom);
283
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
284
				}
285
				break;
286
287
			case CoreQueryBuilder::REMOTE;
288
				try {
289
					$remoteInstance = new RemoteInstance();
290
					$remoteInstance->importFromDatabase($data, $prefix);
291
					$member->setRemoteInstance($remoteInstance);
292
				} catch (RemoteNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
293
				}
294
				break;
295
		}
296
	}
297
298
299
	/**
300
	 * @param FederatedUser $federatedUser
301
	 * @param array $data
302
	 * @param string $path
303
	 * @param string $prefix
304
	 */
305
	private function importIntoFederatedUser(
306
		FederatedUser $federatedUser,
307
		array $data,
308
		string $path,
309
		string $prefix
310
	): void {
311
		switch ($path) {
312
			case CoreQueryBuilder::MEMBERSHIPS;
313
				try {
314
					$membership = new Membership();
315
					$membership->importFromDatabase($data, $prefix);
316
					$federatedUser->setLink($membership);
317
				} catch (MembershipNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
318
				}
319
				break;
320
		}
321
	}
322
323
324
	/**
325
	 * @param ShareWrapper $shareWrapper
326
	 * @param array $data
327
	 * @param string $path
328
	 * @param string $prefix
329
	 */
330
	private function importIntoShareWrapper(
331
		ShareWrapper $shareWrapper,
332
		array $data,
333
		string $path,
334
		string $prefix
335
	): void {
336
		switch ($path) {
337 View Code Duplication
			case CoreQueryBuilder::CIRCLE;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
338
				try {
339
					$circle = new Circle();
340
					$circle->importFromDatabase($data, $prefix);
341
					$shareWrapper->setCircle($circle);
342
				} catch (CircleNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
343
				}
344
				break;
345
346
			case CoreQueryBuilder::INITIATOR;
347
				try {
348
					$initiator = new Member();
349
					$initiator->importFromDatabase($data, $prefix);
350
					$shareWrapper->setInitiator($initiator);
351
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
352
				}
353
				break;
354
355
			case CoreQueryBuilder::INHERITED_BY;
356
				try {
357
					$inheritedBy = new Member();
358
					$inheritedBy->importFromDatabase($data, $prefix);
359
					$shareWrapper->setInitiator($inheritedBy);
360
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
361
				}
362
				break;
363
364
			case CoreQueryBuilder::FILE_CACHE;
365
				try {
366
					$fileCache = new FileCacheWrapper();
367
					$fileCache->importFromDatabase($data, $prefix);
368
					$shareWrapper->setFileCache($fileCache);
369
				} catch (FileCacheNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
370
				}
371
				break;
372
		}
373
	}
374
375
376
	/**
377
	 * @param Mount $mount
378
	 * @param array $data
379
	 * @param string $path
380
	 * @param string $prefix
381
	 */
382 View Code Duplication
	private function importIntoMount(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
383
		Mount $mount,
384
		array $data,
385
		string $path,
386
		string $prefix
387
	): void {
388
		switch ($path) {
389
			case CoreQueryBuilder::MEMBER;
390
				try {
391
					$member = new Member();
392
					$member->importFromDatabase($data, $prefix);
393
					$mount->setOwner($member);
394
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
395
				}
396
				break;
397
398
			case CoreQueryBuilder::INITIATOR;
399
				try {
400
					$initiator = new Member();
401
					$initiator->importFromDatabase($data, $prefix);
402
					$mount->setInitiator($initiator);
403
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
404
				}
405
				break;
406
		}
407
	}
408
409
410
	/**
411
	 * @return string
412
	 */
413
	public function getLocalInstance(): string {
414
		return $this->interfaceService->getLocalInstance();
415
	}
416
417
	/**
418
	 * @param string $instance
419
	 *
420
	 * @return bool
421
	 */
422
	public function isLocalInstance(string $instance): bool {
423
		return $this->configService->isLocalInstance($instance);
424
	}
425
426
427
	/**
428
	 * @param string $instance
429
	 *
430
	 * @return string
431
	 * @throws UnknownInterfaceException
432
	 */
433
	public function fixInstance(string $instance): string {
434
		if (!$this->interfaceService->hasCurrentInterface()) {
435
			return $instance;
436
		}
437
438
		if (!$this->configService->isLocalInstance($instance)) {
439
			return $instance;
440
		}
441
442
		return $this->interfaceService->getCloudInstance();
443
	}
444
445
446
	/**
447
	 * @param string $singleId
448
	 *
449
	 * @return string
450
	 */
451
	public function generateLinkToCircle(string $singleId): string {
452
		return $this->urlGenerator->linkToRouteAbsolute(
453
			$this->configService->getAppValue(ConfigService::ROUTE_TO_CIRCLE),
454
			['singleId' => $singleId]
455
		);
456
	}
457
458
459
	/**
460
	 * @param bool $full
461
	 */
462
	public function setFullDetails(bool $full): void {
463
		$this->fullDetails = $full;
464
	}
465
466
	/**
467
	 * @return bool
468
	 */
469
	public function isFullDetails(): bool {
470
		return $this->fullDetails;
471
	}
472
473
}
474
475