manager   B
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 381
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 93.42%

Importance

Changes 0
Metric Value
dl 0
loc 381
ccs 142
cts 152
cp 0.9342
rs 8.6
c 0
b 0
f 0
wmc 37
lcom 1
cbo 6

14 Methods

Rating   Name   Duplication   Size   Complexity  
A set_u_action() 0 7 1
A set_acp_class() 0 6 1
A get_module() 0 15 2
A handle_ajax_request() 0 8 2
B reset_module() 0 39 4
A get_move_module_data() 0 4 1
A handle_after_move() 0 14 3
A get_last_module_order() 0 16 3
B move_module_vertical() 0 14 6
A move_module_horizontal() 0 13 1
B get_horizontal_move_action() 0 16 5
A get_module_link() 0 4 2
A __construct() 0 12 1
B module_delete() 0 54 5
1
<?php
2
/**
3
 *
4
 * @package Board3 Portal v2.1
5
 * @copyright (c) 2014 Board3 Group ( www.board3.de )
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace board3\portal\portal\modules;
11
12
use board3\portal\includes\helper;
13
use board3\portal\modules\module_interface;
14
use board3\portal\portal\columns;
15
use phpbb\db\driver\driver_interface;
16
use phpbb\request\request_interface;
17
18
class manager
19
{
20
	/** @var \phpbb\cache\service */
21
	protected $cache;
22
23
	/** @var \board3\portal\controller\helper */
24
	protected $controller_helper;
25
26
	/** @var \board3\portal\portal\columns */
27
	protected $portal_columns;
28
29
	/** @var \board3\portal\includes\helper */
30
	protected $portal_helper;
31
32
	/** @var \board3\portal\portal\modules\constraints_handler */
33
	protected $constraints_handler;
34
35
	/** @var \board3\portal\portal\modules\database_handler */
36
	protected $database_handler;
37
38
	/** @var \phpbb\db\driver\driver_interface */
39
	protected $db;
40
41
	/** @var \phpbb\request\request_interface */
42
	protected $request;
43
44
	/** @var \phpbb\user */
45
	protected $user;
46
47
	/** @var \board3\portal\modules\module_interface */
48
	protected $module;
49
50
	/** @var string u_action of acp module */
51
	protected $u_action;
52
53
	/** @var string class of acp module */
54
	protected $acp_class;
55
56
	/**
57
	 * Constructor for modules manager
58
	 *
59
	 * @param \phpbb\cache\service $cache phpBB cache
60
	 * @param \phpbb\db\driver\driver_interface $db Database driver
61
	 * @param \board3\portal\controller\helper $controller_helper Board3 Portal controller helper
62
	 * @param \board3\portal\portal\columns $portal_columns Portal columns helper
63
	 * @param \board3\portal\includes\helper $portal_helper Portal helper
64
	 * @param \board3\portal\portal\modules\constraints_handler $constraints_handler Modules constraints handler
65
	 * @param \board3\portal\portal\modules\database_handler $database_handler Modules database handler
66
	 * @param \phpbb\request\request_interface $request phpBB request
67
	 * @param \phpbb\user $user phpBB user
68
	 */
69 43
	public function __construct($cache, driver_interface $db, \board3\portal\controller\helper $controller_helper, columns $portal_columns, helper $portal_helper, constraints_handler $constraints_handler, database_handler $database_handler, request_interface $request, $user)
70
	{
71 43
		$this->cache = $cache;
72 43
		$this->db = $db;
73 43
		$this->controller_helper = $controller_helper;
74 43
		$this->portal_columns = $portal_columns;
75 43
		$this->portal_helper = $portal_helper;
76 43
		$this->constraints_handler = $constraints_handler;
77 43
		$this->database_handler = $database_handler;
78 43
		$this->request = $request;
79 43
		$this->user = $user;
80 43
	}
81
82
	/**
83
	 * Set u_action for module
84
	 *
85
	 * @param string $u_action u_action for module
86
	 *
87
	 * @return \board3\portal\portal\modules\manager This class
88
	 */
