Completed
Push — master ( 3aeedd...be0cda )
by Maxence
03:35 queued 11s
created

Membership::getInheritanceDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\nc22\INC22QueryRow;
35
use daita\MySmallPhpTools\Exceptions\InvalidItemException;
36
use daita\MySmallPhpTools\IDeserializable;
37
use daita\MySmallPhpTools\Traits\TArrayTools;
38
use JsonSerializable;
39
use OCA\Circles\Exceptions\MembershipNotFoundException;
40
41
42
/**
43
 * Class Membership
44
 *
45
 * @package OCA\Circles\Model
46
 */
47
class Membership extends ManagedModel implements IDeserializable, INC22QueryRow, JsonSerializable {
48
49
50
	use TArrayTools;
51
52
53
	/** @var string */
54
	private $singleId = '';
55
56
	/** @var string */
57
	private $circleId = '';
58
59
	/** @var int */
60
	private $level = 0;
61
62
	/** @var string */
63
	private $inheritanceFirst = '';
64
65
	/** @var string */
66
	private $inheritanceLast = '';
67
68
	/** @var array */
69
	private $inheritancePath = [];
70
71
	/** @var int */
72
	private $inheritanceDepth = 0;
73
74
	/** @var array */
75
	private $inheritanceDetails = [];
76
77
78
	/**
79
	 * Membership constructor.
80
	 *
81
	 * @param string $singleId
82
	 * @param Member|null $member
83
	 * @param string $inheritanceLast
84
	 */
85
	public function __construct(
86
		string $singleId = '',
87
		string $inheritanceLast = '',
88
		?Member $member = null
89
	) {
90
		if (is_null($member)) {
91
			return;
92
		}
93
94
		$this->setSingleId($singleId);
95
		$this->setCircleId($member->getCircleId());
96
		$this->setInheritanceFirst($member->getSingleId());
97
		$this->setInheritanceLast($inheritanceLast === '' ? $member->getCircleId() : $inheritanceLast);
98
		$this->setLevel($member->getLevel());
99
	}
100
101
102
	/**
103
	 * @param string $singleId
104
	 *
105
	 * @return self
106
	 */
107
	public function setSingleId(string $singleId): self {
108
		$this->singleId = $singleId;
109
110
		return $this;
111
	}
112
113
	/**
114
	 * @return string
115
	 */
116
	public function getSingleId(): string {
117
		return $this->singleId;
118
	}
119
120
121
	/**
122
	 * @param string $circleId
123
	 *
124
	 * @return Membership
125
	 */
126
	public function setCircleId(string $circleId): self {
127
		$this->circleId = $circleId;
128
129
		return $this;
130
	}
131
132
	/**
133
	 * @return string
134
	 */
135
	public function getCircleId(): string {
136
		return $this->circleId;
137
	}
138
139
140
	/**
141
	 * @param int $level
142
	 *
143
	 * @return Membership
144
	 */
145
	public function setLevel(int $level): self {
146
		$this->level = $level;
147
148
		return $this;
149
	}
150
151
	/**
152
	 * @return int
153
	 */
154
	public function getLevel(): int {
155
		return $this->level;
156
	}
157
158
159
	/**
160
	 * @param string $inheritanceFirst
161
	 *
162
	 * @return Membership
163
	 */
164
	public function setInheritanceFirst(string $inheritanceFirst): self {
165
		$this->inheritanceFirst = $inheritanceFirst;
166
167
		return $this;
168
	}
169
170
	/**
171
	 * @return string
172
	 */
173
	public function getInheritanceFirst(): string {
174
		return $this->inheritanceFirst;
175
	}
176
177
178
	/**
179
	 * @param string $inheritanceLast
180
	 *
181
	 * @return Membership
182
	 */
183
	public function setInheritanceLast(string $inheritanceLast): self {
184
		$this->inheritanceLast = $inheritanceLast;
185
186
		return $this;
187
	}
188
189
	/**
190
	 * @return string
191
	 */
192
	public function getInheritanceLast(): string {
193
		return $this->inheritanceLast;
194
	}
195
196
197
	/**
198
	 * @param array $inheritancePath
199
	 *
200
	 * @return Membership
201
	 */
202
	public function setInheritancePath(array $inheritancePath): self {
203
		$this->inheritancePath = $inheritancePath;
204
205
		return $this;
206
	}
207
208
	/**
209
	 * @return array
210
	 */
211
	public function getInheritancePath(): array {
212
		return $this->inheritancePath;
213
	}
214
215
216
	/**
217
	 * @param int $inheritanceDepth
218
	 *
219
	 * @return Membership
220
	 */
221
	public function setInheritanceDepth(int $inheritanceDepth): self {
222
		$this->inheritanceDepth = $inheritanceDepth;
223
224
		return $this;
225
	}
226
227
	/**
228
	 * @return int
229
	 */
230
	public function getInheritanceDepth(): int {
231
		return $this->inheritanceDepth;
232
	}
233
234
235
	/**
236
	 * @param array $inheritanceDetails
237
	 *
238
	 * @return Membership
239
	 */
240
	public function setInheritanceDetails(array $inheritanceDetails): self {
241
		$this->inheritanceDetails = $inheritanceDetails;
242
243
		return $this;
244
	}
245
246
	/**
247
	 * @return array
248
	 */
249
	public function getInheritanceDetails(): array {
250
		return $this->inheritanceDetails;
251
	}
252
253
254
	/**
255
	 * @param array $data
256
	 *
257
	 * @return IDeserializable
258
	 * @throws InvalidItemException
259
	 */
260
	public function import(array $data): IDeserializable {
261
		if ($this->get('singleId', $data) === '') {
262
			throw new InvalidItemException();
263
		}
264
265
		$this->setSingleId($this->get('singleId', $data));
266
		$this->setCircleId($this->get('circleId', $data));
267
		$this->setLevel($this->getInt('level', $data));
268
		$this->setInheritanceFirst($this->get('inheritanceFirst', $data));
269
		$this->setInheritanceLast($this->get('inheritanceLast', $data));
270
		$this->setInheritancePath($this->getArray('inheritancePath', $data));
271
		$this->setInheritanceDepth($this->getInt('inheritanceDepth', $data));
272
273
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (OCA\Circles\Model\Membership) is incompatible with the return type declared by the interface daita\MySmallPhpTools\IDeserializable::import of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
274
	}
275
276
	/**
277
	 * @param array $data
278
	 * @param string $prefix
279
	 *
280
	 * @return INC22QueryRow
281
	 * @throws MembershipNotFoundException
282
	 */
283
	public function importFromDatabase(array $data, string $prefix = ''): INC22QueryRow {
284
		if ($this->get($prefix . 'single_id', $data) === '') {
285
			throw new MembershipNotFoundException();
286
		}
287
288
		$this->setSingleId($this->get($prefix . 'single_id', $data));
289
		$this->setCircleId($this->get($prefix . 'circle_id', $data));
290
		$this->setLevel($this->getInt($prefix . 'level', $data));
291
		$this->setInheritanceFirst($this->get($prefix . 'inheritance_first', $data));
292
		$this->setInheritanceLast($this->get($prefix . 'inheritance_last', $data));
293
		$this->setInheritancePath($this->getArray($prefix . 'inheritance_path', $data));
294
		$this->setInheritanceDepth($this->getInt($prefix . 'inheritance_depth', $data));
295
296
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (OCA\Circles\Model\Membership) is incompatible with the return type declared by the interface daita\MySmallPhpTools\Db...Row::importFromDatabase of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
297
	}
298
299
300
	/**
301
	 * @return array
302
	 */
303
	public function jsonSerialize(): array {
304
		$result = [
305
			'singleId'         => $this->getSingleId(),
306
			'circleId'         => $this->getCircleId(),
307
			'level'            => $this->getLevel(),
308
			'inheritanceFirst' => $this->getInheritanceFirst(),
309
			'inheritanceLast'  => $this->getInheritanceLast(),
310
			'inheritancePath'  => $this->getInheritancePath(),
311
			'inheritanceDepth' => $this->getInheritanceDepth()
312
		];
313
314
		if (!empty($this->getInheritanceDetails())) {
315
			$result['inheritanceDetails'] = $this->getInheritanceDetails();
316
		}
317
318
		return $result;
319
	}
320
321
}
322
323