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