89 7
	public function set_u_action($u_action)
90
	{
91 7
		$this->u_action = $u_action;
92 7
		$this->constraints_handler->set_u_action($u_action);
93
94 7
		return $this;
95
	}
96
97
	/**
98
	 * Set acp module class
99
	 *
100
	 * @param string $acp_class ACP module class
101
	 *
102
	 * @return \board3\portal\portal\modules\manager This class
103
	 */
104 7
	public function set_acp_class($acp_class)
105
	{
106 7
		$this->acp_class = $acp_class;
107
108 7
		return $this;
109
	}
110
111
	/**
112
	 * Get module object
113
	 *
114
	 * @param string $class_name Module class name
115
	 * @return null
116
	 */
117 14
	protected function get_module($class_name)
118
	{
119 14
		$module = $this->portal_helper->get_module($class_name);
120
121 14
		if (!$module instanceof module_interface)
122 14
		{
123 2
			trigger_error('CLASS_NOT_FOUND', E_USER_ERROR);
124
		}
125
		else
126
		{
127 12
			$this->module = $module;
128
		}
129
130 12
		unset($module);
131 12
	}
132
133
	/**
134
	 * Handle ajax request.
135
	 * Method will return supplied data if request is an ajax request
136
	 *
137
	 * @param array $data Data to send
138
	 *
139
	 * @return null
140
	 */
141 11
	public function handle_ajax_request($data)
142
	{
143 11
		if ($this->request->is_ajax())
144 11
		{
145
			$json_response = new \phpbb\json_response;
146
			$json_response->send($data);
147
		}
148 11
	}
149
150
	/**
151
	 * Reset module settings to default options
152
	 *
153
	 * @param int $id ID of the acp_portal module
154
	 * @param string|int $mode Mode of the acp_portal module
155
	 * @param int $module_id ID of the module that should be reset
156
	 * @param array $module_data Array containing the module's database row
157
	 */
158 1
	public function reset_module($id, $mode, $module_id, $module_data)
159
	{
160 1
		if (confirm_box(true))
161 1
		{
162 1
			$module_data = $this->get_move_module_data($module_id);
163
164 1
			$this->get_module($module_data['module_classname']);
165
166 1
			$affected_rows = $this->database_handler->reset_module($this->module, $module_id);
167
168 1
			if (empty($affected_rows))
169 1
			{
170
				// We need to return to the module config
171
				meta_refresh(3, $this->get_module_link('config', $module_id));
172
				trigger_error($this->user->lang['MODULE_NOT_EXISTS'] . adm_back_link($this->u_action . "&amp;module_id=$module_id"), E_USER_WARNING);
173
			}
174
175 1
			$this->cache->destroy('config');
176 1
			$this->cache->destroy('portal_config');
177 1
			obtain_portal_config(); // we need to prevent duplicate entry errors
178 1
			$this->module->install($module_id);
179 1
			$this->cache->purge();
180
181
			// We need to return to the module config
182 1
			meta_refresh(3, $this->get_module_link('config', $module_id));
183
184 1
			trigger_error($this->user->lang['MODULE_RESET_SUCCESS'] . adm_back_link($this->u_action . "&amp;module_id=$module_id"));
185 1
		}
186
		else
187
		{
188 1
			$confirm_text = (isset($this->user->lang[$module_data['module_name']])) ? sprintf($this->user->lang['MODULE_RESET_CONFIRM'], $this->user->lang[$module_data['module_name']]) : sprintf($this->user->lang['DELETE_MODULE_CONFIRM'], utf8_normalize_nfc($module_data['module_name']));
189 1
			confirm_box(false, $confirm_text, build_hidden_fields(array(
190 1
				'i'				=> $id,
191 1
				'mode'			=> $mode,
192 1
				'module_reset'	=> true,
193 1
				'module_id'		=> $module_id,
194 1
			)));
195
		}
196 1
	}
197
198
	/**
199
	 * Get module_data required for moving it
200
	 *
201
	 * @param int	$module_id	ID of the module that should be moved
202
	 * @return array|null		Module_data or empty if not successful
203
	 */
