Passed
Push — 5.x ( 8b5a2d...246957 )
by Jerome
11:21 queued 14s
created

EventSerialization::__serialize()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 6
nop 0
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Elgg\Traits\Notifications;
4
5
/**
6
 * Allow events to be (un)serialized
7
 *
8
 * @internal
9
 */
10
trait EventSerialization {
11
	
12
	/**
13
	 * Serializes event object for database storage
14
	 *
15
	 * @return array
16
	 */
17 30
	public function __serialize() {
18 30
		$data = [];
19
		
20 30
		$data['action'] = $this->action;
21 30
		if ($this->object instanceof \ElggData) {
22 30
			if ($this->object instanceof \ElggEntity) {
23 30
				$data['object_id'] = $this->object->guid;
24
			} else {
25
				$data['object_id'] = $this->object->id;
26
			}
27
			
28 30
			$data['object_type'] = $this->object->getType();
29 30
			$data['object_subtype'] = $this->object->getSubtype();
30
		}
31
		
32 30
		if ($this->actor) {
33 23
			$data['actor_guid'] = $this->actor->guid;
34
		}
35
		
36 30
		return $data;
37
	}
38
39
	/**
40
	 * Unserializes the event object stored in the database
41
	 *
42
	 * @param array $data serialized data
43
	 *
44
	 * @return void
45
	 */
46 2
	public function __unserialize($data) {
47 2
		if (isset($data['action'])) {
48 2
			$this->action = $data['action'];
49
		}
50
		
51 2
		if (isset($data['object_id']) && isset($data['object_type'])) {
52 2
			$object_id = $data['object_id'];
53 2
			switch ($data['object_type']) {
54 2
				case 'object':
55
				case 'user':
56
				case 'group':
57
				case 'site':
58 2
					$this->object = get_entity($object_id);
59 2
					break;
60
				case 'annotation':
61
					$this->object = elgg_get_annotation_from_id($object_id);
62
					break;
63
				case 'metadata':
64
					$this->object = elgg_get_metadata_from_id($object_id);
65
					break;
66
				case 'relationship':
67
					$this->object = elgg_get_relationship($object_id);
68
					break;
69
			}
70
		}
71
		
72 2
		if (isset($data['actor_guid'])) {
73 2
			$this->actor = get_entity($data['actor_guid']);
74
		}
75
	}
76
}
77