Passed
Pull Request — master (#1169)
by René
06:00
created

Circle   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 34
c 2
b 0
f 0
dl 0
loc 89
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 0 9 3
A getMembers() 0 16 6
A isEnabled() 0 2 1
A __construct() 0 11 2
A listRaw() 0 7 2
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
31
class Circle extends UserGroupClass {
32
	public const TYPE = 'circle';
33
	public const ICON = 'icon-circles';
34
35
	private $circle;
36
37
	/**
38
	 * Group constructor.
39
	 * @param $id
40
	 * @param $displayName
41
	 */
42
	public function __construct(
43
		$id
44
	) {
45
		parent::__construct($id, self::TYPE);
46
		if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
47
			$this->icon = self::ICON;
48
			$this->circle = Circles::detailsCircle($id);
49
			$this->displayName = $this->circle->getName();
50
			$this->description = $this->circle->gettypeLongString();
51
		} else {
52
			throw new CirclesNotEnabled();
53
		}
54
	}
55
56
	/**
57
	 * isEnabled
58
	 * @NoAdminRequired
59
	 * @return Boolean
60
	 */
61
	public static function isEnabled() {
62
		return \OC::$server->getAppManager()->isEnabledForUser('circles');
63
	}
64
65
	/**
66
	 * listRaw
67
	 * @NoAdminRequired
68
	 * @param string $query
69
	 * @return Array
70
	 */
71
	public static function listRaw($query = '') {
72
		$circles = [];
73
		if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
74
			$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...
75
		}
76
77
		return $circles;
78
	}
79
80
	/**
81
	 * search
82
	 * @NoAdminRequired
83
	 * @param string $query
84
	 * @param array $skip - group names to skip in return array
85
	 * @return Circle[]
86
	 */
87
	public static function search($query = '', $skip = []) {
88
		$circles = [];
89
		foreach (self::listRaw($query) as $circle) {
90
			if (!in_array($circle->getUniqueId(), $skip)) {
91
				$circles[] = new Self($circle->getUniqueId());
92
			}
93
		}
94
95
		return $circles;
96
	}
97
98
	/**
99
	 * Get a list of circle members
100
	 * @NoAdminRequired
101
	 * @param string $query
102
	 * @return User[]
103
	 */
104
	public function getMembers() {
105
		$members = [];
106
		if (\OC::$server->getAppManager()->isEnabledForUser('circles')) {
107
			foreach (Circles::detailsCircle($this->id)->getMembers() as $circleMember) {
108
				if ($circleMember->getType() === Circles::TYPE_USER) {
109
					$members[] = new User($circleMember->getUserId());
110
				} elseif ($circleMember->getType() === Circles::TYPE_MAIL) {
111
					$members[] = new Email($circleMember->getUserId());
112
				} elseif ($circleMember->getType() === Circles::TYPE_CONTACT) {
113
					$members[] = new Contact($circleMember->getUserId());
114
				} else {
115
					continue;
116
				}
117
			}
118
		}
119
		return $members;
120
	}
121
}
122