Passed
Pull Request — master (#1128)
by René
04:45
created

Circle::getOrganisation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
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 OCP\IL10N;
28
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...
29
30
use OCA\Polls\Exceptions\CirclesNotEnabled;
31
use OCA\Polls\Interfaces\IUserObj;
32
33
class Circle implements \JsonSerializable, IUserObj {
34
	public const TYPE = 'circle';
35
36
	/** @var IL10N */
37
	private $l10n;
0 ignored issues
show
introduced by
The private property $l10n is not used, and could be removed.
Loading history...
38
39
	/** @var string */
40
	private $id;
41
42
	private $circle;
43
44
	/**
45
	 * Group constructor.
46
	 * @param $id
47
	 * @param $displayName
48
	 */
49
	public function __construct(
50
		$id
51
	) {
52
		$this->id = $id;
53
		$this->load();
54
	}
55
56
	/**
57
	 * getId
58
	 * @NoAdminRequired
59
	 * @return String
60
	 */
61
	public function getId() {
62
		return $this->id;
63
	}
64
65
	/**
66
	 * getUser
67
	 * Necessary for the avatar component
68
	 * @NoAdminRequired
69
	 * @return String
70
	 */
71
	public function getUser() {
72
		return $this->id;
73
	}
74
75
	/**
76
	 * getType
77
	 * @NoAdminRequired
78
	 * @return String
79
	 */
80
	public function getType() {
81
		return self::TYPE;
82
	}
83
84
	/**
85
	 * getlanguage
86
	 * @NoAdminRequired
87
	 * @return String
88
	 */
89
	public function getLanguage() {
90
		return '';
91
	}
92
93
	/**
94
	 * getDisplayName
95
	 * @NoAdminRequired
96
	 * @return String
97
	 */
98
	public function getDisplayName() {
99
		return Circles::detailsCircle($this->id)->getName();
100
	}
101
102
	/**
103
	 * getOrganisation
104
	 * @NoAdminRequired
105
	 * @return String
106
	 */
107
	public function getOrganisation() {
108
		return '';
109
	}
110
111
	/**
112
	 * getEmailAddress
113
	 * @NoAdminRequired
114
	 * @return String
115
	 */
116
	public function getEmailAddress() {
117
		return '';
118
	}
119
120
	/**
121
	 * getDesc
122
	 * @NoAdminRequired
123
	 * @return String
124
	 */
125
	public function getDesc() {
126
		return Circles::detailsCircle($this->id)->gettypeLongString();
127
	}
128
129
	/**
130
	 * getIcon
131
	 * @NoAdminRequired
132
	 * @return String
133
	 */
134
	public function getIcon() {
135
		return 'icon-circles';
136
	}
137
138
	/**
139
	 * load
140
	 * @NoAdminRequired
141
	 * @return Array
142
	 * @throws CirclesNotEnabled
143
	 */
144
	private function load() {
145
		if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
146
			$this->circle = Circles::detailsCircle($this->id);
147
		} else {
148
			throw new CirclesNotEnabled();
149
		}
150
	}
151
152
	/**
153
	 * isEnabled
154
	 * @NoAdminRequired
155
	 * @return Boolean
156
	 */
157
	public static function isEnabled() {
158
		return \OC::$server->getAppManager()->isEnabledForUser('circles');
159
	}
160
161
	/**
162
	 * listRaw
163
	 * @NoAdminRequired
164
	 * @param string $query
165
	 * @return Array
166
	 */
167
	public static function listRaw($query = '') {
168
		$circles = [];
169
		if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
170
			$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...
171
		}
172
173
		return $circles;
174
	}
175
176
	/**
177
	 * search
178
	 * @NoAdminRequired
179
	 * @param string $query
180
	 * @param array $skip - group names to skip in return array
181
	 * @return Circle[]
182
	 */
183
	public static function search($query = '', $skip = []) {
184
		$circles = [];
185
		foreach (self::listRaw($query) as $circle) {
186
			if (!in_array($circle->getUniqueId(), $skip)) {
187
				$circles[] = new Self($circle->getUniqueId());
188
			}
189
		}
190
191
		return $circles;
192
	}
193
194
	/**
195
	 * Get a list of circle members
196
	 * @NoAdminRequired
197
	 * @param string $query
198
	 * @return User[]
199
	 */
200
	public function getMembers() {
201
		$members = [];
202
		if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
203
			foreach (Circles::detailsCircle($this->id)->getMembers() as $circleMember) {
204
				if ($circleMember->getType() === Circles::TYPE_USER) {
205
					$members[] = new User($circleMember->getUserId());
206
				} elseif ($circleMember->getType() === Circles::TYPE_MAIL) {
207
					$members[] = new Email($circleMember->getUserId());
208
				} elseif ($circleMember->getType() === Circles::TYPE_CONTACT) {
209
					$members[] = new Contact($circleMember->getUserId());
210
				} else {
211
					continue;
212
				}
213
			}
214
		}
215
		return $members;
216
	}
217
218
	/**
219
	 * @return array
220
	 */
221
	public function jsonSerialize(): array {
222
		return	[
223
			'id'        	=> $this->id,
224
			'user'          => $this->id,
225
			'type'       	=> $this->getType(),
226
			'displayName'	=> $this->getDisplayName(),
227
			'organisation'	=> $this->getOrganisation(),
228
			'emailAddress'	=> $this->getEmailAddress(),
229
			'desc' 			=> $this->getDesc(),
230
			'icon'			=> $this->getIcon(),
231
			'isNoUser'		=> true,
232
		];
233
	}
234
}
235