Completed
Push — activities ( eafd5e...7ed318 )
by Maxence
03:19
created

BaseMember   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 193
Duplicated Lines 5.7 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 32
lcom 1
cbo 1
dl 11
loc 193
rs 9.6
c 1
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A setCircleId() 0 5 1
A getCircleId() 0 3 1
A setUserId() 0 5 1
A getUserId() 0 3 1
A setLevel() 0 5 1
A getLevel() 0 3 1
A setNote() 0 5 1
A getNote() 0 3 1
A setStatus() 0 9 2
A getStatus() 0 3 1
A setJoined() 0 5 1
A getJoined() 0 3 1
A isLevel() 0 3 1
A isAlmostMember() 0 4 2
A setAsAMember() 0 4 1
A fromArray2() 0 20 4
A fromJSON() 0 3 1
B getLevelString() 0 16 6
A jsonSerialize() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 OC\L10N\L10N;
30
31
class BaseMember implements \JsonSerializable {
32
33
	const LEVEL_NONE = 0;
34
	const LEVEL_MEMBER = 1;
35
	const LEVEL_MODERATOR = 4;
36
	const LEVEL_ADMIN = 8;
37
	const LEVEL_OWNER = 9;
38
39
	const STATUS_NONMEMBER = 'Unknown';
40
	const STATUS_INVITED = 'Invited';
41
	const STATUS_REQUEST = 'Requesting';
42
	const STATUS_MEMBER = 'Member';
43
	const STATUS_BLOCKED = 'Blocked';
44
	const STATUS_KICKED = 'Kicked';
45
46
	/** @var int */
47
	private $circleId;
48
49
50
	/** @var L10N */
51
	protected $l10n;
52
53
	/** @var string */
54
	private $userId;
55
56
	/** @var int */
57
	private $level;
58
59
	/** @var string */
60
	private $status;
61
62
	/** @var string */
63
	private $note;
64
65
	/** @var string */
66
	private $joined;
67
68
	public function __construct($l10n, $userId = '', $circleId = -1) {
69
		$this->l10n = $l10n;
70
71
		if ($userId !== '') {
72
			$this->setUserId($userId);
73
		}
74
		if ($circleId > -1) {
75
			$this->setCircleId($circleId);
76
		}
77
		$this->setLevel(Member::LEVEL_NONE);
78
		$this->setStatus(Member::STATUS_NONMEMBER);
79
	}
80
81
82
	public function setCircleId($circleId) {
83
		$this->circleId = (int)$circleId;
84
85
		return $this;
86
	}
87
88
	public function getCircleId() {
89
		return $this->circleId;
90
	}
91
92
93
	public function setUserId($userId) {
94
		$this->userId = $userId;
95
96
		return $this;
97
	}
98
99
	public function getUserId() {
100
		return $this->userId;
101
	}
102
103
104
	public function setLevel($level) {
105
		$this->level = (int)$level;
106
107
		return $this;
108
	}
109
110
	public function getLevel() {
111
		return $this->level;
112
	}
113
114
115
	public function setNote($note) {
116
		$this->note = $note;
117
118
		return $this;
119
	}
120
121
	public function getNote() {
122
		return $this->note;
123
	}
124
125
126
	public function setStatus($status) {
127
		if (is_null($status)) {
128
			$this->status = self::STATUS_NONMEMBER;
129
		} else {
130
			$this->status = $status;
131
		}
132
133
		return $this;
134
	}
135
136
	public function getStatus() {
137
		return $this->status;
138
	}
139
140
141
	public function setJoined($joined) {
142
		$this->joined = $joined;
143
144
		return $this;
145
	}
146
147
	public function getJoined() {
148
		return $this->joined;
149
	}
150
151
152
	public function isLevel($level) {
153
		return ($this->getLevel() >= $level);
154
	}
155
156
157
	public function isAlmostMember() {
158
		return ($this->getStatus() === Member::STATUS_INVITED
159
				|| $this->getStatus() === Member::STATUS_REQUEST);
160
	}
161
162
163
	protected function setAsAMember($level = 1) {
164
		$this->setStatus(Member::STATUS_MEMBER);
165
		$this->setLevel($level);
166
	}
167
168
169
	public static function fromArray2($l10n, $arr) {
170
		if ($arr === null)
171
			return null;
172
173
		$member = new Member($l10n);
174
175
		$member->setCircleId($arr['circle_id']);
176
		$member->setUserId($arr['user_id']);
177
		$member->setLevel($arr['level']);
178
		$member->setStatus($arr['status']);
179
		if (key_exists('note', $arr)) {
180
			$member->setNote($arr['note']);
181
		}
182
183
		if (key_exists('joined', $arr)) {
184
			$member->setJoined($arr['joined']);
185
		}
186
187
		return $member;
188
	}
189
190
191
	public static function fromJSON($l10n, $json) {
192
		return self::fromArray2($l10n, json_decode($json, true));
193
	}
194
195 View Code Duplication
	public function jsonSerialize() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
		return array(
197
			'circle_id'    => $this->getCircleId(),
198
			'user_id'      => $this->getUserId(),
199
			'level'        => $this->getLevel(),
200
			'level_string' => $this->getLevelString(),
201
			'status'       => $this->getStatus(),
202
			'note'         => $this->getNote(),
203
			'joined'       => $this->getJoined()
204
		);
205
	}
206
207
	public function getLevelString() {
208
		switch ($this->getLevel()) {
209
			case self::LEVEL_NONE:
210
				return 'Not a member';
211
			case self::LEVEL_MEMBER:
212
				return 'Member';
213
			case self::LEVEL_MODERATOR:
214
				return 'Moderator';
215
			case self::LEVEL_ADMIN:
216
				return 'Admin';
217
			case self::LEVEL_OWNER:
218
				return 'Owner';
219
		}
220
221
		return 'none';
222
	}
223
}
224