Completed
Push — federated-circles ( 915d64...66c054 )
by Maxence
03:31
created

Circle::setOwnerMemberFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Model;
28
29
use OCA\Circles\Exceptions\CircleTypeNotValid;
30
31
class Circle extends BaseCircle implements \JsonSerializable {
32
33
	const CIRCLES_PERSONAL = 1;
34
	const CIRCLES_HIDDEN = 2;
35
	const CIRCLES_PRIVATE = 4;
36
	const CIRCLES_PUBLIC = 8;
37
38
	const CIRCLES_ALL = 15;
39
40
	public function getTypeString() {
41
		switch ($this->getType()) {
42
			case self::CIRCLES_PERSONAL:
43
				return 'Personal';
44
			case self::CIRCLES_HIDDEN:
45
				return 'Hidden';
46
			case self::CIRCLES_PRIVATE:
47
				return 'Private';
48
			case self::CIRCLES_PUBLIC:
49
				return 'Public';
50
			case self::CIRCLES_ALL:
51
				return 'All';
52
		}
53
54
		return 'none';
55
	}
56
57
	public function getTypeLongString() {
58
		return self::typeLongString($this->getType());
59
	}
60
61
62
	public function getInfo() {
63
		return $this->getTypeLongString();
64
	}
65
66
67
	public function jsonSerialize() {
68
		return array(
69
			'id'             => $this->getId(),
70
			'name'           => $this->getName(),
71
			'owner'          => $this->getOwner(),
72
			'user'           => $this->getUser(),
73
			'description'    => $this->getDescription(),
74
			'type'           => $this->getTypeString(),
75
			'creation'       => $this->getCreation(),
76
			'typeString'     => $this->getTypeString(),
77
			'typeLongString' => $this->getTypeLongString(),
78
			'members'        => $this->getMembers(),
79
			'links'          => $this->getRemote()
80
		);
81
	}
82
83
	/**
84
	 * set all infos from an Array.
85
	 *
86
	 * @param $arr
87
	 *
88
	 * @return $this
89
	 */
90
	public function fromArray($arr) {
91
		$this->setId($arr['id']);
92
		$this->setName($arr['name']);
93
		$this->setDescription($arr['description']);
94
		$this->setType($arr['type']);
95
		$this->setCreation($arr['creation']);
96
97
		$this->setOwnerMemberFromArray($arr);
98
		$this->setUserMemberFromArray($arr);
99
100
		return $this;
101
	}
102
103
104
	/**
105
	 * set Owner Infos from Array
106
	 *
107
	 * @param $array
108
	 */
109
	private function setOwnerMemberFromArray(& $array) {
110
		if (key_exists('owner', $array)) {
111
			$owner = new Member($this->l10n);
112
			$owner->setUserId($array['owner']);
113
			$this->setOwner($owner);
114
		}
115
	}
116
117
118
	/**
119
	 * set User Infos from Array
120
	 *
121
	 * @param $array
122
	 */
123
	private function setUserMemberFromArray(& $array) {
124
		if (key_exists('status', $array)
125
			&& key_exists('level', $array)
126
			&& key_exists('joined', $array)
127
		) {
128
			$user = new Member($this->l10n);
129
			$user->setStatus($array['status']);
130
			$user->setLevel($array['level']);
131
			$user->setJoined($array['joined']);
132
			$this->setUser($user);
133
		}
134
	}
135
136
137
	/**
138
	 * @throws CircleTypeNotValid
139
	 */
140
	public function cantBePersonal() {
141
		if ($this->getType() === self::CIRCLES_PERSONAL) {
142
			throw new CircleTypeNotValid(
143
				$this->l10n->t("This option is not available to personal circles")
144
			);
145
		}
146
	}
147
148
149
	/**
150
	 * @param $type
151
	 *
152
	 * @return string
153
	 */
154
	public static function typeLongString($type) {
155
		switch ($type) {
156
			case self::CIRCLES_PERSONAL:
157
				return 'Personal circle';
158
			case self::CIRCLES_HIDDEN:
159
				return 'Hidden circle';
160
			case self::CIRCLES_PRIVATE:
161
				return 'Private circle';
162
			case self::CIRCLES_PUBLIC:
163
				return 'Public circle';
164
			case self::CIRCLES_ALL:
165
				return 'All circles';
166
		}
167
168
		return 'none';
169
	}
170
171
}
172
173
174