Completed
Pull Request — master (#586)
by Maxence
02:42
created

ModelManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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