Completed
Pull Request — master (#546)
by Maxence
03:01
created

ModelManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 2
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getMembers() 0 5 2
A importOwnerFromDatabase() 0 8 2
A getCircleTypes() 0 20 5
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\DeprecatedCirclesRequest;
36
use OCA\Circles\Exceptions\MemberNotFoundException;
37
38
/**
39
 * Class ModelManager
40
 *
41
 * @package OCA\Circles\Model
42
 */
43
class ModelManager {
44
45
46
	const TYPES_SHORT = 1;
47
	const TYPES_LONG = 2;
48
49
50
	/** @var DeprecatedCirclesRequest */
51
	private $circlesRequest;
52
53
54
	public function __construct(DeprecatedCirclesRequest $circlesRequest) {
55
		$this->circlesRequest = $circlesRequest;
56
	}
57
58
59
	public function getMembers(Circle $circle): void {
60
		if (empty($circle->getMembers())) {
61
			$circle->setMembers(['oui' => 1]);
62
		}
63
	}
64
65
66
	/**
67
	 * @param Circle $circle
68
	 * @param array $data
69
	 */
70
	public function importOwnerFromDatabase(Circle $circle, array $data): void {
71
		try {
72
			$owner = new Member();
73
			$owner->importFromDatabase($data, 'owner_');
74
			$circle->setOwner($owner);
75
		} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
76
		}
77
	}
78
79
80
	/**
81
	 * @param Circle $circle
82
	 * @param int $display
83
	 *
84
	 * @return array
85
	 */
86
	public function getCircleTypes(Circle $circle, int $display = self::TYPES_LONG): array {
87
		$types = [];
88
		foreach (array_keys(Circle::$DEF) as $def) {
0 ignored issues
show
Bug introduced by
The property DEF cannot be accessed from this context as it is declared private in class OCA\Circles\Model\Circle.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
89
			if ($circle->isType($def)) {
90
				list($short, $long) = explode('|', Circle::$DEF[$def]);
0 ignored issues
show
Bug introduced by
The property DEF cannot be accessed from this context as it is declared private in class OCA\Circles\Model\Circle.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
91
				switch ($display) {
92
93
					case self::TYPES_SHORT:
94
						$types[] = $short;
95
						break;
96
97
					case self::TYPES_LONG:
98
						$types[] = $long;
99
						break;
100
				}
101
			}
102
		}
103
104
		return $types;
105
	}
106
107
108
}
109
110