ContactGroup   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 28
c 0
b 0
f 0
dl 0
loc 67
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isEnabled() 0 2 1
A getDisplayName() 0 5 2
B search() 0 18 7
A __construct() 0 9 2
A getMembers() 0 10 3
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\App\IAppManager;
28
use OCP\Contacts\IManager as IContactsManager;
29
use OCA\Polls\Exceptions\ContactsNotEnabledExceptions;
30
31
class ContactGroup extends UserGroupClass {
32
	public const TYPE = 'contactGroup';
33
	public const ICON = 'icon-group';
34
35
	public function __construct(
36
		string $id
37
	) {
38
		parent::__construct($id, self::TYPE);
39
		if (self::isEnabled()) {
40
			$this->icon = self::ICON;
41
			$this->description = \OC::$server->getL10N('polls')->t('Contact group');
42
		} else {
43
			throw new ContactsNotEnabledExceptions();
44
		}
45
	}
46
47
	/**
48
	 * @return string
49
	 */
50
	public function getDisplayName(): string {
51
		if (!$this->displayName) {
52
			return $this->id;
53
		}
54
		return $this->displayName;
55
	}
56
57
	/**
58
	 * Get a list of contacts group members
59
	 * @return Contact[]
60
	 */
61
	public function getMembers(): array {
62
		$contacts = [];
63
64
		foreach (self::getContainer()->query(IContactsManager::class)->search($this->id, ['CATEGORIES']) as $contact) {
65
			if (array_key_exists('EMAIL', $contact)) {
66
				$contacts[] = new Contact($contact['UID']);
67
			}
68
		}
69
70
		return $contacts;
71
	}
72
73
	public static function isEnabled(): bool {
74
		return self::getContainer()->query(IAppManager::class)->isEnabledForUser('contacts');
75
	}
76
77
	/**
78
	 * @return ContactGroup[]
79
	 */
80
	public static function search(string $query = ''): array {
81
		$contactGroups = [];
82
		$categories = [];
83
		if (self::isEnabled() && $query) {
84
			foreach (self::getContainer()->query(IContactsManager::class)->search($query, ['CATEGORIES']) as $contact) {
85
				// get all groups from the found contact and explode to array
86
87
				foreach (explode(',', $contact['CATEGORIES']) as $category) {
88
					if (stripos($category, $query) === 0) {
89
						$categories[] = $category;
90
					}
91
				}
92
			}
93
			foreach (array_unique($categories) as $contactGroup) {
94
				$contactGroups[] = new self($contactGroup);
95
			}
96
		}
97
		return $contactGroups;
98
	}
99
}
100