Test Failed
Push — master ( 8c47c2...3acf9f )
by Steve
12:37
created

engine/classes/ElggExtender.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * The base class for \ElggEntity extenders.
4
 *
5
 * Extenders allow you to attach extended information to an
6
 * \ElggEntity.  Core supports two: \ElggAnnotation and \ElggMetadata.
7
 *
8
 * Saving the extender data to database is handled by the child class.
9
 *
10
 * @package    Elgg.Core
11
 * @subpackage DataModel.Extender
12
 * @see        \ElggAnnotation
13
 * @see        \ElggMetadata
14
 *
15
 * @property string $type         annotation or metadata (read-only after save)
16
 * @property int    $id           The unique identifier (read-only)
17
 * @property int    $entity_guid  The GUID of the entity that this extender describes
18
 * @property int    $owner_guid   The GUID of the owner of this extender
19
 * @property int    $access_id    Specifies the visibility level of this extender
20
 * @property string $name         The name of this extender
21
 * @property mixed  $value        The value of the extender (int or string)
22
 * @property int    $time_created A UNIX timestamp of when the extender was created (read-only, set on first save)
23
 * @property string $value_type   'integer' or 'text'
24
 * @property string $enabled      Is this extender enabled ('yes' or 'no')
25
 */
26
abstract class ElggExtender extends \ElggData {
27
28
	/**
29
	 * (non-PHPdoc)
30
	 *
31
	 * @see \ElggData::initializeAttributes()
32
	 *
33
	 * @return void
34
	 */
35 105
	protected function initializeAttributes() {
36 105
		parent::initializeAttributes();
37
38 105
		$this->attributes['type'] = null;
39 105
		$this->attributes['id'] = null;
40 105
		$this->attributes['entity_guid'] = null;
41 105
		$this->attributes['owner_guid'] = null;
42 105
		$this->attributes['access_id'] = ACCESS_PRIVATE;
43 105
		$this->attributes['enabled'] = 'yes';
44 105
	}
45
46
	/**
47
	 * Set an attribute
48
	 *
49
	 * @param string $name  Name
50
	 * @param mixed  $value Value
51
	 * @return void
52
	 */
53 4
	public function __set($name, $value) {
54 4
		if ($name === 'access_id' && $this instanceof ElggMetadata) {
55
			$value = ACCESS_PUBLIC;
56
		}
57 4
		$this->attributes[$name] = $value;
58 4
		if ($name == 'value') {
59 2
			$this->attributes['value_type'] = self::detectValueType($value);
60
		}
61 4
	}
62
63
	/**
64
	 * Set the value of the extender
65
	 *
66
	 * @param mixed  $value      The value being set
67
	 * @param string $value_type The type of the : 'integer' or 'text'
68
	 * @return void
69
	 * @since 1.9
70
	 */
71 2
	public function setValue($value, $value_type = '') {
72 2
		$this->attributes['value'] = $value;
73 2
		$this->attributes['value_type'] = self::detectValueType($value, $value_type);
74 2
	}
75
76
	/**
77
	 * Gets an attribute
78
	 *
79
	 * @param string $name Name
80
	 * @return mixed
81
	 */
82 58
	public function __get($name) {
83 58
		if (array_key_exists($name, $this->attributes)) {
84 57
			if ($name == 'value') {
85 22
				switch ($this->attributes['value_type']) {
86 22
					case 'integer' :
87 2
						return (int) $this->attributes['value'];
88
						break;
89 21
					case 'text' :
90 21
						return $this->attributes['value'];
91
						break;
92
					default :
93
						$msg = "{$this->attributes['value_type']} is not a supported \ElggExtender value type.";
94
						throw new \UnexpectedValueException($msg);
95
						break;
96
				}
97
			}
98
99 57
			if ($name === 'access_id' && $this instanceof ElggMetadata) {
100 3
				return ACCESS_PUBLIC;
101
			}
102
103 57
			return $this->attributes[$name];
104
		}
105
106 3
		return null;
107
	}
108
109
	/**
110
	 * Get the GUID of the extender's owner entity.
111
	 *
112
	 * @return int The owner GUID
113
	 */
114 1
	public function getOwnerGUID() {
115 1
		return $this->owner_guid;
116
	}
117
118
	/**
119
	 * Get the entity that owns this extender
120
	 *
121
	 * @return \ElggEntity
122
	 */
123 1
	public function getOwnerEntity() {
124 1
		return get_entity($this->owner_guid);
125
	}
126
127
	/**
128
	 * Get the entity this describes.
129
	 *
130
	 * @return \ElggEntity The entity
131
	 */
132 6
	public function getEntity() {
133 6
		return get_entity($this->entity_guid);
134
	}
135
136
	/**
137
	 * Returns if a user can edit this entity extender.
138
	 *
139
	 * @param int $user_guid The GUID of the user doing the editing
140
	 *                      (defaults to currently logged in user)
141
	 *
142
	 * @return bool
143
	 * @see elgg_set_ignore_access()
144
	 */
145
	abstract public function canEdit($user_guid = 0);
146
147
	/**
148
	 * {@inheritdoc}
149
	 */
150 1
	public function toObject() {
151 1
		$object = new \stdClass();
152 1
		$object->id = $this->id;
153 1
		$object->entity_guid = $this->entity_guid;
154 1
		$object->owner_guid = $this->owner_guid;
155 1
		$object->name = $this->name;
156 1
		$object->value = $this->value;
157 1
		$object->time_created = date('c', $this->getTimeCreated());
158 1
		$object->read_access = $this->access_id;
159
		$params = [
160 1
			$this->getSubtype() => $this, // deprecated use
161 1
			$this->getType() => $this,
162
		];
163 1
		if (_elgg_services()->hooks->hasHandler('to:object', $this->getSubtype())) {
164
			_elgg_services()->deprecation->sendNotice("Triggering 'to:object' hook by extender name '{$this->getSubtype()}' has been deprecated. "
165
			. "Use the generic 'to:object','{$this->getType()}' hook instead.", '2.3');
166
			$object = _elgg_services()->hooks->trigger('to:object', $this->getSubtype(), $params, $object);
167
		}
168 1
		return _elgg_services()->hooks->trigger('to:object', $this->getType(), $params, $object);
169
	}
170
171
	/*
172
	 * SYSTEM LOG INTERFACE
173
	 */
174
175
	/**
176
	 * Return an identification for the object for storage in the system log.
177
	 * This id must be an integer.
178
	 *
179
	 * @return int
180
	 */
181 1
	public function getSystemLogID() {
182 1
		return $this->id;
183
	}
184
185
	/**
186
	 * Return a type of extension.
187
	 *
188
	 * @return string
189
	 */
190 26
	public function getType() {
191 26
		return $this->type;
192
	}
193
194
	/**
195
	 * Return a subtype. For metadata & annotations this is the 'name' and
196
	 * for relationship this is the relationship type.
197
	 *
198
	 * @return string
199
	 */
200 26
	public function getSubtype() {
201 26
		return $this->name;
202
	}
203
204
	/**
205
	 * Get a url for this extender.
206
	 *
207
	 * Plugins can register for the 'extender:url', <type> plugin hook to
208
	 * customize the url for an annotation or metadata.
209
	 *
210
	 * @return string
211
	 */
212 7
	public function getURL() {
213
214 7
		$url = "";
215 7
		$type = $this->getType();
216 7
		$subtype = $this->getSubtype();
217
218
		// @todo remove when elgg_register_extender_url_handler() has been removed
219 7
		if ($this->id) {
220 7
			global $CONFIG;
221
222 7
			$function = "";
223 7
			if (isset($CONFIG->extender_url_handler[$type][$subtype])) {
224
				$function = $CONFIG->extender_url_handler[$type][$subtype];
225
			}
226 7 View Code Duplication
			if (isset($CONFIG->extender_url_handler[$type]['all'])) {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
				$function = $CONFIG->extender_url_handler[$type]['all'];
228
			}
229 7 View Code Duplication
			if (isset($CONFIG->extender_url_handler['all']['all'])) {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
				$function = $CONFIG->extender_url_handler['all']['all'];
231
			}
232 7
			if (is_callable($function)) {
233
				$url = call_user_func($function, $this);
234
			}
235
236 7
			if ($url) {
237
				$url = elgg_normalize_url($url);
238
			}
239
		}
240
241 7
		$params = ['extender' => $this];
242 7
		$url = _elgg_services()->hooks->trigger('extender:url', $type, $params, $url);
243
244 7
		return elgg_normalize_url($url);
245
	}
246
247
	/**
248
	 * Detect the value_type for a value to be stored as metadata or an annotation
249
	 *
250
	 * @param mixed  $value      The value
251
	 * @param string $value_type If specified as "text" or "integer", overrides the detection.
252
	 *
253
	 * @return string
254
	 * @access private
255
	 * @internal
256
	 */
257 101
	public static function detectValueType($value, $value_type = "") {
258 101
		if ($value_type === 'integer' || $value_type === 'text') {
259 88
			return $value_type;
260
		}
261
262 34
		return is_int($value) ? 'integer' : 'text';
263
	}
264
}
265