Completed
Pull Request — master (#551)
by Maxence
03:11
created

FederatedUser   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 5
dl 0
loc 204
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A set() 0 7 1
A setSingleId() 0 5 1
A getSingleId() 0 3 1
A setUserId() 0 5 1
A getUserId() 0 3 1
A setUserType() 0 5 1
A getUserType() 0 3 1
A setInstance() 0 9 2
A getInstance() 0 3 1
A setMemberships() 0 5 1
A getMemberships() 0 7 2
A import() 0 13 2
A jsonSerialize() 0 9 1
A importFromDatabase() 0 8 1
A compareWith() 0 6 4
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Model;
33
34
use daita\MySmallPhpTools\Db\Nextcloud\nc21\INC21QueryRow;
35
use daita\MySmallPhpTools\Exceptions\InvalidItemException;
36
use daita\MySmallPhpTools\IDeserializable;
37
use daita\MySmallPhpTools\Traits\TArrayTools;
38
use JsonSerializable;
39
use OCA\Circles\IFederatedUser;
40
41
42
/**
43
 * Class FederatedUser
44
 *
45
 * @package OCA\Circles\Model
46
 */
47
class FederatedUser extends ManagedModel implements IFederatedUser, IDeserializable, INC21QueryRow, JsonSerializable {
48
49
50
	use TArrayTools;
51
52
53
	/** @var string */
54
	private $singleId = '';
55
56
	/** @var string */
57
	private $userId;
58
59
	/** @var int */
60
	private $userType;
61
62
	/** @var string */
63
	private $instance;
64
65
	/** @var Membership[] */
66
	private $memberships = [];
67
68
69
	/**
70
	 * FederatedUser constructor.
71
	 */
72
	public function __construct() {
73
	}
74
75
76
	public function set(string $userId = '', $instance = '', int $type = Member::TYPE_USER): self {
77
		$this->userId = $userId;
78
		$this->setInstance($instance);
79
		$this->userType = $type;
80
81
		return $this;
82
	}
83
84
	/**
85
	 * @param string $singleId
86
	 *
87
	 * @return self
88
	 */
89
	public function setSingleId(string $singleId): self {
90
		$this->singleId = $singleId;
91
92
		return $this;
93
	}
94
95
	/**
96
	 * @return string
97
	 */
98
	public function getSingleId(): string {
99
		return $this->singleId;
100
	}
101
102
103
	/**
104
	 * @param string $userId
105
	 *
106
	 * @return self
107
	 */
108
	public function setUserId(string $userId): self {
109
		$this->userId = $userId;
110
111
		return $this;
112
	}
113
114
	/**
115
	 * @return string
116
	 */
117
	public function getUserId(): string {
118
		return $this->userId;
119
	}
120
121
122
	/**
123
	 * @param int $userType
124
	 *
125
	 * @return self
126
	 */
127
	public function setUserType(int $userType): self {
128
		$this->userType = $userType;
129
130
		return $this;
131
	}
132
133
	/**
134
	 * @return int
135
	 */
136
	public function getUserType(): int {
137
		return $this->userType;
138
	}
139
140
141
	/**
142
	 * @param string $instance
143
	 *
144
	 * @return self
145
	 */
146
	public function setInstance(string $instance): self {
147
		if ($instance === '') {
148
			$instance = $this->getManager()->getLocalInstance();
149
		}
150
151
		$this->instance = $instance;
152
153
		return $this;
154
	}
155
156
	/**
157
	 * @return string
158
	 */
159
	public function getInstance(): string {
160
		return $this->instance;
161
	}
162
163
164
	/**
165
	 * @param Membership[] $memberships
166
	 *
167
	 * @return self
168
	 */
169
	public function setMemberships(array $memberships): self {
170
		$this->memberships = $memberships;
171
172
		return $this;
173
	}
174
175
	/**
176
	 * @return array
177
	 */
178
	public function getMemberships(): array {
179
		if ($this->memberships === null) {
180
			$this->getManager()->getMemberships($this);
181
		}
182
183
		return $this->memberships;
184
	}
185
186
187
	/**
188
	 * @param array $data
189
	 *
190
	 * @return $this
191
	 * @throws InvalidItemException
192
	 */
193
	public function import(array $data): IDeserializable {
194
		if ($this->get('user_id', $data) === '') {
195
			throw new InvalidItemException();
196
		}
197
198
		$this->setSingleId($this->get('id', $data));
199
		$this->setUserId($this->get('user_id', $data));
200
		$this->setUserType($this->getInt('user_type', $data));
201
		$this->setInstance($this->get('instance', $data));
202
203
//$this->setMemberships($this->getArray('memberships'));
204
		return $this;
205
	}
206
207
208
	/**
209
	 * @return string[]
210
	 */
211
	public function jsonSerialize(): array {
212
		return [
213
			'id'        => $this->getSingleId(),
214
			'user_id'   => $this->getUserId(),
215
			'user_type' => $this->getUserType(),
216
			'instance'  => $this->getInstance(),
217
			//			'memberships' => $this->getMemberships()
218
		];
219
	}
220
221
222
	/**
223
	 * @param array $data
224
	 * @param string $prefix
225
	 *
226
	 * @return INC21QueryRow
227
	 */
228
	public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow {
229
		$this->setSingleId($this->get($prefix . 'member_id', $data));
230
		$this->setUserId($this->get($prefix . 'user_id', $data));
231
		$this->setUserType($this->getInt($prefix . 'user_type', $data));
232
		$this->setInstance($this->get($prefix . 'instance', $data));
233
234
		return $this;
235
	}
236
237
238
	/**
239
	 * @param IFederatedUser $member
240
	 *
241
	 * @return bool
242
	 */
243
	public function compareWith(IFederatedUser $member): bool {
244
		return !($this->getSingleId() !== $member->getSingleId()
245
				 || $this->getUserId() !== $member->getUserId()
246
				 || $this->getUserType() <> $member->getUserType()
247
				 || $this->getInstance() !== $member->getInstance());
248
	}
249
250
}
251
252