Passed
Pull Request — master (#1128)
by René
06:43
created

Circle   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
eloc 51
c 1
b 0
f 0
dl 0
loc 196
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 2 1
A getIcon() 0 2 1
A jsonSerialize() 0 11 1
A getDescription() 0 2 1
A load() 0 5 2
A search() 0 9 3
A getMembers() 0 16 6
A getOrganisation() 0 2 1
A getDisplayName() 0 2 1
A getUser() 0 2 1
A isEnabled() 0 2 1
A __construct() 0 5 1
A getEmailAddress() 0 2 1
A listRaw() 0 7 2
A getLanguage() 0 2 1
A getType() 0 2 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
25
namespace OCA\Polls\Model;
26
27
use OCA\Circles\Api\v1\Circles;
0 ignored issues
show
Bug introduced by
The type OCA\Circles\Api\v1\Circles was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
29
use OCA\Polls\Exceptions\CirclesNotEnabled;
30
use OCA\Polls\Interfaces\IUserObj;
31
32
class Circle implements \JsonSerializable, IUserObj {
33
	public const TYPE = 'circle';
34
35
	/** @var string */
36
	private $id;
37
38
	private $circle;
39
40
	/**
41
	 * Group constructor.
42
	 * @param $id
43
	 * @param $displayName
44
	 */
45
	public function __construct(
46
		$id
47
	) {
48
		$this->id = $id;
49
		$this->load();
50
	}
51
52
	/**
53
	 * getId
54
	 * @NoAdminRequired
55
	 * @return String
56
	 */
57
	public function getId() {
58
		return $this->id;
59
	}
60
61
	/**
62
	 * getUser
63
	 * Necessary for the avatar component
64
	 * @NoAdminRequired
65
	 * @return String
66
	 */
67
	public function getUser() {
68
		return $this->id;
69
	}
70
71
	/**
72
	 * getType
73
	 * @NoAdminRequired
74
	 * @return String
75
	 */
76
	public function getType() {
77
		return self::TYPE;
78
	}
79
80
	/**
81
	 * getlanguage
82
	 * @NoAdminRequired
83
	 * @return String
84
	 */
85
	public function getLanguage() {
86
		return '';
87
	}
88
89
	/**
90
	 * getDisplayName
91
	 * @NoAdminRequired
92
	 * @return String
93
	 */
94
	public function getDisplayName() {
95
		return Circles::detailsCircle($this->id)->getName();
96
	}
97
98
	/**
99
	 * getOrganisation
100
	 * @NoAdminRequired
101
	 * @return String
102
	 */
103
	public function getOrganisation() {
104
		return '';
105
	}
106
107
	/**
108
	 * getEmailAddress
109
	 * @NoAdminRequired
110
	 * @return String
111
	 */
112
	public function getEmailAddress() {
113
		return '';
114
	}
115
116
	/**
117
	 * getDescription
118
	 * @NoAdminRequired
119
	 * @return String
120
	 */
121
	public function getDescription() {
122
		return Circles::detailsCircle($this->id)->gettypeLongString();
123
	}
124
125
	/**
126
	 * getIcon
127
	 * @NoAdminRequired
128
	 * @return String
129
	 */
130
	public function getIcon() {
131
		return 'icon-circles';
132
	}
133
134
	/**
135
	 * load
136
	 * @NoAdminRequired
137
	 * @return Array
138
	 * @throws CirclesNotEnabled
139
	 */
140
	private function load() {
141
		if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
142
			$this->circle = Circles::detailsCircle($this->id);
143
		} else {
144
			throw new CirclesNotEnabled();
145
		}
146
	}
147
148
	/**
149
	 * isEnabled
150
	 * @NoAdminRequired
151
	 * @return Boolean
152
	 */
153
	public static function isEnabled() {
154
		return \OC::$server->getAppManager()->isEnabledForUser('circles');
155
	}
156
157
	/**
158
	 * listRaw
159
	 * @NoAdminRequired
160
	 * @param string $query
161
	 * @return Array
162
	 */
163
	public static function listRaw($query = '') {
164
		$circles = [];
165
		if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
166
			$circles = Circles::listCircles(\OCA\Circles\Model\Circle::CIRCLES_ALL, $query);
0 ignored issues
show
Bug introduced by
The type OCA\Circles\Model\Circle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
167
		}
168
169
		return $circles;
170
	}
171
172
	/**
173
	 * search
174
	 * @NoAdminRequired
175
	 * @param string $query
176
	 * @param array $skip - group names to skip in return array
177
	 * @return Circle[]
178
	 */
179
	public static function search($query = '', $skip = []) {
180
		$circles = [];
181
		foreach (self::listRaw($query) as $circle) {
182
			if (!in_array($circle->getUniqueId(), $skip)) {
183
				$circles[] = new Self($circle->getUniqueId());
184
			}
185
		}
186
187
		return $circles;
188
	}
189
190
	/**
191
	 * Get a list of circle members
192
	 * @NoAdminRequired
193
	 * @param string $query
194
	 * @return User[]
195
	 */
196
	public function getMembers() {
197
		$members = [];
198
		if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
199
			foreach (Circles::detailsCircle($this->id)->getMembers() as $circleMember) {
200
				if ($circleMember->getType() === Circles::TYPE_USER) {
201
					$members[] = new User($circleMember->getUserId());
202
				} elseif ($circleMember->getType() === Circles::TYPE_MAIL) {
203
					$members[] = new Email($circleMember->getUserId());
204
				} elseif ($circleMember->getType() === Circles::TYPE_CONTACT) {
205
					$members[] = new Contact($circleMember->getUserId());
206
				} else {
207
					continue;
208
				}
209
			}
210
		}
211
		return $members;
212
	}
213
214
	/**
215
	 * @return array
216
	 */
217
	public function jsonSerialize(): array {
218
		return	[
219
			'id'        	=> $this->id,
220
			'user'          => $this->id,
221
			'type'       	=> $this->getType(),
222
			'displayName'	=> $this->getDisplayName(),
223
			'organisation'	=> $this->getOrganisation(),
224
			'emailAddress'	=> $this->getEmailAddress(),
225
			'desc' 			=> $this->getDescription(),
226
			'icon'			=> $this->getIcon(),
227
			'isNoUser'		=> true,
228
		];
229
	}
230
}
231