204 18
	public function get_move_module_data($module_id)
205
	{
206 18
		return $this->database_handler->get_module_data($module_id);
207
	}
208
209
	/**
210
	 * Handle output after moving module
211
	 *
212
	 * @param bool $success	Whether moving module was successful
213
	 * @param bool $is_row	Whether the module move was inside a row
214
	 * @return void
215
	 */
216 14
	public function handle_after_move($success = true, $is_row = false)
217
	{
218 14
		if (!$success)
219 14
		{
220 13
			trigger_error($this->user->lang['UNABLE_TO_MOVE' . (($is_row) ? '_ROW' : '')] . adm_back_link($this->u_action));
221
		}
222
223 9
		$this->cache->destroy('sql', PORTAL_MODULES_TABLE);
224
225
		// Handle ajax requests
226 9
		$this->handle_ajax_request(array('success' => true));
227
228 9
		redirect($this->u_action); // redirect in order to get rid of excessive URL parameters
229 9
	}
230
231
	/**
232
	 * Get the module order to the last module in the column
233
	 *
234
	 * @param int $module_column	Module column to check
235
	 * @return int Module order of the last module in the column
236
	 */
237 5
	public function get_last_module_order($module_column)
238
	{
239 5
		$modules = obtain_portal_modules();
240 5
		$last_order = 1;
241 5
		foreach ($modules as $cur_module)
242
		{
243 5
			if ($cur_module['module_column'] != $module_column)
244 5
			{
245 5
				continue;
246
			}
247
248 5
			$last_order = max($last_order, $cur_module['module_order']);
249 5
		}
250
251 5
		return $last_order;
252
	}
253
254
	/**
255
	 * Move module vertically
256
	 *
257
	 * @param int $module_id Module ID
258
	 * @param int $direction Direction of move, either -1 for up or 1 for down
259
	 */
260 2
	public function move_module_vertical($module_id, $direction)
261
	{
262 2
		$module_data = $this->get_move_module_data($module_id);
263
264 2
		if ($module_data === false || ($direction == database_handler::MOVE_DIRECTION_UP && $module_data['module_order'] <= 1) ||
265 2
			($direction == database_handler::MOVE_DIRECTION_DOWN && $this->get_last_module_order($module_data['module_column']) == $module_data['module_order']))
266 2
		{
267 2
			$this->handle_after_move(false, true);
268
		}
269
		else
270
		{
271 2
			$this->handle_after_move($this->database_handler->move_module_vertical($module_id, $module_data, $direction, 1), true);
0 ignored issues
show
Bug introduced by
It seems like $module_data defined by $this->get_move_module_data($module_id) on line 262 can also be of type null; however, board3\portal\portal\mod...:move_module_vertical() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
272
		}
273 2
	}
274
275
	/**
276
	 * Move module horizontally
277
	 *
278
	 * @param int $module_id ID of the module that should be moved
279
	 * @param int $direction The direction to move the module
280
	 *
281
	 * @return null
282
	 */
283 12
	public function move_module_horizontal($module_id, $direction)
284
	{
285 12
		$module_data = $this->get_move_module_data($module_id);
286
287 12
		$this->get_module($module_data['module_classname']);
288
289 10
		$move_action = $this->get_horizontal_move_action($module_data, $direction);
290 8
		$this->constraints_handler->check_module_conflict($module_data, $move_action);
291
292 6
		$this->database_handler->move_module_horizontal($module_id, $module_data, $move_action);
293
294 6
		$this->handle_after_move(true);
295 6
	}
296
297
	/**
298
	 * Get the horizontal move action (columns to move)
299
	 *
300
	 * @param array $module_data Array containing the module data
301
	 * @param int $direction Direction to move; 1 for right, -1 for left
302
	 *
303
	 * @return int|null Move action if module can be moved, calls
304
	 *		handle_after_move() if it can't be moved
305
	 */
306 11
	public function get_horizontal_move_action($module_data, $direction)
