Issues (1697)

ElkArte/Notifications/NotificationsTask.php (1 issue)

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
 * @package   ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
10
 *
11
 * @version 2.0 dev
12
 *
13
 */
14
15
namespace ElkArte\Notifications;
16
17
use ElkArte\Helper\ValuesContainer;
18
19
/**
20
 * Class NotificationsTask
21
 */
22
class NotificationsTask extends ValuesContainer
23
{
24
	/** @var array Data of the members to notify. Populated only if the getMembersData method is called. */
25
	protected $_members_data;
26
27
	/** @var array Data of the member generating the notification. Populated only if the getNotifierData method is called. */
28
	protected $_notifier_data;
29
30
	/**
31
	 * The constructor prepared the data array and fills some default values if needed.
32
	 *
33
	 * @param string $type The notification type we are dealing with
34
	 * @param int $id The id of the target (can be a message, a topic, a member, whatever)
35
	 * @param int $id_member The id of the member generating the notification
36
	 * @param array $data An array of data that can be necessary in the process
37
	 * @param string $namespace A namespace for the class if different from the default \ElkArte\Mentions\MentionType\Notification\
38
	 */
39
	public function __construct($type, $id, $id_member, $data, $namespace = '')
40
	{
41
		parent::__construct();
42
43
		$this->data = [
44
			'notification_type' => $type,
45
			'namespace' => empty($namespace) ? '\\ElkArte\\Mentions\\MentionType\\Notification\\' : rtrim($namespace, '\\') . '\\',
46
			'id_target' => $id,
47
			'id_member_from' => $id_member,
48
			'source_data' => $data,
49
			'log_time' => time()
50
		];
51
52
		if (!isset($this->data['source_data']['status']))
53
		{
54
			$this->data['source_data']['status'] = 'new';
55
		}
56
57
		if (isset($this->data['source_data']['id_members']))
58
		{
59
			$this->setMembers($this->data['source_data']['id_members']);
60
		}
61
		else
62
		{
63
			$this->setMembers([]);
64
		}
65
	}
66
67
	/**
68
	 * Sets the members that have to receive the notification.
69
	 *
70
	 * @param int|int[] $members An array of member id
71
	 */
72
	public function setMembers($members): void
73
	{
74
		$this->data['source_data']['id_members'] = (array) $members;
75
	}
76
77
	/**
78
	 * Returns the data from getBasicMemberData about the members to be notified.
79
	 *
80
	 * @return array
81
	 */
82
	public function getMembersData(): array
83
	{
84
		if ($this->_members_data === null)
85
		{
86
			require_once(SUBSDIR . '/Members.subs.php');
87
			$this->_members_data = getBasicMemberData($this->getMembers(), ['preferences' => true, 'authentication' => true, 'lists' => 'true']);
88
		}
89
90
		return $this->_members_data;
91
	}
92
93
	/**
94
	 * Returns the array of member that have to receive the notification.
95
	 *
96
	 * @return int[] An array of member id
97
	 */
98
	public function getMembers(): array
99
	{
100
		return $this->data['source_data']['id_members'];
101
	}
102
103
	/**
104
	 * Returns the data from getBasicMemberData about the member that generated the notification
105
	 *
106
	 * @return array
107
	 */
108
	public function getNotifierData(): array
109
	{
110
		if ($this->_notifier_data === null)
111
		{
112
			require_once(SUBSDIR . '/Members.subs.php');
113
			$this->_notifier_data = getBasicMemberData($this->id_member_from);
0 ignored issues
show
Bug Best Practice introduced by
The property id_member_from does not exist on ElkArte\Notifications\NotificationsTask. Since you implemented __get, consider adding a @property annotation.
Loading history...
114
		}
115
116
		return $this->_notifier_data;
117
	}
118
119
	/**
120
	 * Returns the fully qualified class name for the notification/MentionType type.
121
	 *
122
	 * @return string The fully qualified class name for the notification type.
123
	 */
124
	public function getClass(): string
125
	{
126
		return $this->data['namespace'] . ucfirst($this->data['notification_type']);
127
	}
128
}
129