Completed
Push — master ( 5b173e...59e37d )
by Stanislav
08:42
created

main_controller::get_unread()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 14
c 4
b 1
f 0
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 9.7998
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 *
5
 * @package phpBB Extension - Active Notifications
6
 * @copyright (c) 2015 Lucifer <https://www.anavaro.com>
7
 * @copyright (c) 2016 kasimi <https://kasimi.net>
8
 * @license GNU General Public License, version 2 (GPL-2.0)
9
 *
10
 */
11
12
namespace anavaro\activenotifications\controller;
13
14
use phpbb\config\config;
0 ignored issues
show
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use phpbb\db\driver\driver_interface as db_interface;
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use phpbb\exception\http_exception;
0 ignored issues
show
Bug introduced by
The type phpbb\exception\http_exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use phpbb\notification\manager;
0 ignored issues
show
Bug introduced by
The type phpbb\notification\manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use phpbb\notification\type\type_interface;
0 ignored issues
show
Bug introduced by
The type phpbb\notification\type\type_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use phpbb\path_helper;
0 ignored issues
show
Bug introduced by
The type phpbb\path_helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use phpbb\request\request_interface;
0 ignored issues
show
Bug introduced by
The type phpbb\request\request_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use phpbb\template\template;
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use phpbb\user;
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
class main_controller
26
{
27
	/** @var user */
28
	protected $user;
29
30
	/** @var config */
31
	protected $config;
32
33
	/** @var request_interface */
34
	protected $request;
35
36
	/** @var manager */
37
	protected $notification_manager;
38
39
	/** @var string */
40
	protected $notifications_table;
41
42
	/** @var db_interface */
43
	protected $db;
44
45
	/** @var template */
46
	protected $template;
47
48
	/** @var path_helper */
49
	protected $path_helper;
50
51
	/**
52
	 * Constructor
53
	 *
54
	 * @param user				$user
55
	 * @param config			$config
56
	 * @param request_interface	$request
57
	 * @param manager			$notification_manager
58
	 * @param string			$notifications_table
59
	 * @param db_interface		$db
60
	 * @param template			$template
61
	 * @param path_helper		$path_helper
62
	 */
63 9
	public function __construct(
64
		user $user,
65
		config $config,
66
		request_interface $request,
67
		manager $notification_manager,
68
		$notifications_table,
69
		db_interface $db,
70
		template $template,
71
		path_helper $path_helper
72
	)
73
	{
74 9
		$this->user					= $user;
75 9
		$this->config				= $config;
76 9
		$this->request				= $request;
77 9
		$this->notification_manager	= $notification_manager;
78 9
		$this->notifications_table	= $notifications_table;
79 9
		$this->db					= $db;
80 9
		$this->template				= $template;
81 9
		$this->path_helper			= $path_helper;
82 9
	}
83
84
	/**
85
	 * @return JsonResponse
86
	 */
87 9
	public function base()
88
	{
89 9
		if ($this->user->data['user_id'] == ANONYMOUS || !$this->user->data['is_registered'] || $this->user->data['is_bot'] || !$this->request->is_ajax() || !$this->config['allow_board_notifications'])
0 ignored issues
show
Bug introduced by
The constant anavaro\activenotifications\controller\ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
90
		{
91 4
			throw new http_exception(403, 'NO_AUTH_OPERATION');
92
		}
93
94 5
		$last = $this->request->variable('last', 0);
95
96 5
		$notifications_content = '';
97 5
		$notifications = $this->get_unread($last);
98
99 5
		if (!empty($notifications['notifications']))
100
		{
101 3
			$this->template->assign_var('T_THEME_PATH', generate_board_url() . '/styles/' . rawurlencode($this->user->style['style_path']) . '/theme');
0 ignored issues
show
Bug introduced by
The function generate_board_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
			$this->template->assign_var('T_THEME_PATH', /** @scrutinizer ignore-call */ generate_board_url() . '/styles/' . rawurlencode($this->user->style['style_path']) . '/theme');
Loading history...
102
103 3
			foreach ($notifications['notifications'] as $notification)
104
			{
105 3
				$last = max($last, $notification->notification_id);
106
107
				/*
108
				 * Let's cheat a bit for tests sake
109
				 * We know that PHP 7.4.x has issue with bellow line and reports:
110
				 * "Creating default object from empty value"
111
				 * So we will depricate errors ONLY for the line that triggers it
112
				 */
113 3
				if (PHP_VERSION_ID > 70399)
114
				{
115
					$errorlevel=error_reporting();
116
					error_reporting(0);
117
				}
118
119
				/** @var type_interface $notification */
120 3
				$notification_for_display = $notification->prepare_for_display();
121
122 3
				if (PHP_VERSION_ID > 70399)
123
				{
124
					error_reporting($errorlevel);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $errorlevel does not seem to be defined for all execution paths leading up to this point.
Loading history...
125
				}
126 3
				$notification_for_display['URL'] = $this->relative_to_absolute_url($notification_for_display['URL']);
127 3
				$notification_for_display['U_MARK_READ'] = $this->relative_to_absolute_url($notification_for_display['U_MARK_READ']);
128
129 3
				$this->template->assign_block_vars('notifications', $notification_for_display);
130
			}
131
132 3
			$notifications_content = $this->render_template('notification_dropdown.html');
133
		}
134
135 5
		return new JsonResponse([
136 5
			'last'			=> $last,
137 5
			'unread'		=> $notifications['unread_count'],
138 5
			'notifications'	=> $notifications_content,
139
		]);
140
	}
141
142
	/**
143
	 * @param int $last
144
	 * @return array
145
	 */
146 5
	protected function get_unread($last)
147
	{
148
		$sql_array = [
149 5
			'SELECT'	=> 'n.notification_id',
150 5
			'FROM'		=> [$this->notifications_table => 'n'],
151 5
			'WHERE'		=> 'notification_id > ' . (int) $last . ' AND user_id = ' . (int) $this->user->data['user_id'],
152
		];
153
154 5
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
155 5
		$result = $this->db->sql_query($sql);
156 5
		$rows = $this->db->sql_fetchrowset($result);
157 5
		$this->db->sql_freeresult($result);
158
159 5
		$notifications_new = array_column($rows, 'notification_id');
160
161
		// Add non-existent notification so that no new notifications are returned
162 5
		if (!$notifications_new)
0 ignored issues
show
Bug Best Practice introduced by
The expression $notifications_new of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
163
		{
164 2
			$notifications_new[] = 0;
165
		}
166
167 5
		return $this->notification_manager->load_notifications('notification.method.board', [
168 5
			'notification_id'	=> $notifications_new,
169
			'count_unread'		=> true,
170
		]);
171
	}
172
173
	/**
174
	 * Renders a template file and returns it
175
	 *
176
	 * @param string $template_file
177
	 * @return string
178
	 */
179 3
	protected function render_template($template_file)
180
	{
181 3
		$this->template->set_filenames(['body' => $template_file]);
182 3
		$content = $this->template->assign_display('body', '', true);
183
184 3
		return trim(str_replace(["\r", "\n"], '', $content));
185
	}
186
187
	/**
188
	 * Removes all ../ from the beginning of the $url and prepends the board url.
189
	 *
190
	 * Example
191
	 *  in: "./../index.php"
192
	 *  out: "http://example-board.net/index.php"
193
	 *
194
	 * @param string $url
195
	 * @return string
196
	 */
197 3
	protected function relative_to_absolute_url($url)
198
	{
199 3
		if (!$url)
200
		{
201 1
			return '';
202
		}
203
204
		// Remove leading ../
205 3
		$url = $this->path_helper->remove_web_root_path($url);
206
207
		// Remove leading . if present
208 3
		if (strlen($url) && $url[0] === '.')
209
		{
210
			$url = substr($url, 1);
211
		}
212
213
		// Prepend / if not present
214 3
		if (strlen($url) && $url[0] !== '/')
215
		{
216 3
			$url = '/' . $url;
217
		}
218
219
		// Prepend board url
220 3
		$url = generate_board_url() . $url;
0 ignored issues
show
Bug introduced by
The function generate_board_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

220
		$url = /** @scrutinizer ignore-call */ generate_board_url() . $url;
Loading history...
221
222 3
		return $url;
223
	}
224
}
225