Completed
Pull Request — development (#2330)
by Joshua
41:37 queued 30:33
created

Notifications_Task   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 0%
Metric Value
wmc 11
lcom 2
cbo 1
dl 0
loc 112
ccs 0
cts 47
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 4
A getMembers() 0 4 1
A setMembers() 0 4 1
A getMembersData() 0 10 2
A getNotifierData() 0 10 2
A getClass() 0 4 1
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 1.0.3
12
 *
13
 */
14
15
if (!defined('ELK'))
16
	die('No access...');
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
	public function __construct($type, $id, $id_member, $data, $namespace = '')
48
	{
49
		$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...
50
			'notification_type' => $type,
51
			'namespace' => empty($namespace) ? '\\ElkArte\\sources\\subs\\MentionType\\' : rtrim($namespace, '\\') . '\\',
52
			'id_target' => $id,
53
			'id_member_from' => $id_member,
54
			'source_data' => $data,
55
			'log_time' => time()
56
		);
57
58
		if (!isset($this->data['source_data']['status']))
59
			$this->data['source_data']['status'] = 'new';
60
61
		if (isset($this->data['source_data']['id_members']))
62
			$this->setMembers($this->data['source_data']['id_members']);
63
		else
64
			$this->setMembers(array());
65
	}
66
67
	/**
68
	 * Returns the array of member that have to receive the notification.
69
	 *
70
	 * @return int[] An array of member id
71
	 */
72
	public function getMembers()
73
	{
74
		return $this->data['source_data']['id_members'];
75
	}
76
77
	/**
78
	 * Sets the members that have to receive the notification.
79
	 *
80
	 * @param int|int[] An array of member id
81
	 */
82
	public function setMembers($members)
83
	{
84
		$this->data['source_data']['id_members'] = (array) $members;
85
	}
86
87
	/**
88
	 * Returns the data from getBasicMemberData about the members to be notified.
89
	 *
90
	 * @return mixed[]
91
	 */
92
	public function getMembersData()
93
	{
94
		if ($this->_members_data === null)
95
		{
96
			require_once(SUBSDIR . '/Members.subs.php');
97
			$this->_members_data = getBasicMemberData($this->getMembers(), array('preferences' => true));
98
		}
99
100
		return $this->_members_data;
101
	}
102
103
	/**
104
	 * Returns the data from getBasicMemberData about the member that
105
	 * generated the notification
106
	 *
107
	 * @return mixed[]
108
	 */
109
	public function getNotifierData()
110
	{
111
		if ($this->_notifier_data === null)
112
		{
113
			require_once(SUBSDIR . '/Members.subs.php');
114
			$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...
115
		}
116
117
		return $this->_notifier_data;
118
	}
119
120
	/**
121
	 * Returns the class name of the MentionType.
122
	 *
123
	 * @return string
124
	 */
125
	public function getClass()
126
	{
127
		return $this->data['namespace'] . ucfirst($this->data['notification_type']) . '_Mention';
128
	}
129
}