Completed
Push — master ( 0030e4...e4893b )
by Jeroen
13:26
created

ElggAccessCollection::getMembers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
	public function __construct(stdClass $row = null) {
22
		$this->initializeAttributes();
23
24
		foreach ((array) $row as $key => $value) {
25
			$this->attributes[$key] = $value;
26
		}
27
	}
28
29
	/**
30
	 * Initialize the attributes array
31
	 *
32
	 * @see ElggData::initializeAttributes()
33
	 * @return void
34
	 */
35
	protected function initializeAttributes() {
36
		parent::initializeAttributes();
37
38
		$this->attributes['id'] = null;
39
		$this->attributes['owner_guid'] = null;
40
		$this->attributes['name'] = null;
41
	}
42
43
	/**
44
	 * Set an attribute
45
	 *
46
	 * @param string $name  Name
47
	 * @param mixed  $value Value
48
	 * @return void
49
	 * @throws RuntimeException
50
	 */
51
	public function __set($name, $value) {
52
		if (in_array($name, ['id', 'owner_guid'])) {
53
			throw new RuntimeException("$name can not be set at runtime");
54
		}
55
		$this->attributes[$name] = $value;
56
	}
57
58
	/**
59
	 * Get an attribute
60
	 *
61
	 * @param string $name Name
62
	 * @return mixed
63
	 */
64
	public function __get($name) {
65
		if (array_key_exists($name, $this->attributes)) {
66
			return $this->attributes[$name];
67
		}
68
69
		return null;
70
	}
71
72
	/**
73
	 * {@inheritdoc}
74
	 */
75
	public function save() {
76
		if ($this->id > 0) {
77
			return _elgg_services()->accessCollections->rename($this->id, $this->name);
0 ignored issues
show
Bug Compatibility introduced by
The expression _elgg_services()->access...this->id, $this->name); of type integer|false adds the type integer to the return on line 77 which is incompatible with the return type declared by the abstract method ElggData::save of type boolean.
Loading history...
78
		} else {
79
			return _elgg_services()->accessCollections->create($this->name, $this->owner_guid);
0 ignored issues
show
Bug Compatibility introduced by
The expression _elgg_services()->access...me, $this->owner_guid); of type false|integer adds the type integer to the return on line 79 which is incompatible with the return type declared by the abstract method ElggData::save of type boolean.
Loading history...
80
		}
81
	}
82
83
	/**
84
	 * {@inheritdoc}
85
	 */
86
	public function delete() {
87
		return _elgg_services()->accessCollections->delete($this->id);
88
	}
89
90
	/**
91
	 * Check if user can this collection
92
	 * 
93
	 * @param int $user_guid GUID of the user
94
	 * @return bool
95
	 */
96
	public function canEdit($user_guid = null) {
97
		return _elgg_services()->accessCollections->canEdit($this->id, $user_guid);
98
	}
99
100
	/**
101
	 * Returns members of the access collection
102
	 * 
103
	 * @param array $options ege options
104
	 * @return ElggEntity|int|false
105
	 */
106
	public function getMembers(array $options = []) {
107
		return _elgg_services()->accessCollections->getMembers($this->id, $options);
108
	}
109
110
	/**
111
	 * Checks if user is already in access collection
112
	 * 
113
	 * @param int $member_guid GUID of the user
114
	 * @return bool
115
	 */
116
	public function hasMember($member_guid = 0) {
117
		return _elgg_services()->accessCollections->hasUser($member_guid, $this->id);
118
	}
119
120
	/**
121
	 * Adds a new member to access collection
122
	 * 
123
	 * @param int $member_guid GUID of the user
124
	 * @return bool
125
	 */
126
	public function addMember($member_guid = 0) {
127
		return _elgg_services()->accessCollections->addUser($member_guid, $this->id);
128
	}
129
130
	/**
131
	 * Removes a user from access collection
132
	 * 
133
	 * @param int $member_guid GUID of the user
134
	 * @return bool
135
	 */
136
	public function removeMember($member_guid = 0) {
137
		return _elgg_services()->accessCollections->removeUser($member_guid, $this->id);
138
	}
139
140
	/**
141
	 * {@inheritdoc}
142
	 */
143
	public function getURL() {
144
		$type = $this->getType();
145
		$params = [
146
			'access_collection' => $this,
147
		];
148
		$url = _elgg_services()->hooks->trigger('access_collection:url', $type, $params, $url);
0 ignored issues
show
Bug introduced by
The variable $url seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
149
		return elgg_normalize_url($url);
150
	}
151
152
	/**
153
	 * {@inheritdoc}
154
	 */
155
	public function toObject() {
156
		$object = new stdClass();
157
		$object->type = $this->getType();
158
		$object->subtype = $this->getSubtype();
159
		$object->id = $this->id;
160
		$object->owner_guid = $this->owner_guid;
161
		$object->name = $this->name;
162
163
		$params = [
164
			'access_collection' => $this,
165
		];
166
		return _elgg_services()->hooks->trigger('to:object', 'access_collection', $params, $object);
167
	}
168
169
	/**
170
	 * {@inheritdoc}
171
	 */
172
	public function getSystemLogID() {
173
		return $this->id;
174
	}
175
176
	/**
177
	 * {@inheritdoc}
178
	 */
179
	public function getObjectFromID($id) {
180
		return _elgg_services()->accessCollections->get($id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return _elgg_services()-...sCollections->get($id); (array) is incompatible with the return type declared by the interface Loggable::getObjectFromID of type ElggEntity.

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...
181
	}
182
183
	/**
184
	 * {@inheritdoc}
185
	 */
186
	public function getType() {
187
		return 'access_collection';
188
	}
189
190
	/**
191
	 * {@inheritdoc}
192
	 */
193
	public function getSubtype() {
194
		return $this->name;
195
	}
196
197
}
198