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

InstantNotificationEvent::getDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 2
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $object can also be of type ElggData. However, the property $object is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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