Completed
Push — master ( 59e37d...dbec74 )
by Stanislav
09:02 queued 14s
created

main_controller::base()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 18
c 3
b 2
f 0
dl 0
loc 35
ccs 19
cts 19
cp 1
rs 8.4444
cc 8
nc 3
nop 0
crap 8
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
				/** @var type_interface $notification */
108 3
				$notification_for_display = $notification->prepare_for_display();
109 3
				$notification_for_display['URL'] = $this->relative_to_absolute_url($notification_for_display['URL']);
110 3
				$notification_for_display['U_MARK_READ'] = $this->relative_to_absolute_url($notification_for_display['U_MARK_READ']);
111
112 3
				$this->template->assign_block_vars('notifications', $notification_for_display);
113
			}
114
115 3
			$notifications_content = $this->render_template('notification_dropdown.html');
116
		}
117
118 5
		return new JsonResponse([
119 5
			'last'			=> $last,
120 5
			'unread'		=> $notifications['unread_count'],
121 5
			'notifications'	=> $notifications_content,
122
		]);
123
	}
124
125
	/**
126
	 * @param int $last
127
	 * @return array
128
	 */
129 5
	protected function get_unread($last)
130
	{
131
		$sql_array = [
132 5
			'SELECT'	=> 'n.notification_id',
133 5
			'FROM'		=> [$this->notifications_table => 'n'],
134 5
			'WHERE'		=> 'notification_id > ' . (int) $last . ' AND user_id = ' . (int) $this->user->data['user_id'],
135
		];
136
137 5
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
138 5
		$result = $this->db->sql_query($sql);
139 5
		$rows = $this->db->sql_fetchrowset($result);
140 5
		$this->db->sql_freeresult($result);
141
142 5
		$notifications_new = array_column($rows, 'notification_id');
143
144
		// Add non-existent notification so that no new notifications are returned
145 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...
146
		{
147 2
			$notifications_new[] = 0;
148
		}
149
150 5
		return $this->notification_manager->load_notifications('notification.method.board', [
151 5
			'notification_id'	=> $notifications_new,
152
			'count_unread'		=> true,
153
		]);
154
	}
155
156
	/**
157
	 * Renders a template file and returns it
158
	 *
159
	 * @param string $template_file
160
	 * @return string
161
	 */
162 3
	protected function render_template($template_file)
163
	{
164 3
		$this->template->set_filenames(['body' => $template_file]);
165 3
		$content = $this->template->assign_display('body', '', true);
166
167 3
		return trim(str_replace(["\r", "\n"], '', $content));
168
	}
169
170
	/**
171
	 * Removes all ../ from the beginning of the $url and prepends the board url.
172
	 *
173
	 * Example
174
	 *  in: "./../index.php"
175
	 *  out: "http://example-board.net/index.php"
176
	 *
177
	 * @param string $url
178
	 * @return string
179
	 */
180 3
	protected function relative_to_absolute_url($url)
181
	{
182 3
		if (!$url)
183
		{
184 1
			return '';
185
		}
186
187
		// Remove leading ../
188 3
		$url = $this->path_helper->remove_web_root_path($url);
189
190
		// Remove leading . if present
191 3
		if (strlen($url) && $url[0] === '.')
192
		{
193
			$url = substr($url, 1);
194
		}
195
196
		// Prepend / if not present
197 3
		if (strlen($url) && $url[0] !== '/')
198
		{
199 3
			$url = '/' . $url;
200
		}
201
202
		// Prepend board url
203 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

203
		$url = /** @scrutinizer ignore-call */ generate_board_url() . $url;
Loading history...
204
205 3
		return $url;
206
	}
207
}
208