307
	{
308 11
		if ($this->constraints_handler->can_move_horizontally($module_data, $direction))
309 11
		{
310 10
			if ($this->module->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($module_data['module_column'] + $direction)))
311 10
			{
312 6
				return $direction; // we move 1 column
313
			}
314 4
			else if ($this->module->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($module_data['module_column'] + $direction * 2)) && $module_data['module_column'] != $this->portal_columns->string_to_number('center'))
315 4
			{
316 2
				return 2 * $direction; // we move 2 columns
317
			}
318 2
		}
319
320 9
		$this->handle_after_move(false);
321
	}
322
323
	/**
324
	 * Delete module
325
	 *
326
	 * @param int|string $id Module ID of the acp_portal module
327
	 * @param string $mode Mode of the acp_portal module
328
	 * @param string $action Current action of the acp_portal module
329
	 * @param int $module_id ID of the module that should be deleted
330
	 */
331 1
	public function module_delete($id, $mode, $action, $module_id)
332
	{
333 1
		$module_data = $this->get_move_module_data($module_id);
334
335 1
		if ($module_data !== false)
336 1
		{
337 1
			$module_classname = $this->request->variable('module_classname', '');
338
339 1
			$this->get_module($module_data['module_classname']);
340
341 1
			if (confirm_box(true))
342 1
			{
343 1
				$this->module->uninstall($module_data['module_id'], $this->db);
344
345 1
				$sql = 'DELETE FROM ' . PORTAL_MODULES_TABLE . '
346 1
					WHERE module_id = ' . (int) $module_id;
347 1
				$this->db->sql_query($sql);
348
349 1
				$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
350
					SET module_order = module_order - 1
351 1
					WHERE module_column = ' . (int) $module_data['module_column'] . '
352 1
						AND module_order > ' . (int) $module_data['module_order'];
353 1
				$this->db->sql_query($sql);
354
355 1
				$this->cache->purge(); // make sure we don't get errors after re-adding a module
356
357
				// Handle ajax request
358 1
				$this->handle_ajax_request(array(
359 1
					'success' => true,
360 1
					'MESSAGE_TITLE'	=> $this->user->lang['INFORMATION'],
361 1
					'MESSAGE_TEXT'	=> $this->user->lang['SUCCESS_DELETE'],
362 1
				));
363
364 1
				trigger_error($this->user->lang['SUCCESS_DELETE'] . adm_back_link($this->u_action));
365 1
			}
366
			else
367
			{
368 1
				if ($this->module->get_language())
369 1
				{
370 1
					$this->controller_helper->load_module_language($this->module);
371 1
				}
372 1
				$confirm_text = (isset($this->user->lang[$module_data['module_name']])) ? sprintf($this->user->lang['DELETE_MODULE_CONFIRM'], $this->user->lang[$module_data['module_name']]) : sprintf($this->user->lang['DELETE_MODULE_CONFIRM'], utf8_normalize_nfc($module_data['module_name']));
373 1
				confirm_box(false, $confirm_text, build_hidden_fields(array(
374 1
					'i'					=> $id,
375 1
					'mode'				=> $mode,
376 1
					'action'			=> $action,
377 1
					'module_id'			=> $module_id,
378 1
					'module_classname'	=> $module_classname,
379 1
				)));
380
			}
381 1
		}
382
383 1
		$this->cache->destroy('sql', PORTAL_MODULES_TABLE);
384 1
	}
385
386
	/**
387
	 * Get link to module settings with specified ID and portal_module mode
388
	 *
389
	 * @param string $mode portal_module mode
390
	 * @param int $module_id Module ID
391
	 *
392
	 * @return string Link to module settings
393
	 */
394 4
	public function get_module_link($mode, $module_id)
395
	{
396 4
		return preg_replace(array('/i=[0-9]+/', '/mode=[a-zA-Z0-9_]+/'), array('i=-' . str_replace('\\', '-', $this->acp_class), 'mode=' . $mode), $this->u_action) . (($module_id) ? '&amp;module_id=' . $module_id : '');
397
	}
398
}
399