Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/ElggAccessCollection.php (1 issue)

Checks if the types of returned expressions are compatible with the documented types.

Best Practice Bug Major
1
<?php
2
3
/**
4
 * Access collection class
5
 *
6
 * @package    Elgg.Core
7
 * @subpackage Core
8
 *
9
 * @property-read int    $id         The unique identifier (read-only)
10
 * @property      int    $owner_guid GUID of the owner
11
 * @property      string $name       Name of the collection
12
 */
13
class ElggAccessCollection extends ElggData {
14
15
	/**
16
	 * Create an access collection object
17
	 *
18
	 * @param stdClass $row Database row
19
	 * @throws InvalidArgumentException
20
	 */
21 56
	public function __construct(stdClass $row = null) {
22 56
		$this->initializeAttributes();
23
24 56
		foreach ((array) $row as $key => $value) {
25 56
			$this->attributes[$key] = $value;
26
		}
27 56
	}
28
29
	/**
30
	 * Initialize the attributes array
31
	 *
32
	 * @see ElggData::initializeAttributes()
33
	 * @return void
34
	 */
35 56
	protected function initializeAttributes() {
36 56
		parent::initializeAttributes();
37
38 56
		$this->attributes['id'] = null;
39 56
		$this->attributes['owner_guid'] = null;
40 56
		$this->attributes['name'] = null;
41 56
		$this->attributes['subtype'] = null;
42 56
	}
43
44
	/**
45
	 * Set an attribute
46
	 *
47
	 * @param string $name  Name
48
	 * @param mixed  $value Value
49
	 * @return void
50
	 * @throws RuntimeException
51
	 */
52 1
	public function __set($name, $value) {
53 1
		if (in_array($name, ['id', 'owner_guid', 'subtype'])) {
54
			throw new RuntimeException("$name can not be set at runtime");
55
		}
56 1
		$this->attributes[$name] = $value;
57 1
	}
58
59
	/**
60
	 * Get an attribute
61
	 *
62
	 * @param string $name Name
63
	 * @return mixed
64
	 */
65 56
	public function __get($name) {
66 56
		if (array_key_exists($name, $this->attributes)) {
67 56
			return $this->attributes[$name];
68
		}
69
70
		return null;
71
	}
72
73
	/**
74
	 * Returns owner entity of the collection
75
	 * @return \ElggEntity|false
76
	 */
77 5
	public function getOwnerEntity() {
78 5
		return _elgg_services()->entityTable->get($this->owner_guid);
79
	}
80
81
	/**
82
	 * Get readable access level name for this collection
83
	 * @return string
84
	 */
85
	public function getDisplayName() {
86
87 5
		$filter = function($name = null) {
88 5
			if (!isset($name)) {
89 2
				$name = _elgg_services()->translator->translate('access:limited:label');
90
			}
91
			$params = [
92 5
				'access_collection' => $this,
93
			];
94 5
			return _elgg_services()->hooks->trigger('access_collection:name', $this->getType(), $params, $name);
95 5
		};
96
97 5
		$user = _elgg_services()->session->getLoggedInUser();
98 5
		$owner = $this->getOwnerEntity();
99 5
		if (!$user || !$owner) {
100
			// User is not logged in or does not access to the owner entity:
101
			// return default 'Limited' label
102 1
			return $filter();
103
		}
104
		
105 5
		if ($user->isAdmin() || $owner->guid == $user->guid) {
106 4
			return $filter($this->name);
107
		}
108
109 1
		return $filter();
110
	}
111
112
	/**
113
	 * {@inheritdoc}
114
	 */
115 1
	public function save() {
116 1
		if ($this->id > 0) {
117 1
			return _elgg_services()->accessCollections->rename($this->id, $this->name);
118
		} else {
119
			return _elgg_services()->accessCollections->create($this->name, $this->owner_guid);
120
		}
121
	}
122
123
	/**
124
	 * {@inheritdoc}
125
	 */
126 46
	public function delete() {
127 46
		return _elgg_services()->accessCollections->delete($this->id);
128
	}
129
130
	/**
131
	 * Check if user can this collection
132
	 *
133
	 * @param int $user_guid GUID of the user
134
	 * @return bool
135
	 */
136 1
	public function canEdit($user_guid = null) {
137 1
		return _elgg_services()->accessCollections->canEdit($this->id, $user_guid);
138
	}
139
140
	/**
141
	 * Returns members of the access collection
142
	 *
143
	 * @param array $options ege options
144
	 * @return ElggEntity|int|false
145
	 */
146
	public function getMembers(array $options = []) {
147
		return _elgg_services()->accessCollections->getMembers($this->id, $options);
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression return _elgg_services()-...rs($this->id, $options) also could return the type ElggEntity[] which is incompatible with the documented return type ElggEntity|integer|false.
Loading history...
148
	}
149
150
	/**
151
	 * Checks if user is already in access collection
152
	 *
153
	 * @param int $member_guid GUID of the user
154
	 * @return bool
155
	 */
156 2
	public function hasMember($member_guid = 0) {
157 2
		return _elgg_services()->accessCollections->hasUser($member_guid, $this->id);
158
	}
159
160
	/**
161
	 * Adds a new member to access collection
162
	 *
163
	 * @param int $member_guid GUID of the user
164
	 * @return bool
165
	 */
166 9
	public function addMember($member_guid = 0) {
167 9
		return _elgg_services()->accessCollections->addUser($member_guid, $this->id);
168
	}
169
170
	/**
171
	 * Removes a user from access collection
172
	 *
173
	 * @param int $member_guid GUID of the user
174
	 * @return bool
175
	 */
176 3
	public function removeMember($member_guid = 0) {
177 3
		return _elgg_services()->accessCollections->removeUser($member_guid, $this->id);
178
	}
179
180
	/**
181
	 * {@inheritdoc}
182
	 */
183
	public function getURL() {
184
		$type = $this->getType();
185
		$params = [
186
			'access_collection' => $this,
187
		];
188
		$url = _elgg_services()->hooks->trigger('access_collection:url', $type, $params);
189
		return elgg_normalize_url($url);
190
	}
191
192
	/**
193
	 * {@inheritdoc}
194
	 */
195
	public function toObject() {
196
		$object = new stdClass();
197
		$object->type = $this->getType();
198
		$object->subtype = $this->getSubtype();
199
		$object->id = $this->id;
200
		$object->owner_guid = $this->owner_guid;
201
		$object->name = $this->name;
202
203
		$params = [
204
			'access_collection' => $this,
205
		];
206
		return _elgg_services()->hooks->trigger('to:object', 'access_collection', $params, $object);
207
	}
208
209
	/**
210
	 * {@inheritdoc}
211
	 */
212
	public function getSystemLogID() {
213
		return $this->id;
214
	}
215
216
	/**
217
	 * {@inheritdoc}
218
	 */
219
	public function getObjectFromID($id) {
220
		return _elgg_services()->accessCollections->get($id);
221
	}
222
223
	/**
224
	 * {@inheritdoc}
225
	 */
226 5
	public function getType() {
227 5
		return 'access_collection';
228
	}
229
230
	/**
231
	 * {@inheritdoc}
232
	 */
233 4
	public function getSubtype() {
234 4
		if (isset($this->subtype)) {
235 1
			return $this->subtype;
236
		}
237
		
238 3
		return $this->name;
239
	}
240
241
}
242