Completed
Pull Request — master (#597)
by Maxence
02:57
created

ModelManager::getMembers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
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
51
/**
52
 * Class ModelManager
53
 *
54
 * @package OCA\Circles\Model
55
 */
56
class ModelManager {
57
58
59
	/** @var CoreQueryBuilder */
60
	private $coreRequestBuilder;
61
62
	/** @var MemberRequest */
63
	private $memberRequest;
64
65
	/** @var MembershipRequest */
66
	private $membershipRequest;
67
68
	/** @var InterfaceService */
69
	private $interfaceService;
70
71
	/** @var ConfigService */
72
	private $configService;
73
74
75
	/** @var bool */
76
	private $fullDetails = false;
77
78
79
	/**
80
	 * ModelManager constructor.
81
	 *
82
	 * @param CoreQueryBuilder $coreRequestBuilder
83
	 * @param MemberRequest $memberRequest
84
	 * @param MembershipRequest $membershipRequest
85
	 * @param ConfigService $configService
86
	 */
87
	public function __construct(
88
		CoreQueryBuilder $coreRequestBuilder,
89
		MemberRequest $memberRequest,
90
		MembershipRequest $membershipRequest,
91
		InterfaceService $interfaceService,
92
		ConfigService $configService
93
	) {
94
		$this->coreRequestBuilder = $coreRequestBuilder;
95
		$this->memberRequest = $memberRequest;
96
		$this->membershipRequest = $membershipRequest;
97
		$this->interfaceService = $interfaceService;
98
		$this->configService = $configService;
99
	}
100
101
102
	/**
103
	 * @return ConfigService
104
	 */
105
	public function getConfigService(): ConfigService {
106
		return $this->configService;
107
	}
108
109
110
	/**
111
	 * @param Circle $circle
112
	 */
113
	public function getMembers(Circle $circle): void {
114
		try {
115
			$circle->setMembers($this->memberRequest->getMembers($circle->getSingleId()));
116
		} catch (RequestBuilderException $e) {
117
			// TODO: debug log
118
		}
119
	}
120
121
122
	/**
123
	 * @param Circle $circle
124
	 * @param bool $detailed
125
	 */
126
	public function getInheritedMembers(Circle $circle, bool $detailed = false): void {
127
		try {
128
			$circle->setInheritedMembers(
129
				$this->memberRequest->getInheritedMembers($circle->getSingleId(), $detailed),
130
				$detailed
131
			);
132
		} catch (RequestBuilderException $e) {
133
			// TODO: debug log
134
		}
135
	}
136
137
138
	/**
139
	 * @param IMemberships $member
140
	 */
141
	public function getMemberships(IMemberships $member): void {
142
		$memberships = $this->membershipRequest->getMemberships($member->getSingleId());
143
		$member->setMemberships($memberships);
144
	}
145
146
147
	/**
148
	 * @param ManagedModel $model
149
	 * @param array $data
150
	 * @param string $base
151
	 */
152
	public function manageImportFromDatabase(ManagedModel $model, array $data, string $base): void {
153
		if ($model instanceof Circle) {
154
			if ($base === '') {
155
				$base = CoreQueryBuilder::CIRCLE;
156
			}
157
		}
158
159
		if ($model instanceof Member) {
160
			if ($base === '') {
161
				$base = CoreQueryBuilder::MEMBER;
162
			}
163
		}
164
165
		if ($model instanceof ShareWrapper) {
166
			if ($base === '') {
167
				$base = CoreQueryBuilder::SHARE;
168
			}
169
		}
170
171
		if ($model instanceof Mount) {
172
			if ($base === '') {
173
				$base = CoreQueryBuilder::MOUNT;
174
			}
175
		}
176
177
		foreach ($this->coreRequestBuilder->getAvailablePath($base) as $path => $prefix) {
178
			$this->importBasedOnPath($model, $data, $path, $prefix);
179
		}
180
	}
181
182
183
	private function importBasedOnPath(ManagedModel $model, array $data, string $path, string $prefix) {
184
		if ($model instanceof Circle) {
185
			$this->importIntoCircle($model, $data, $path, $prefix);
186
		}
187
188
		if ($model instanceof Member) {
189
			$this->importIntoMember($model, $data, $path, $prefix);
190
		}
191
192
		if ($model instanceof FederatedUser) {
193
			$this->importIntoFederatedUser($model, $data, $path, $prefix);
194
		}
195
196
		if ($model instanceof ShareWrapper) {
197
			$this->importIntoShareWrapper($model, $data, $path, $prefix);
198
		}
199
200
		if ($model instanceof Mount) {
201
			$this->importIntoMount($model, $data, $path, $prefix);
202
		}
203
	}
204
205
206
	/**
207
	 * @param Circle $circle
208
	 * @param array $data
209
	 * @param string $path
210
	 * @param string $prefix
211
	 */
212 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...
213
		switch ($path) {
214
			case CoreQueryBuilder::OWNER;
215
				try {
216
					$owner = new Member();
217
					$owner->importFromDatabase($data, $prefix);
218
					$circle->setOwner($owner);
219
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
220
				}
221
				break;
222
223
			case CoreQueryBuilder::INITIATOR;
224
				try {
225
					$initiator = new Member();
226
					$initiator->importFromDatabase($data, $prefix);
227
					$circle->setInitiator($initiator);
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
	}
233
234
235
	/**
236
	 * @param Member $member
237
	 * @param array $data
238
	 * @param string $path
239
	 * @param string $prefix
240
	 */
241
	private function importIntoMember(Member $member, array $data, string $path, string $prefix): void {
242
		switch ($path) {
243 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...
244
				try {
245
					$circle = new Circle();
246
					$circle->importFromDatabase($data, $prefix);
247
					$member->setCircle($circle);
248
				} catch (CircleNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
249
				}
250
				break;
251
252 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...
253
				try {
254
					$circle = new Circle();
255
					$circle->importFromDatabase($data, $prefix);
256
					$member->setBasedOn($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::INHERITED_BY;
262
				try {
263
					$inheritedBy = new FederatedUser();
264
					$inheritedBy->importFromDatabase($data, $prefix);
265
					$member->setInheritedBy($inheritedBy);
266
				} catch (FederatedUserNotFoundException $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::INHERITANCE_FROM;
271
				try {
272
					$inheritanceFrom = new Member();
273
					$inheritanceFrom->importFromDatabase($data, $prefix);
274
					$member->setInheritanceFrom($inheritanceFrom);
275
				} catch (MemberNotFoundException $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::REMOTE;
280
				try {
281
					$remoteInstance = new RemoteInstance();
282
					$remoteInstance->importFromDatabase($data, $prefix);
283
					$member->setRemoteInstance($remoteInstance);
284
				} catch (RemoteNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
285
				}
286
				break;
287
		}
288
	}
289
290
291
	/**
292
	 * @param FederatedUser $federatedUser
293
	 * @param array $data
294
	 * @param string $path
295
	 * @param string $prefix
296
	 */
297
	private function importIntoFederatedUser(
298
		FederatedUser $federatedUser,
299
		array $data,
300
		string $path,
301
		string $prefix
302
	): void {
303
		switch ($path) {
304
			case CoreQueryBuilder::MEMBERSHIPS;
305
				try {
306
					$membership = new Membership();
307
					$membership->importFromDatabase($data, $prefix);
308
					$federatedUser->setLink($membership);
309
				} catch (MembershipNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
310
				}
311
				break;
312
		}
313
	}
314
315
316
	/**
317
	 * @param ShareWrapper $shareWrapper
318
	 * @param array $data
319
	 * @param string $path
320
	 * @param string $prefix
321
	 */
322
	private function importIntoShareWrapper(
323
		ShareWrapper $shareWrapper,
324
		array $data,
325
		string $path,
326
		string $prefix
327
	): void {
328
		switch ($path) {
329 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...
330
				try {
331
					$circle = new Circle();
332
					$circle->importFromDatabase($data, $prefix);
333
					$shareWrapper->setCircle($circle);
334
				} catch (CircleNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
335
				}
336
				break;
337
338
			case CoreQueryBuilder::INITIATOR;
339
				try {
340
					$initiator = new Member();
341
					$initiator->importFromDatabase($data, $prefix);
342
					$shareWrapper->setInitiator($initiator);
343
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
344
				}
345
				break;
346
347
			case CoreQueryBuilder::INHERITED_BY;
348
				try {
349
					$inheritedBy = new Member();
350
					$inheritedBy->importFromDatabase($data, $prefix);
351
					$shareWrapper->setInitiator($inheritedBy);
352
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
353
				}
354
				break;
355
356
			case CoreQueryBuilder::FILE_CACHE;
357
				try {
358
					$fileCache = new FileCacheWrapper();
359
					$fileCache->importFromDatabase($data, $prefix);
360
					$shareWrapper->setFileCache($fileCache);
361
				} catch (FileCacheNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
362
				}
363
				break;
364
		}
365
	}
366
367
368
	/**
369
	 * @param Mount $mount
370
	 * @param array $data
371
	 * @param string $path
372
	 * @param string $prefix
373
	 */
374 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...
375
		Mount $mount,
376
		array $data,
377
		string $path,
378
		string $prefix
379
	): void {
380
		switch ($path) {
381
			case CoreQueryBuilder::MEMBER;
382
				try {
383
					$member = new Member();
384
					$member->importFromDatabase($data, $prefix);
385
					$mount->setOwner($member);
386
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
387
				}
388
				break;
389
390
			case CoreQueryBuilder::INITIATOR;
391
				try {
392
					$initiator = new Member();
393
					$initiator->importFromDatabase($data, $prefix);
394
					$mount->setInitiator($initiator);
395
				} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
396
				}
397
				break;
398
		}
399
	}
400
401
	/**
402
	 * @return string
403
	 */
404
	public function getLocalInstance(): string {
405
		return $this->interfaceService->getLocalInstance();
406
	}
407
408
	/**
409
	 * @param string $instance
410
	 *
411
	 * @return string
412
	 * @throws UnknownInterfaceException
413
	 */
414
	public function fixInstance(string $instance): string {
415
		if (!$this->interfaceService->hasCurrentInterface()) {
416
			return $instance;
417
		}
418
419
		if (!$this->configService->isLocalInstance($instance)) {
420
			return $instance;
421
		}
422
423
		return $this->interfaceService->getCloudInstance();
424
	}
425
426
	/**
427
	 * @param bool $full
428
	 */
429
	public function setFullDetails(bool $full): void {
430
		$this->fullDetails = $full;
431
	}
432
433
	/**
434
	 * @return bool
435
	 */
436
	public function isFullDetails(): bool {
437
		return $this->fullDetails;
438
	}
439
440
}
441
442