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

Elgg/Notifications/InstantNotificationEvent.php (1 issue)

1
<?php
2
3
namespace Elgg\Notifications;
4
5
use ElggData;
6
use ElggEntity;
7
use stdClass;
8
9
/**
10
 * Instant notification event
11
 *
12
 * @since 2.3
13
 */
14
class InstantNotificationEvent implements NotificationEvent {
15
16
	use EventSerialization;
17
	
18
	const DEFAULT_ACTION_NAME = 'notify_user';
19
20
	/* @var string The name of the action/event */
21
22
	protected $action;
23
24
	/* @var string Action's object */
25
	protected $object;
26
27
	/* @var ElggEntity User who triggered the event */
28
	protected $actor;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param ElggData   $object The object of the event (ElggEntity)
34
	 * @param string     $action The name of the action (default: create)
35
	 * @param ElggEntity $actor  The entity that caused the event (default: logged in user)
36
	 */
37 26
	public function __construct(ElggData $object = null, $action = null, ElggEntity $actor = null) {
38
39 26
		$this->object = $object;
40
41 26
		$this->actor = $actor;
42 26
		if (!isset($actor)) {
43
			$this->actor = _elgg_services()->session->getLoggedInUser();
44
		}
45
46 26
		$this->action = $action ? : self::DEFAULT_ACTION_NAME;
47 26
	}
48
49
	/**
50
	 * Get the actor of the event
51
	 *
52
	 * @note Note that the actor and the object of the notification event
53
	 * may have been deleted/disabled since the event was serialized and
54
	 * stored in the database.
55
	 *
56
	 * @return ElggEntity|false|null
57
	 */
58 20
	public function getActor() {
59 20
		return $this->actor;
60
	}
61
62
	/**
63
	 * Get the GUID of the actor
64
	 *
65
	 * @note Note that the actor and the object of the notification event
66
	 * may have been deleted/disabled since the event was serialized and
67
	 * stored in the database.
68
	 *
69
	 * @return int
70
	 */
71
	public function getActorGUID() {
72
		return $this->actor ? $this->actor->guid : 0;
73
	}
74
75
	/**
76
	 * Get the object of the event
77
	 *
78
	 * @note Note that the actor and the object of the notification event
79
	 * may have been deleted/disabled since the event was serialized and
80
	 * stored in the database.
81
	 *
82
	 * @return ElggData|false|null
83
	 */
84 20
	public function getObject() {
85 20
		return $this->object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->object returns the type string which is incompatible with the documented return type null|false|ElggData.
Loading history...
86
	}
87
88
	/**
89
	 * Get the name of the action
90
	 *
91
	 * @return string
92
	 */
93 20
	public function getAction() {
94 20
		return $this->action;
95
	}
96
97
	/**
98
	 * Get a description of the event
99
	 *
100
	 * @return string
101
	 */
102 20
	public function getDescription() {
103 20
		if (!$this->object) {
104 12
			return $this->action;
105
		}
106
107 8
		return implode(':', [
108 8
			$this->action,
109 8
			$this->object->getType(),
110 8
			$this->object->getSubtype(),
111
		]);
112
	}
113
114
	/**
115
	 * Export the notification event into a serializable object
116
	 * This method is mainly used for logging purposes
117
	 *
118
	 * @return stdClass
119
	 */
120
	public function toObject() {
121
		$obj = new stdClass();
122
		$vars = get_object_vars($this);
123
		foreach ($vars as $key => $value) {
124
			if (is_object($value) && is_callable([$value, 'toObject'])) {
125
				$obj->$key = $value->toObject();
126
			} else {
127
				$obj->$key = $value;
128
			}
129
		}
130
		return $obj;
131
	}
132
}
133