Completed
Pull Request — development (#3050)
by John
23:37
created

Notifications_Task::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 8
nop 5
dl 0
loc 21
ccs 14
cts 15
cp 0.9333
crap 4.0047
rs 9.0534
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This is a notification task, by default a container that may act like
5
 * an array (through ArrayAccess), with some ad-hoc methods.
6
 *
7
 * @name      ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
10
 *
11
 * @version 2.0 dev
12
 *
13
 */
14
15
/**
16
 * Class Notifications_Task
17
 */
18
class Notifications_Task extends \ElkArte\ValuesContainer
19
{
20
	/**
21
	 * Data of the members to notify.
22
	 * Populated only if the getMembersData method is called.
23
	 *
24
	 * @var mixed[]
25
	 */
26
	protected $_members_data = null;
27
28
	/**
29
	 * Data of the member generating the notification.
30
	 * Populated only if the getNotifierData method is called.
31
	 *
32
	 * @var mixed[]
33
	 */
34
	protected $_notifier_data = null;
35
36
	/**
37
	 * The constructor prepared the data array and fills some default values
38
	 * if needed.
39
	 *
40
	 * @param string $type The notification type we are dealing with
41
	 * @param int $id The id of the target (can be a message, a topic, a member, whatever)
42
	 * @param int $id_member The id of the member generating the notification
43
	 * @param mixed[] $data An array of data that can be necessary in the process
44
	 * @param string $namespace A namespace for the class if different from the
45
	 *               default \ElkArte\sources\subs\MentionType\
46
	 */
47 2
	public function __construct($type, $id, $id_member, $data, $namespace = '')
48
	{
49 2
		parent::__construct();
50
51 2
		$this->data = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('notification_type..., 'log_time' => time()) of type array<string,string|inte...,"log_time":"integer"}> is incompatible with the declared type array<integer,*> of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52 2
			'notification_type' => $type,
53 2
			'namespace' => empty($namespace) ? '\\ElkArte\\sources\\subs\\MentionType\\' : rtrim($namespace, '\\') . '\\',
54 2
			'id_target' => $id,
55 2
			'id_member_from' => $id_member,
56 2
			'source_data' => $data,
57 2
			'log_time' => time()
58
		);
59
60 2
		if (!isset($this->data['source_data']['status']))
61 2
			$this->data['source_data']['status'] = 'new';
62
63 2
		if (isset($this->data['source_data']['id_members']))
64 2
			$this->setMembers($this->data['source_data']['id_members']);
65
		else
66
			$this->setMembers(array());
67 2
	}
68
69
	/**
70
	 * Returns the array of member that have to receive the notification.
71
	 *
72
	 * @return int[] An array of member id
73
	 */
74 2
	public function getMembers()
75
	{
76 2
		return $this->data['source_data']['id_members'];
77
	}
78
79
	/**
80
	 * Sets the members that have to receive the notification.
81
	 *
82
	 * @param int|int[] An array of member id
83
	 */
84 2
	public function setMembers($members)
85
	{
86 2
		$this->data['source_data']['id_members'] = (array) $members;
87 2
	}
88
89
	/**
90
	 * Returns the data from getBasicMemberData about the members to be notified.
91
	 *
92
	 * @return mixed[]
93
	 */
94 2
	public function getMembersData()
95
	{
96 2
		if ($this->_members_data === null)
97
		{
98 2
			require_once(SUBSDIR . '/Members.subs.php');
99 2
			$this->_members_data = getBasicMemberData($this->getMembers(), array('preferences' => true));
100
		}
101
102 2
		return $this->_members_data;
103
	}
104
105
	/**
106
	 * Returns the data from getBasicMemberData about the member that
107
	 * generated the notification
108
	 *
109
	 * @return mixed[]
110
	 */
111
	public function getNotifierData()
112
	{
113
		if ($this->_notifier_data === null)
114
		{
115
			require_once(SUBSDIR . '/Members.subs.php');
116
			$this->_notifier_data = getBasicMemberData($this->id_member_from);
0 ignored issues
show
Documentation introduced by
The property id_member_from does not exist on object<Notifications_Task>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
		}
118
119
		return $this->_notifier_data;
120
	}
121
122
	/**
123
	 * Returns the class name of the MentionType.
124
	 *
125
	 * @return string
126
	 */
127 2
	public function getClass()
128
	{
129 2
		return $this->data['namespace'] . ucfirst($this->data['notification_type']) . '_Mention';
130
	}
131
}