Completed
Pull Request — master (#3233)
by Jan-Christoph
12:25
created

Entry::addEMailAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @copyright 2017 Christoph Wurst <[email protected]>
5
 *
6
 * @author 2017 Christoph Wurst <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\Contacts\ContactsMenu;
26
27
use OCP\Contacts\ContactsMenu\IAction;
28
use OCP\Contacts\ContactsMenu\IEntry;
29
30
class Entry implements IEntry {
31
32
	/** @var string|int|null */
33
	private $id = null;
34
35
	/** @var string */
36
	private $fullName = '';
37
38
	/** @var string[] */
39
	private $emailAddresses = [];
40
41
	/** @var string|null */
42
	private $avatar;
43
44
	/** @var IAction[] */
45
	private $actions = [];
46
47
	/** @var array */
48
	private $properties = [];
49
50
	/**
51
	 * @param string $id
52
	 */
53
	public function setId($id) {
54
		$this->id = $id;
55
	}
56
57
	/**
58
	 * @param string $displayName
59
	 */
60
	public function setFullName($displayName) {
61
		$this->fullName = $displayName;
62
	}
63
64
	/**
65
	 * @return string
66
	 */
67
	public function getFullName() {
68
		return $this->fullName;
69
	}
70
71
	/**
72
	 * @param string $address
73
	 */
74
	public function addEMailAddress($address) {
75
		$this->emailAddresses[] = $address;
76
	}
77
78
	/**
79
	 * @return string
80
	 */
81
	public function getEMailAddresses() {
82
		return $this->emailAddresses;
83
	}
84
85
	/**
86
	 * @param string $avatar
87
	 */
88
	public function setAvatar($avatar) {
89
		$this->avatar = $avatar;
90
	}
91
92
	/**
93
	 * @return string
94
	 */
95
	public function getAvatar() {
96
		return $this->avatar;
97
	}
98
99
	/**
100
	 * @param IAction $action
101
	 */
102
	public function addAction(IAction $action) {
103
		$this->actions[] = $action;
104
		$this->sortActions();
105
	}
106
107
	/**
108
	 * @return IAction[]
109
	 */
110
	public function getActions() {
111
		return $this->actions;
112
	}
113
114
	/**
115
	 * sort the actions by priority and name
116
	 */
117
	private function sortActions() {
118
		usort($this->actions, function(IAction $action1, IAction $action2) {
119
			$prio1 = $action1->getPriority();
120
			$prio2 = $action2->getPriority();
121
122
			if ($prio1 === $prio2) {
123
				// Ascending order for same priority
124
				return strcasecmp($action1->getName(), $action2->getName());
125
			}
126
127
			// Descending order when priority differs
128
			return $prio2 - $prio1;
129
		});
130
	}
131
132
	/**
133
	 * @param array $contact key-value array containing additional properties
134
	 */
135
	public function setProperties(array $contact) {
136
		$this->properties = $contact;
137
	}
138
139
	/**
140
	 * @param string $key
141
	 * @return mixed
142
	 */
143
	public function getProperty($key) {
144
		if (!isset($this->properties[$key])) {
145
			return null;
146
		}
147
		return $this->properties[$key];
148
	}
149
150
	/**
151
	 * @return array
152
	 */
153
	public function jsonSerialize() {
154
		$topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null;
155
		$otherActions = array_map(function(IAction $action) {
156
			return $action->jsonSerialize();
157
		}, array_slice($this->actions, 1));
158
159
		return [
160
			'id' => $this->id,
161
			'fullName' => $this->fullName,
162
			'avatar' => $this->getAvatar(),
163
			'topAction' => $topAction,
164
			'actions' => $otherActions,
165
			'lastMessage' => '',
166
		];
167
	}
168
169
}
170