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

EventSerialization   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 79.41%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 27
cts 34
cp 0.7941
rs 10
c 0
b 0
f 0
wmc 16

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 16 4
C unserialize() 0 26 12
1
<?php
2
3
namespace Elgg\Notifications;
4
5
use ElggData;
6
use ElggEntity;
7
use stdClass;
8
9
/**
10
 * Allow events to be (un)serialized
11
 */
12
trait EventSerialization {
13
	
14
	/**
15
	 * Serializes event object for database storage
16
	 * @return string
17
	 */
18 49
	public function serialize() {
19 49
		$data = new stdClass();
20 49
		$data->action = $this->action;
21 49
		if ($this->object instanceof ElggData) {
22 49
			if ($this->object instanceof ElggEntity) {
23 28
				$data->object_id = $this->object->guid;
24
			} else {
25 21
				$data->object_id = $this->object->id;
26
			}
27 49
			$data->object_type = $this->object->getType();
28 49
			$data->object_subtype = $this->object->getSubtype();
29
		}
30 49
		if ($this->actor) {
31 49
			$data->actor_guid = $this->actor->guid;
32
		}
33 49
		return serialize($data);
34
	}
35
36
	/**
37
	 * Unserializes the event object stored in the database
38
	 *
39
	 * @param string $serialized Serialized string
40
	 * @return string
41
	 */
42 36
	public function unserialize($serialized) {
43 36
		$data = unserialize($serialized);
44 36
		if (isset($data->action)) {
45 36
			$this->action = $data->action;
1 ignored issue
show
Bug Best Practice introduced by
The property action does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
		}
47 36
		if (isset($data->object_id) && isset($data->object_type)) {
48 36
			switch ($data->object_type) {
49
				case 'object' :
50
				case 'user' :
51
				case 'group' :
52
				case 'site' :
53 18
					$this->object = get_entity($data->object_id);
1 ignored issue
show
Bug Best Practice introduced by
The property object does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54 18
					break;
55
				case 'annotation' :
56 6
					$this->object = elgg_get_annotation_from_id($data->object_id);
57 6
					break;
58
				case 'metadata' :
59 6
					$this->object = elgg_get_metadata_from_id($data->object_id);
60 6
					break;
61
				case 'relationship' :
62 6
					$this->object = get_relationship($data->object_id);
63
			}
64
		}
65
		
66 36
		if (isset($data->actor_guid)) {
67 36
			$this->actor = get_entity($data->actor_guid);
1 ignored issue
show
Bug Best Practice introduced by
The property actor does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
68
		}
69 36
	}
70
71
}
72