Completed
Push — master ( 3ddca8...414a73 )
by Maxence
02:57
created

FederatedShare::getInstance()   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\Federated;
33
34
35
use daita\MySmallPhpTools\Db\Nextcloud\nc22\INC22QueryRow;
36
use daita\MySmallPhpTools\IDeserializable;
37
use daita\MySmallPhpTools\Model\SimpleDataStore;
38
use daita\MySmallPhpTools\Traits\TArrayTools;
39
use JsonSerializable;
40
use OCA\Circles\IFederatedModel;
41
use OCA\Circles\Model\ManagedModel;
42
43
44
/**
45
 * Class FederatedShare
46
 *
47
 * @package OCA\Circles\Model\Federated
48
 */
49
class FederatedShare extends ManagedModel implements IFederatedModel, JsonSerializable, INC22QueryRow, IDeserializable {
50
51
52
	use TArrayTools;
53
54
55
	/** @var int */
56
	private $id = 0;
57
58
	/** @var string */
59
	private $itemId = '';
60
61
	/** @var string */
62
	private $circleId = '';
63
64
	/** @var string */
65
	private $instance = '';
66
67
	/** @var string */
68
	private $lockStatus = '';
69
70
	/** @var SimpleDataStore */
71
	private $data;
72
73
74
	/**
75
	 * FederatedShare constructor.
76
	 */
77
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
78
	}
79
80
81
	/**
82
	 * @param int $id
83
	 *
84
	 * @return FederatedShare
85
	 */
86
	public function setId(int $id): self {
87
		$this->id = $id;
88
89
		return $this;
90
	}
91
92
	/**
93
	 * @return int
94
	 */
95
	public function getId(): int {
96
		return $this->id;
97
	}
98
99
100
	/**
101
	 * @param string $itemId
102
	 *
103
	 * @return FederatedShare
104
	 */
105
	public function setItemId(string $itemId): self {
106
		$this->itemId = $itemId;
107
108
		return $this;
109
	}
110
111
	/**
112
	 * @return string
113
	 */
114
	public function getItemId(): string {
115
		return $this->itemId;
116
	}
117
118
119
	/**
120
	 * @param string $circleId
121
	 *
122
	 * @return FederatedShare
123
	 */
124
	public function setCircleId(string $circleId): self {
125
		$this->circleId = $circleId;
126
127
		return $this;
128
	}
129
130
	/**
131
	 * @return string
132
	 */
133
	public function getCircleId(): string {
134
		return $this->circleId;
135
	}
136
137
138
	/**
139
	 * @param string $instance
140
	 *
141
	 * @return FederatedShare
142
	 */
143
	public function setInstance(string $instance): self {
144
		$this->instance = $instance;
145
146
		return $this;
147
	}
148
149
150
	/**
151
	 * @param string $lockStatus
152
	 *
153
	 * @return FederatedShare
154
	 */
155
	public function setLockStatus(string $lockStatus): self {
156
		$this->lockStatus = $lockStatus;
157
158
		return $this;
159
	}
160
161
	/**
162
	 * @return string
163
	 */
164
	public function getLockStatus(): string {
165
		return $this->lockStatus;
166
	}
167
168
169
	/**
170
	 * @return string
171
	 */
172
	public function getInstance(): string {
173
		return $this->instance;
174
	}
175
176
	/**
177
	 * @return bool
178
	 */
179
	public function isLocal(): bool {
180
		return $this->getManager()->isLocalInstance($this->getInstance());
181
	}
182
183
184
	/**
185
	 * @param array $data
186
	 *
187
	 * @return IDeserializable
188
	 */
189
	public function import(array $data): IDeserializable {
190
		$this->setId($this->getInt('id', $data));
191
		$this->setItemId($this->get('itemId', $data));
192
		$this->setCircleId($this->get('circleId', $data));
193
		$this->setInstance($this->get('instance', $data));
194
195
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (OCA\Circles\Model\Federated\FederatedShare) 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...
196
	}
197
198
199
	/**
200
	 * @param array $data
201
	 *
202
	 * @return INC22QueryRow
203
	 */
204
	public function importFromDatabase(array $data): INC22QueryRow {
205
		$this->setId($this->getInt('id', $data));
206
		$this->setItemId($this->get('item_id', $data));
207
		$this->setCircleId($this->get('circle_id', $data));
208
		$this->setInstance($this->get('instance', $data));
209
210
		if ($this->getInstance() === '') {
211
			$this->setInstance($this->getManager()->getLocalInstance());
212
		}
213
214
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (OCA\Circles\Model\Federated\FederatedShare) 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...
215
	}
216
217
218
	/**
219
	 * @return array
220
	 */
221
	public function jsonSerialize(): array {
222
		return [
223
			'id'       => $this->getId(),
224
			'itemId'   => $this->getItemId(),
225
			'circleId' => $this->getCircleId(),
226
			'instance' => $this->getInstance()
227
		];
228
	}
229
230
}
231
232