Passed
Pull Request — master (#1128)
by René
04:35
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
26
use OCA\Polls\Model\User;
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
class CirclesService {
30
	public function __construct(
31
	) {
32
		$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...
33
	}
34
35
	/**
36
	 * Get a list of avaiable circles
37
	 * @NoAdminRequired
38
	 * @param string $query
39
	 * @param array $skip - circle names to skip in return array
40
	 * @return Array
41
	 */
42
	public function getCircles($query = '', $skip = []) {
43
		if (!$this->enabled || $query === '') {
44
			return [];
45
		}
46
47
		$circles = [];
48
49
50
		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...
51
			if (!in_array($circle->getId(), $skip)) {
52
				$displayName = $circle->getName();
0 ignored issues
show
Unused Code introduced by
The assignment to $displayName is dead and can be removed.
Loading history...
53
54
				$circles[] = new User(User::TYPE_CIRCLE, $circle->getUniqueId());
55
			}
56
		}
57
		return $circles;
58
	}
59
60
61
	/**
62
	 * Get circle details
63
	 * @NoAdminRequired
64
	 * @param string $circleId
65
	 * @return String
66
	 */
67
	public function getDisplayName($circleId = 0) {
68
		if (!$this->enabled) {
69
			return 'NixCircle';
70
		}
71
		return \OCA\Circles\Api\v1\Circles::detailsCircle($circleId)->getName();
72
	}
73
74
	/**
75
	 * Get circle details
76
	 * @NoAdminRequired
77
	 * @param string $circleId
78
	 * @return Array
79
	 */
80
	public function getDetails($circleId = 0) {
81
		if (!$this->enabled) {
82
			return 'Unknown circle';
83
		}
84
		return \OCA\Circles\Api\v1\Circles::detailsCircle($circleId);
85
	}
86
87
	/**
88
	 * Get a list of contacts
89
	 * @NoAdminRequired
90
	 * @param string $query
91
	 * @return User[]
92
	 */
93
	public function getCircleMembers($circleId) {
94
		if (!$this->enabled) {
95
			return [];
96
		}
97
		$members = [];
98
		foreach (\OCA\Circles\Api\v1\Circles::detailsCircle($circleId)->getMembers() as $circleMember) {
99
			if ($circleMember->getType() === Circles::TYPE_USER) {
100
				$members[] = new User(User::TYPE_USER, $circleMember->getUserId());
101
			} elseif ($circleMember->getType() === Circles::TYPE_GROUP) {
102
				$members[] = new User(User::TYPE_GROUP, $circleMember->getUserId());
103
			} elseif ($circleMember->getType() === Circles::TYPE_MAIL) {
104
				$members[] = new User(User::TYPE_EMAIL, $circleMember->getUserId());
105
			} elseif ($circleMember->getType() === Circles::TYPE_CONTACT) {
106
				$members[] = new User(User::TYPE_CONTACT, $circleMember->getUserId());
107
			}
108
		}
109
		return $members;
110
	}
111
}
112