Completed
Pull Request — master (#544)
by Maxence
02:26
created

AppService::setUidFromKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Model;
31
32
33
use daita\MySmallPhpTools\Db\Nextcloud\nc21\INC21QueryRow;
34
use daita\MySmallPhpTools\Model\Nextcloud\nc21\NC21Signatory;
35
use daita\MySmallPhpTools\Traits\TArrayTools;
36
use JsonSerializable;
37
use OCA\Circles\Exceptions\RemoteUidException;
38
39
40
/**
41
 * Class AppService
42
 *
43
 * @package OCA\Circles\Model
44
 */
45
class AppService extends NC21Signatory implements INC21QueryRow, JsonSerializable {
46
47
48
	use TArrayTools;
49
50
51
	/** @var string */
52
	private $test = '';
53
54
	/** @var string */
55
	private $incoming = '';
56
57
	/** @var string */
58
	private $circles = '';
59
60
	/** @var string */
61
	private $members = '';
62
63
	/** @var string */
64
	private $uid = '';
65
66
	/** @var string */
67
	private $authSigned = '';
68
69
	/** @var bool */
70
	private $identityAuthed = false;
71
72
73
	/**
74
	 * @param string $test
75
	 *
76
	 * @return AppService
77
	 */
78
	public function setTest(string $test): self {
79
		$this->test = $test;
80
81
		return $this;
82
	}
83
84
	/**
85
	 * @return string
86
	 */
87
	public function getTest(): string {
88
		return $this->test;
89
	}
90
91
92
	/**
93
	 * @return string
94
	 */
95
	public function getIncoming(): string {
96
		return $this->incoming;
97
	}
98
99
	/**
100
	 * @param string $incoming
101
	 *
102
	 * @return self
103
	 */
104
	public function setIncoming(string $incoming): self {
105
		$this->incoming = $incoming;
106
107
		return $this;
108
	}
109
110
111
	/**
112
	 * @return string
113
	 */
114
	public function getCircles(): string {
115
		return $this->circles;
116
	}
117
118
	/**
119
	 * @param string $circles
120
	 *
121
	 * @return self
122
	 */
123
	public function setCircles(string $circles): self {
124
		$this->circles = $circles;
125
126
		return $this;
127
	}
128
129
130
	/**
131
	 * @return string
132
	 */
133
	public function getMembers(): string {
134
		return $this->members;
135
	}
136
137
	/**
138
	 * @param string $members
139
	 *
140
	 * @return self
141
	 */
142
	public function setMembers(string $members): self {
143
		$this->members = $members;
144
145
		return $this;
146
	}
147
148
149
	/**
150
	 * @return $this
151
	 */
152
	public function setUidFromKey(): self {
153
		$this->setUid(hash('sha512', $this->getPublicKey()));
154
155
		return $this;
156
	}
157
158
	/**
159
	 * @param string $uid
160
	 *
161
	 * @return AppService
162
	 */
163
	public function setUid(string $uid): self {
164
		$this->uid = $uid;
165
166
		return $this;
167
	}
168
169
	/**
170
	 * @param bool $shorten
171
	 *
172
	 * @return string
173
	 */
174
	public function getUid(bool $shorten = false): string {
175
		if ($shorten) {
176
			return substr($this->uid, 0, 18);
177
		}
178
179
		return $this->uid;
180
	}
181
182
183
	/**
184
	 * @param string $authSigned
185
	 *
186
	 * @return AppService
187
	 */
188
	public function setAuthSigned(string $authSigned): self {
189
		$this->authSigned = $authSigned;
190
191
		return $this;
192
	}
193
194
	/**
195
	 * @return string
196
	 */
197
	public function getAuthSigned(): string {
198
		return $this->authSigned;
199
	}
200
201
202
	/**
203
	 * @param bool $identityAuthed
204
	 *
205
	 * @return AppService
206
	 */
207
	public function setIdentityAuthed(bool $identityAuthed): self {
208
		$this->identityAuthed = $identityAuthed;
209
210
		return $this;
211
	}
212
213
	/**
214
	 * @return bool
215
	 */
216
	public function isIdentityAuthed(): bool {
217
		return $this->identityAuthed;
218
	}
219
220
	/**
221
	 * @throws RemoteUidException
222
	 */
223
	public function mustBeIdentityAuthed(): void {
224
		if (!$this->isIdentityAuthed()) {
225
			throw new RemoteUidException('identity not authed');
226
		}
227
	}
228
229
230
	/**
231
	 * @param array $data
232
	 *
233
	 * @return NC21Signatory
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use AppService.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
234
	 */
235
	public function import(array $data): NC21Signatory {
236
		parent::import($data);
237
238
		$this->setTest($this->get('test', $data));
239
		$this->setIncoming($this->get('incoming', $data));
240
		$this->setCircles($this->get('circles', $data));
241
		$this->setMembers($this->get('members', $data));
242
		$this->setAuthSigned($this->get('auth-signed', $data));
243
		$this->setUid($this->get('uid', $data));
244
245
		return $this;
246
	}
247
248
249
	/**
250
	 * @return array
251
	 */
252
	public function jsonSerialize(): array {
253
		$data = [
254
			'uid'      => $this->getUid(true),
255
			'test'     => $this->getTest(),
256
			'incoming' => $this->getIncoming(),
257
			'circles'  => $this->getCircles(),
258
			'members'  => $this->getMembers(),
259
		];
260
261
		if ($this->getAuthSigned() !== '') {
262
			$data['auth-signed'] = $this->getAlgorithm() . ':' . $this->getAuthSigned();
263
		}
264
265
		return array_filter(array_merge($data, parent::jsonSerialize()));
266
	}
267
268
269
	/**
270
	 * @param array $data
271
	 *
272
	 * @return self
273
	 */
274
	public function importFromDatabase(array $data): INC21QueryRow {
275
		$this->import($this->getArray('item', $data));
276
		$this->setInstance($this->get('instance', $data));
277
		$this->setId($this->get('href', $data));
278
279
		return $this;
280
	}
281
282
}
283
284