Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
73.81% |
31 / 42 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
|
73.81% |
31 / 42 |
|
62.50% |
5 / 8 |
32.50 | |
0.00% |
0 / 1 |
|
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
get_type | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
is_available | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
get_notified_users | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
notify | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
mark_notifications | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
mark_notifications_by_parent | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
clean_data | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\notification\method; |
15 | |
16 | use phpbb\notification\type\type_interface; |
17 | |
18 | /** |
19 | * Email notification method class |
20 | * This class handles sending emails for notifications |
21 | */ |
22 | |
23 | class email extends \phpbb\notification\method\messenger_base |
24 | { |
25 | /** @var \phpbb\user */ |
26 | protected $user; |
27 | |
28 | /** @var \phpbb\config\config */ |
29 | protected $config; |
30 | |
31 | /** @var \phpbb\db\driver\driver_interface */ |
32 | protected $db; |
33 | |
34 | /** @var string Notification emails table */ |
35 | protected $notification_emails_table; |
36 | |
37 | /** |
38 | * Notification Method email Constructor |
39 | * |
40 | * @param \phpbb\user_loader $user_loader |
41 | * @param \phpbb\user $user |
42 | * @param \phpbb\config\config $config |
43 | * @param \phpbb\db\driver\driver_interface $db |
44 | * @param string $phpbb_root_path |
45 | * @param string $php_ext |
46 | * @param string $notification_emails_table |
47 | */ |
48 | public function __construct(\phpbb\user_loader $user_loader, \phpbb\user $user, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext, $notification_emails_table) |
49 | { |
50 | parent::__construct($user_loader, $phpbb_root_path, $php_ext); |
51 | |
52 | $this->user = $user; |
53 | $this->config = $config; |
54 | $this->db = $db; |
55 | $this->notification_emails_table = $notification_emails_table; |
56 | } |
57 | |
58 | /** |
59 | * Get notification method name |
60 | * |
61 | * @return string |
62 | */ |
63 | public function get_type() |
64 | { |
65 | return 'notification.method.email'; |
66 | } |
67 | |
68 | /** |
69 | * Is this method available for the user? |
70 | * This is checked on the notifications options |
71 | * |
72 | * @param type_interface|null $notification_type An optional instance of a notification type. If provided, this |
73 | * method additionally checks if the type provides an email template. |
74 | * @return bool |
75 | */ |
76 | public function is_available(type_interface $notification_type = null) |
77 | { |
78 | return parent::is_available($notification_type) && $this->config['email_enable'] && !empty($this->user->data['user_email']); |
79 | } |
80 | |
81 | /** |
82 | * {@inheritdoc} |
83 | */ |
84 | public function get_notified_users($notification_type_id, array $options) |
85 | { |
86 | $notified_users = []; |
87 | |
88 | $sql = 'SELECT user_id |
89 | FROM ' . $this->notification_emails_table . ' |
90 | WHERE notification_type_id = ' . (int) $notification_type_id . |
91 | (isset($options['item_id']) ? ' AND item_id = ' . (int) $options['item_id'] : '') . |
92 | (isset($options['item_parent_id']) ? ' AND item_parent_id = ' . (int) $options['item_parent_id'] : '') . |
93 | (isset($options['user_id']) ? ' AND user_id = ' . (int) $options['user_id'] : ''); |
94 | $result = $this->db->sql_query($sql); |
95 | while ($row = $this->db->sql_fetchrow($result)) |
96 | { |
97 | $notified_users[$row['user_id']] = $row; |
98 | } |
99 | $this->db->sql_freeresult($result); |
100 | |
101 | return $notified_users; |
102 | } |
103 | |
104 | /** |
105 | * Parse the queue and notify the users |
106 | */ |
107 | public function notify() |
108 | { |
109 | $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->notification_emails_table); |
110 | |
111 | /** @var type_interface $notification */ |
112 | foreach ($this->queue as $notification) |
113 | { |
114 | $data = self::clean_data($notification->get_insert_array()); |
115 | $insert_buffer->insert($data); |
116 | } |
117 | |
118 | $insert_buffer->flush(); |
119 | |
120 | $this->notify_using_messenger(NOTIFY_EMAIL); |
121 | } |
122 | |
123 | /** |
124 | * {@inheritdoc} |
125 | */ |
126 | public function mark_notifications($notification_type_id, $item_id, $user_id, $time = false, $mark_read = true) |
127 | { |
128 | $sql = 'DELETE FROM ' . $this->notification_emails_table . ' |
129 | WHERE ' . ($notification_type_id !== false ? $this->db->sql_in_set('notification_type_id', is_array($notification_type_id) ? $notification_type_id : [$notification_type_id]) : '1=1') . |
130 | ($user_id !== false ? ' AND ' . $this->db->sql_in_set('user_id', $user_id) : '') . |
131 | ($item_id !== false ? ' AND ' . $this->db->sql_in_set('item_id', $item_id) : ''); |
132 | $this->db->sql_query($sql); |
133 | } |
134 | |
135 | /** |
136 | * {@inheritdoc} |
137 | */ |
138 | public function mark_notifications_by_parent($notification_type_id, $item_parent_id, $user_id, $time = false, $mark_read = true) |
139 | { |
140 | $sql = 'DELETE FROM ' . $this->notification_emails_table . ' |
141 | WHERE ' . ($notification_type_id !== false ? $this->db->sql_in_set('notification_type_id', is_array($notification_type_id) ? $notification_type_id : [$notification_type_id]) : '1=1') . |
142 | ($user_id !== false ? ' AND ' . $this->db->sql_in_set('user_id', $user_id) : '') . |
143 | ($item_parent_id !== false ? ' AND ' . $this->db->sql_in_set('item_parent_id', $item_parent_id, false, true) : ''); |
144 | $this->db->sql_query($sql); |
145 | } |
146 | |
147 | /** |
148 | * Clean data to contain only what we need for email notifications table |
149 | * |
150 | * @param array $data Notification data |
151 | * @return array Cleaned notification data |
152 | */ |
153 | public static function clean_data(array $data) |
154 | { |
155 | $row = [ |
156 | 'notification_type_id' => null, |
157 | 'item_id' => null, |
158 | 'item_parent_id' => null, |
159 | 'user_id' => null, |
160 | ]; |
161 | |
162 | return array_intersect_key($data, $row); |
163 | } |
164 | } |