Completed
Push — master ( 5d3621...1755ef )
by Maxence
02:25
created

ModelManager::importIntoMember()   B

Complexity

Conditions 11
Paths 21

Size

Total Lines 48

Duplication

Lines 16
Ratio 33.33 %

Importance

Changes 0
Metric Value
dl 16
loc 48
rs 7.3166
c 0
b 0
f 0
cc 11
nc 21
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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