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 | use phpbb\user; |
18 | use phpbb\user_loader; |
19 | use phpbb\config\config; |
20 | use phpbb\db\driver\driver_interface; |
21 | use phpbb\di\service_collection; |
22 | use phpbb\messenger\method\messenger_interface; |
23 | |
24 | /** |
25 | * Email notification method class |
26 | * This class handles sending emails for notifications |
27 | */ |
28 | |
29 | class email extends \phpbb\notification\method\messenger_base |
30 | { |
31 | /** @var user */ |
32 | protected $user; |
33 | |
34 | /** @var config */ |
35 | protected $config; |
36 | |
37 | /** @var driver_interface */ |
38 | protected $db; |
39 | |
40 | /** @var string Notification emails table */ |
41 | protected $notification_emails_table; |
42 | |
43 | /** @var service_collection */ |
44 | protected $messenger; |
45 | |
46 | /** |
47 | * Notification Method email Constructor |
48 | * |
49 | * @param user_loader $user_loader |
50 | * @param user $user |
51 | * @param config $config |
52 | * @param driver_interface $db |
53 | * @param string $phpbb_root_path |
54 | * @param string $php_ext |
55 | * @param string $notification_emails_table |
56 | * @param service_collection $messenger |
57 | */ |
58 | public function __construct( |
59 | user_loader $user_loader, |
60 | user $user, |
61 | config $config, |
62 | driver_interface $db, |
63 | $phpbb_root_path, |
64 | $php_ext, |
65 | $notification_emails_table, |
66 | service_collection $messenger |
67 | ) |
68 | { |
69 | parent::__construct($messenger, $user_loader, $phpbb_root_path, $php_ext); |
70 | |
71 | $this->user = $user; |
72 | $this->config = $config; |
73 | $this->db = $db; |
74 | $this->notification_emails_table = $notification_emails_table; |
75 | } |
76 | |
77 | /** |
78 | * Get notification method name |
79 | * |
80 | * @return string |
81 | */ |
82 | public function get_type() |
83 | { |
84 | return 'notification.method.email'; |
85 | } |
86 | |
87 | /** |
88 | * Is this method available for the user? |
89 | * This is checked on the notifications options |
90 | * |
91 | * @param type_interface|null $notification_type An optional instance of a notification type. If provided, this |
92 | * method additionally checks if the type provides an email template. |
93 | * @return bool |
94 | */ |
95 | public function is_available(type_interface $notification_type = null) |
96 | { |
97 | return parent::is_available($notification_type) && $this->config['email_enable'] && !empty($this->user->data['user_email']); |
98 | } |
99 | |
100 | /** |
101 | * {@inheritdoc} |
102 | */ |
103 | public function get_notified_users($notification_type_id, array $options) |
104 | { |
105 | $notified_users = []; |
106 | |
107 | $sql = 'SELECT user_id |
108 | FROM ' . $this->notification_emails_table . ' |
109 | WHERE notification_type_id = ' . (int) $notification_type_id . |
110 | (isset($options['item_id']) ? ' AND item_id = ' . (int) $options['item_id'] : '') . |
111 | (isset($options['item_parent_id']) ? ' AND item_parent_id = ' . (int) $options['item_parent_id'] : '') . |
112 | (isset($options['user_id']) ? ' AND user_id = ' . (int) $options['user_id'] : ''); |
113 | $result = $this->db->sql_query($sql); |
114 | while ($row = $this->db->sql_fetchrow($result)) |
115 | { |
116 | $notified_users[$row['user_id']] = $row; |
117 | } |
118 | $this->db->sql_freeresult($result); |
119 | |
120 | return $notified_users; |
121 | } |
122 | |
123 | /** |
124 | * Parse the queue and notify the users |
125 | */ |
126 | public function notify() |
127 | { |
128 | $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->notification_emails_table); |
129 | |
130 | /** @var type_interface $notification */ |
131 | foreach ($this->queue as $notification) |
132 | { |
133 | $data = self::clean_data($notification->get_insert_array()); |
134 | $insert_buffer->insert($data); |
135 | } |
136 | |
137 | $insert_buffer->flush(); |
138 | |
139 | $this->notify_using_messenger(messenger_interface::NOTIFY_EMAIL); |
140 | } |
141 | |
142 | /** |
143 | * {@inheritdoc} |
144 | */ |
145 | public function mark_notifications($notification_type_id, $item_id, $user_id, $time = false, $mark_read = true) |
146 | { |
147 | $sql = 'DELETE FROM ' . $this->notification_emails_table . ' |
148 | 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') . |
149 | ($user_id !== false ? ' AND ' . $this->db->sql_in_set('user_id', $user_id) : '') . |
150 | ($item_id !== false ? ' AND ' . $this->db->sql_in_set('item_id', $item_id) : ''); |
151 | $this->db->sql_query($sql); |
152 | } |
153 | |
154 | /** |
155 | * {@inheritdoc} |
156 | */ |
157 | public function mark_notifications_by_parent($notification_type_id, $item_parent_id, $user_id, $time = false, $mark_read = true) |
158 | { |
159 | $sql = 'DELETE FROM ' . $this->notification_emails_table . ' |
160 | 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') . |
161 | ($user_id !== false ? ' AND ' . $this->db->sql_in_set('user_id', $user_id) : '') . |
162 | ($item_parent_id !== false ? ' AND ' . $this->db->sql_in_set('item_parent_id', $item_parent_id, false, true) : ''); |
163 | $this->db->sql_query($sql); |
164 | } |
165 | |
166 | /** |
167 | * Clean data to contain only what we need for email notifications table |
168 | * |
169 | * @param array $data Notification data |
170 | * @return array Cleaned notification data |
171 | */ |
172 | public static function clean_data(array $data) |
173 | { |
174 | $row = [ |
175 | 'notification_type_id' => null, |
176 | 'item_id' => null, |
177 | 'item_parent_id' => null, |
178 | 'user_id' => null, |
179 | ]; |
180 | |
181 | return array_intersect_key($data, $row); |
182 | } |
183 | } |