Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
15.62% |
5 / 32 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| messenger_base | |
15.62% |
5 / 32 |
|
66.67% |
2 / 3 |
131.73 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| is_available | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| notify_using_messenger | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
132 | |||
| 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\di\service_collection; |
| 18 | use phpbb\user_loader; |
| 19 | |
| 20 | /** |
| 21 | * Abstract notification method handling messenger notifications |
| 22 | * using the phpBB messenger. |
| 23 | */ |
| 24 | abstract class messenger_base extends \phpbb\notification\method\base |
| 25 | { |
| 26 | /** @var service_collection */ |
| 27 | protected $messenger; |
| 28 | |
| 29 | /** @var user_loader */ |
| 30 | protected $user_loader; |
| 31 | |
| 32 | /** @var string */ |
| 33 | protected $phpbb_root_path; |
| 34 | |
| 35 | /** @var string */ |
| 36 | protected $php_ext; |
| 37 | |
| 38 | /** |
| 39 | * Notification Method Board Constructor |
| 40 | * |
| 41 | * @param service_collection $messenger |
| 42 | * @param user_loader $user_loader |
| 43 | * @param string $phpbb_root_path |
| 44 | * @param string $php_ext |
| 45 | */ |
| 46 | public function __construct(service_collection $messenger, user_loader $user_loader, $phpbb_root_path, $php_ext) |
| 47 | { |
| 48 | $this->messenger = $messenger; |
| 49 | $this->user_loader = $user_loader; |
| 50 | $this->phpbb_root_path = $phpbb_root_path; |
| 51 | $this->php_ext = $php_ext; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Is this method available for the user? |
| 56 | * This is checked on the notifications options |
| 57 | * |
| 58 | * @param type_interface|null $notification_type An optional instance of a notification type. This method returns false |
| 59 | * only if the type is provided and if it doesn't provide an email template. |
| 60 | * @return bool |
| 61 | */ |
| 62 | public function is_available(type_interface|null $notification_type = null) |
| 63 | { |
| 64 | return $notification_type === null || $notification_type->get_email_template() !== false; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Notify using phpBB messenger |
| 69 | * |
| 70 | * @param string $notify_method Notify method service for messenger (e.g. 'messenger.method.email'), empty string for all available methods |
| 71 | * @param string $template_dir_prefix Base directory to prepend to the email template name |
| 72 | * |
| 73 | * @return void |
| 74 | */ |
| 75 | protected function notify_using_messenger(string $notify_method, string $template_dir_prefix = ''): void |
| 76 | { |
| 77 | if (empty($this->queue)) |
| 78 | { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Load all users we want to notify (we need their email address) |
| 83 | $user_ids = []; |
| 84 | foreach ($this->queue as $notification) |
| 85 | { |
| 86 | $user_ids[] = $notification->user_id; |
| 87 | } |
| 88 | |
| 89 | // We do not notify banned users |
| 90 | if (!function_exists('phpbb_get_banned_user_ids')) |
| 91 | { |
| 92 | include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); |
| 93 | } |
| 94 | $banned_users = phpbb_get_banned_user_ids($user_ids); |
| 95 | |
| 96 | // Load all the users we need |
| 97 | $this->user_loader->load_users(array_diff($user_ids, $banned_users), array(USER_IGNORE)); |
| 98 | |
| 99 | // Time to go through the queue and send notifications |
| 100 | $messenger_collection_iterator = $this->messenger->getIterator(); |
| 101 | |
| 102 | /** @var type_interface $notification */ |
| 103 | foreach ($this->queue as $notification) |
| 104 | { |
| 105 | if ($notification->get_email_template() === false) |
| 106 | { |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | $user = $this->user_loader->get_user($notification->user_id); |
| 111 | |
| 112 | if ($user['user_type'] == USER_INACTIVE && $user['user_inactive_reason'] == INACTIVE_MANUAL) |
| 113 | { |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @var \phpbb\messenger\method\messenger_interface $messenger_method |
| 119 | * @psalm-suppress UndefinedMethod |
| 120 | */ |
| 121 | foreach ($messenger_collection_iterator as $messenger_method) |
| 122 | { |
| 123 | if (empty($notify_method) || $messenger_collection_iterator->key() == $notify_method) |
| 124 | { |
| 125 | $messenger_method->template($notification->get_email_template(), $user['user_lang'], '', $template_dir_prefix); |
| 126 | $messenger_method->set_addresses($user); |
| 127 | $messenger_method->assign_vars(array_merge([ |
| 128 | 'USERNAME' => $user['username'], |
| 129 | 'U_NOTIFICATION_SETTINGS' => generate_board_url() . '/ucp.' . $this->php_ext . '?i=ucp_notifications&mode=notification_options', |
| 130 | ], $notification->get_email_template_variables())); |
| 131 | |
| 132 | $messenger_method->send(); |
| 133 | |
| 134 | // Save the queue in the messenger method class (has to be called or these messages could be lost) |
| 135 | $messenger_method->save_queue(); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // We're done, empty the queue |
| 141 | $this->empty_queue(); |
| 142 | } |
| 143 | } |