Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
15.15% covered (danger)
15.15%
5 / 33
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
messenger_base
15.15% covered (danger)
15.15%
5 / 33
66.67% covered (warning)
66.67%
2 / 3
152.44
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 is_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 notify_using_messenger
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
156
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
14namespace phpbb\notification\method;
15
16use phpbb\notification\type\type_interface;
17use phpbb\di\service_collection;
18use phpbb\user_loader;
19
20/**
21* Abstract notification method handling email and jabber notifications
22* using the phpBB messenger.
23*/
24abstract 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 $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 int $notify_method                Notify method for messenger (e.g. \phpbb\messenger\method\messenger_interface::NOTIFY_IM)
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($notify_method, $template_dir_prefix = '')
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            foreach ($messenger_collection_iterator as $messenger_method)
118            {
119                if ($messenger_method->get_id() == $notify_method || $notify_method == $messenger_method::NOTIFY_BOTH)
120                {
121                    $messenger_method->template($notification->get_email_template(), $user['user_lang'], '', $template_dir_prefix);
122                    $messenger_method->set_addresses($user);
123                    $messenger_method->assign_vars(array_merge([
124                        'USERNAME'                    => $user['username'],
125                        'U_NOTIFICATION_SETTINGS'    => generate_board_url() . '/ucp.' . $this->php_ext . '?i=ucp_notifications&mode=notification_options',
126                    ], $notification->get_email_template_variables()));
127
128                    $messenger_method->send();
129                }
130            }
131        }
132
133        // Save the queue in the messenger method class (has to be called or these messages could be lost)
134        foreach ($messenger_collection_iterator as $messenger_method)
135        {
136            $messenger_method->save_queue();
137        }
138
139        // We're done, empty the queue
140        $this->empty_queue();
141    }
142}