Passed
Pull Request — master (#1128)
by René
08:09 queued 03:52
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
	/**
34
	 * User constructor.
35
	 * @param $id
36
	 * @param $displayName
37
	 */
38
	public function __construct(
39
		$id,
40
		$displayName = ''
41
	) {
42
		parent::__construct($id, self::TYPE, $displayName);
43
		$this->icon = self::ICON;
44
45
		if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
46
			$this->contact = [];
0 ignored issues
show
Bug Best Practice introduced by
The property contact does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
48
			$parts = explode(":", $this->id);
49
			$this->id = end($parts);
50
51
			// Search for UID and FN
52
			// Before this implementation contacts where stored with their FN property
53
			// From now on, the contact's UID is used as identifier
54
			// TODO: Remove FN as search range for loading a contact in a polls version later than 1.6
55
			$contacts = \OC::$server->getContactsManager()->search($this->id, ['UID', 'FN']);
56
57
			if (count($contacts) === 1) {
58
				$this->contact = $contacts[0];
59
				$this->id = $this->contact['UID'];
60
				$this->displayName = isset($this->contact['FN']) ? $this->contact['FN'] : $this->displayName;
61
				$this->emailAddress = isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : $this->emailAddress;
62
			} elseif (count($contacts) > 1) {
63
				throw new MultipleContactsFound('Multiple contacts found for id ' . $this->id);
64
			}
65
66
			$this->organisation = isset($this->contact['ORG']) ? $this->contact['ORG'] : '';
67
68
			if (isset($this->contact['CATEGORIES'])) {
69
				$this->categories = explode(',', $this->contact['CATEGORIES']);
0 ignored issues
show
Documentation Bug introduced by
It seems like explode(',', $this->contact['CATEGORIES']) of type string[] is incompatible with the declared type string of property $categories.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
			} else {
71
				$this->categories = [];
72
			}
73
74
			$description = $this->categories;
75
76
			if (isset($this->contact['ORG'])) {
77
				array_unshift($description, $this->organisation);
78
			}
79
80
			if (count($description) > 0) {
81
				$this->description = implode(", ", $description);
82
			} else {
83
				$this->description = \OC::$server->getL10N('polls')->t('Contact');
84
			}
85
		} else {
86
			throw new ContactsNotEnabled();
87
		}
88
	}
89
90
	/**
91
	 * isEnabled
92
	 * @NoAdminRequired
93
	 * @return Boolean
94
	 */
95
	public static function isEnabled() {
96
		return \OC::$server->getAppManager()->isEnabledForUser('contacts');
97
	}
98
99
	/**
100
	 * listRaw
101
	 * @NoAdminRequired
102
	 * @param string $query
103
	 * @return Array
104
	 */
105
	public static function listRaw($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
106
		$contacts = [];
107
		if (\OC::$server->getAppManager()->isEnabledForUser('contacts')) {
108
			foreach (\OC::$server->getContactsManager()->search($query, $queryRange) as $contact) {
109
				if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
110
					$contacts[] = $contact;
111
				}
112
			}
113
		}
114
		return $contacts;
115
	}
116
117
	/**
118
	 * list
119
	 * @NoAdminRequired
120
	 * @param string $query
121
	 * @param array $queryRange
122
	 * @return self[]
123
	 */
124
	public static function search($query = '', $queryRange = ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) {
125
		$contacts = [];
126
		foreach (self::listRaw($query, $queryRange) as $contact) {
127
			$contacts[] = new Self($contact['UID']);
128
		}
129
		return $contacts;
130
	}
131
}
132