Completed
Pull Request — master (#1128)
by René
04:27
created

Contact::__construct()   B

Complexity

Conditions 10
Paths 82

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 49
rs 7.6666
cc 10
nc 82
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Model;
25
26
use OCA\Polls\Exceptions\MultipleContactsFound;
27
use OCA\Polls\Exceptions\ContactsNotEnabled;
28
29
class Contact extends UserGroupClass {
30
	public const TYPE = 'contact';
31
	public const ICON = 'icon-mail';
32
33
	/** @var Array */
34
	private $contact;
35
36
	/**
37
	 * User constructor.
38
	 * @param $id
39
	 * @param $displayName
40
	 */
41
	public function __construct(
42
		$id,
43
		$displayName = ''
44
	) {
45
		parent::__construct($id, self::TYPE, $displayName);
46
		$this->icon = self::ICON;
47
48
		if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
49
			$this->contact = [];
50
51
			$parts = explode(":", $this->id);
52
			$this->id = end($parts);
53
54
			// Search for UID and FN
55
			// Before this implementation contacts where stored with their FN property
56
			// From now on, the contact's UID is used as identifier
57
			// TODO: Remove FN as search range for loading a contact in a polls version later than 1.6
58
			$contacts = \OC::$server->getContactsManager()->search($this->id, ['UID', 'FN']);
59
60
			if (count($contacts) === 1) {
61
				$this->contact = $contacts[0];
62
				$this->id = $this->contact['UID'];
63
				$this->displayName = isset($this->contact['FN']) ? $this->contact['FN'] : $this->displayName;
64
				$this->emailAddress = isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : $this->emailAddress;
65
			} elseif (count($contacts) > 1) {
66
				throw new MultipleContactsFound('Multiple contacts found for id ' . $this->id);
67
			}
68
69
			$this->organisation = isset($this->contact['ORG']) ? $this->contact['ORG'] : '';
70
71
			if (isset($this->contact['CATEGORIES'])) {
72
				$this->categories = explode(',', $this->contact['CATEGORIES']);
73
			} else {
74
				$this->categories = [];
75
			}
76
77
			$description = $this->categories;
78
79
			if (isset($this->contact['ORG'])) {
80
				array_unshift($description, $this->organisation);
81
			}
82
83
			if (count($description) > 0) {
84
				$this->description = implode(", ", $description);
85
			} else {
86
				$this->description = \OC::$server->getL10N('polls')->t('Contact');
87
			}
88
		} else {
89
			throw new ContactsNotEnabled();
90
		}
91
	}
92
93
	/**
94
	 * isEnabled
95
	 * @NoAdminRequired
96
	 * @return Boolean
97
	 */
98
	public static function isEnabled() {
99
		return \OC::$server->getAppManager()->isEnabledForUser('contacts');
100
	}
101
102
	/**
103
	 * listRaw
104
	 * @NoAdminRequired
105
	 * @param string $query
106
	 * @return Array
107
	 */
108
	public static function listRaw($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
109
		$contacts = [];
110
		if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
111
			foreach (\OC::$server->getContactsManager()->search($query, $queryRange) as $contact) {
112
				if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
113
					$contacts[] = $contact;
114
				}
115
			}
116
		}
117
		return $contacts;
118
	}
119
120
	/**
121
	 * list
122
	 * @NoAdminRequired
123
	 * @param string $query
124
	 * @param array $queryRange
125
	 * @return self[]
126
	 */
127
	public static function search($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
128
		$contacts = [];
129
		foreach (self::listRaw($query, $queryRange) as $contact) {
130
			$contacts[] = new Self($contact['UID']);
131
		}
132
		return $contacts;
133
	}
134
}
135