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

CirclesService   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 17
eloc 29
dl 0
loc 81
rs 10
c 2
b 0
f 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getCircleMembers() 0 17 7
A getDisplayName() 0 5 2
A getDetails() 0 5 2
A getCircles() 0 16 5
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
namespace OCA\Polls\Service;
25
use OCA\Polls\Model\User;
26
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...
27
28
class CirclesService {
29
	public function __construct(
30
	) {
31
		$this->enabled = \OC::$server->getAppManager()->isEnabledForUser('circles');
0 ignored issues
show
Bug Best Practice introduced by
The property enabled does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
	}
33
34
	/**
35
	 * Get a list of avaiable circles
36
	 * @NoAdminRequired
37
	 * @param string $query
38
	 * @param array $skip - circle names to skip in return array
39
	 * @return Array
40
	 */
41
	public function getCircles($query = '', $skip = []) {
42
		if (!$this->enabled || $query === '') {
43
			return [];
44
		}
45
46
		$circles = [];
47
48
49
		foreach (\OCA\Circles\Api\v1\Circles::listCircles(\OCA\Circles\Model\Circle::CIRCLES_ALL, $query) as $circle) {
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...
50
			if (!in_array($circle->getId(), $skip)) {
51
				$displayName = $circle->getName();
0 ignored issues
show
Unused Code introduced by
The assignment to $displayName is dead and can be removed.
Loading history...
52
53
				$circles[] = new User(User::TYPE_CIRCLE, $circle->getUniqueId());
54
			}
55
		}
56
		return $circles;
57
	}
58
59
60
	/**
61
	 * Get circle details
62
	 * @NoAdminRequired
63
	 * @param string $circleId
64
	 * @return String
65
	 */
66
	public function getDisplayName($circleId = 0) {
67
		if (!$this->enabled) {
68
			return 'NixCircle';
69
		}
70
		return \OCA\Circles\Api\v1\Circles::detailsCircle($circleId)->getName();
71
	}
72
73
	/**
74
	 * Get circle details
75
	 * @NoAdminRequired
76
	 * @param string $circleId
77
	 * @return Array
78
	 */
79
	public function getDetails($circleId = 0) {
80
		if (!$this->enabled) {
81
			return 'Unknown circle';
82
		}
83
		return \OCA\Circles\Api\v1\Circles::detailsCircle($circleId);
84
	}
85
86
	/**
87
	 * Get a list of contacts
88
	 * @NoAdminRequired
89
	 * @param string $query
90
	 * @return User[]
91
	 */
92
	public function getCircleMembers($circleId) {
93
		if (!$this->enabled) {
94
			return [];
95
		}
96
		$members = [];
97
		foreach (\OCA\Circles\Api\v1\Circles::detailsCircle($circleId)->getMembers() as $circleMember) {
98
			if ($circleMember->getType() === Circles::TYPE_USER) {
99
				$members[] = new User(User::TYPE_USER, $circleMember->getUserId());
100
			} elseif ($circleMember->getType() === Circles::TYPE_GROUP) {
101
				$members[] = new User(User::TYPE_GROUP, $circleMember->getUserId());
102
			} elseif ($circleMember->getType() === Circles::TYPE_MAIL) {
103
				$members[] = new User(User::TYPE_EMAIL, $circleMember->getUserId());
104
			} elseif ($circleMember->getType() === Circles::TYPE_CONTACT) {
105
				$members[] = new User(User::TYPE_CONTACT, $circleMember->getUserId());
106
			}
107
		}
108
		return $members;
109
	}
110
111
}
112