Completed
Push — develop ( ae80e4...ae707d )
by Daniel
10:44
created

admin_bar::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 11
dl 0
loc 13
ccs 12
cts 12
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services\blocks;
11
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
class admin_bar
15
{
16
	/** @var \phpbb\config\config */
17
	protected $config;
18
19
	/** @var \phpbb\controller\helper */
20
	protected $controller_helper;
21
22
	/** @var ContainerInterface */
23
	protected $phpbb_container;
24
25
	/** @var \phpbb\event\dispatcher_interface */
26
	protected $phpbb_dispatcher;
27
28
	/** @var \phpbb\template\template */
29
	protected $template;
30
31
	/** @var \phpbb\language\language */
32
	protected $translator;
33
34
	/** @var \phpbb\user */
35
	protected $user;
36
37
	/** @var \blitze\sitemaker\services\filemanager\setup */
38
	protected $filemanager;
39
40
	/** @var \blitze\sitemaker\services\icon_picker */
41
	protected $icons;
42
43
	/** @var \blitze\sitemaker\services\util */
44
	protected $util;
45
46
	/** @var string phpEx */
47
	protected $php_ext;
48
49
	/**
50
	 * Constructor
51
	 *
52
	 * @param \phpbb\config\config							$config					Config object
53
	 * @param \phpbb\controller\helper						$controller_helper		Controller Helper object
54
	 * @param ContainerInterface							$phpbb_container		Service container
55
	 * @param \phpbb\event\dispatcher_interface				$phpbb_dispatcher		Event dispatcher object
56
	 * @param \phpbb\template\template						$template				Template object
57
	 * @param \phpbb\language\language						$translator				Language object
58
	 * @param \phpbb\user									$user					User object
59
	 * @param \blitze\sitemaker\services\filemanager\setup	$filemanager			Filemanager object
60
	 * @param \blitze\sitemaker\services\icon_picker		$icons					Sitemaker icon picker object
61
	 * @param \blitze\sitemaker\services\util				$util					Sitemaker util object
62
	 * @param string										$php_ext				phpEx
63
	 */
64 12
	public function __construct(\phpbb\config\config $config, \phpbb\controller\helper $controller_helper, ContainerInterface $phpbb_container, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\template\template $template, \phpbb\language\language $translator, \phpbb\user $user, \blitze\sitemaker\services\filemanager\setup $filemanager, \blitze\sitemaker\services\icon_picker $icons, \blitze\sitemaker\services\util $util, $php_ext)
65
	{
66 12
		$this->config = $config;
67 12
		$this->controller_helper = $controller_helper;
68 12
		$this->phpbb_container = $phpbb_container;
69 12
		$this->phpbb_dispatcher = $phpbb_dispatcher;
70 12
		$this->template = $template;
71 12
		$this->translator = $translator;
72 12
		$this->user = $user;
73 12
		$this->filemanager = $filemanager;
74 12
		$this->icons = $icons;
75 12
		$this->util = $util;
76 12
		$this->php_ext = $php_ext;
77 12
	}
78
79
	/**
80
	 * Show admin bar
81
	 *
82
	 * @param array $route_info
83
	 */
84 2
	public function show(array $route_info)
85
	{
86 2
		$this->translator->add_lang('block_manager', 'blitze/sitemaker');
87
88 2
		$this->phpbb_container->get('blitze.sitemaker.auto_lang')->add('blocks_admin');
89
90 2
		$route = $route_info['route'];
91 2
		$style_id = $route_info['style'];
92
93 2
		$this->get_available_blocks();
94 2
		$this->get_startpage_options();
95 2
		$this->set_javascript_data($route, $style_id);
96 2
		$this->set_assets();
97
98 2
		$this->template->assign_vars(array(
99 2
			'S_EDIT_MODE'		=> true,
100 2
			'S_ROUTE_OPS'		=> $this->get_route_options($route),
101 2
			'S_HIDE_BLOCKS'		=> $route_info['hide_blocks'],
102 2
			'S_POSITION_OPS'	=> $this->get_excluded_position_options($route_info['ex_positions']),
103 2
			'S_EX_POSITIONS'	=> join(', ', $route_info['ex_positions']),
104 2
			'S_STYLE_OPTIONS'	=> style_select($style_id, true),
105 2
			'S_STARTPAGE'		=> $this->startpage_is_set(),
106
107 2
			'ICON_PICKER'		=> $this->icons->picker(),
108 2
		));
109 2
	}
110
111
	/**
112
	 * Set data used in javascript
113
	 * @param string $route
114
	 * @param int $style_id
115
	 */
116 4
	public function set_javascript_data($route, $style_id)
117
	{
118 4
		$board_url = generate_board_url();
119 4
		$is_default_route = $u_default_route = false;
120 4
		if ($this->config['sitemaker_default_layout'])
121 4
		{
122 1
			$is_default_route = ($this->config['sitemaker_default_layout'] === $route) ? true : false;
123 1
			$u_default_route .= $board_url . '/' . $this->config['sitemaker_default_layout'];
124 1
			$u_default_route = reapply_sid($u_default_route);
125 1
		}
126
127 4
		$this->template->assign_vars(array_merge(array(
128 4
			'S_IS_DEFAULT'		=> $is_default_route,
129
130 4
			'BLOCK_ACTIONS'		=> $this->get_block_actions(),
131 4
			'PAGE_URL'			=> build_url(array('style')),
132 4
133 4
			'UA_BOARD_URL'		=> $board_url,
134
			'UA_ROUTE'			=> $route,
135 4
			'UA_STYLE_ID'		=> $style_id,
136 4
			'UA_SCRIPT_PATH'	=> $this->user->page['root_script_path'],
137 4
			'UA_MODREWRITE'		=> $this->config['enable_mod_rewrite'],
138 4
			'UA_WEB_ROOT_PATH'	=> $this->util->get_web_path(),
139 4
			'UA_UPLOAD_URL'		=> $this->controller_helper->route('blitze_sitemaker_image_upload'),
140 4
141 4
			'U_VIEW_DEFAULT'	=> $u_default_route,
142
		), $this->get_filemanager_settings()));
0 ignored issues
show
Bug introduced by
$this->get_filemanager_settings() of type void is incompatible with the type null|array expected by parameter $array2 of array_merge(). ( Ignorable by Annotation )

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

142
		), /** @scrutinizer ignore-type */ $this->get_filemanager_settings()));
Loading history...
Bug introduced by
Are you sure the usage of $this->get_filemanager_settings() targeting blitze\sitemaker\service..._filemanager_settings() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
143 4
	}
144 4
145 4
	/**
146
	 * @return array
147
	 */
148
	protected function get_block_actions()
149
	{
150 4
		$list = array(
151
			'add_block',
152
			'copy_route',
153 4
			'edit_block',
154 4
			'handle_custom_action',
155 4
			'save_block',
156 4
			'save_blocks',
157 4
			'set_default_route',
158 4
			'set_route_prefs',
159 4
			'set_startpage',
160 4
			'update_block',
161 4
		);
162 4
163 4
		$actions = array();
164
		foreach ($list as $action)
165 4
		{
166 4
			$actions[$action] = $this->controller_helper->route('blitze_sitemaker_blocks_admin', array('action' => $action));
167
		}
168 4
169 4
		return $actions;
170
	}
171 4
172
	/**
173
	 * Get all available sitemaker blocks
174
	 */
175
	public function get_available_blocks()
176
	{
177 3
		$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks.factory')->get_all_blocks();
178
179 3
		foreach ($blocks as $service => $name)
180
		{
181 3
			$this->template->assign_block_vars('block', array(
182
				'NAME'		=> $name,
183 3
				'SERVICE'	=> $service)
184 3
			);
185 3
		}
186 3
	}
187 3
188 3
	/**
189
	 * Provide options to set/unset current page as landing page
190
	 */
191
	public function get_startpage_options()
192
	{
193 5
		$symfony_request = $this->phpbb_container->get('symfony_request');
194
		$controller = $symfony_request->attributes->get('_controller');
195 5
196 5
		if ($controller)
197
		{
198
			list($controller_service, $controller_method) = explode(':', $controller);
199 5
			$controller_params	= $symfony_request->attributes->get('_route_params');
200 2
			$controller_object	= $this->phpbb_container->get($controller_service);
201 2
			$controller_class	= get_class($controller_object);
202 2
203 2
			$r = new \ReflectionMethod($controller_class, $controller_method);
204
			$class_params = $r->getParameters();
205 2
206 2
			list($namespace, $extension) = explode('\\', $controller_class);
207
			$controller_arguments = $this->get_arguments($controller_params, $class_params);
208 2
209 2
			$this->template->assign_vars(array(
210
				'CONTROLLER_NAME'	=> $controller_service,
211 2
				'CONTROLLER_METHOD'	=> $controller_method,
212 2
				'CONTROLLER_PARAMS'	=> $controller_arguments,
213 2
				'S_IS_STARTPAGE'	=> $this->is_startpage($controller_service, $controller_arguments),
214 2
				'UA_EXTENSION'		=> $namespace . '/' . $extension,
215 2
			));
216 2
		}
217 2
	}
218 2
219 5
	/**
220
	 * Add js/css
221
	 */
222
	public function set_assets()
223
	{
224 2
		$assets = array(
225
			'js'	=> array(
226
				'@blitze_sitemaker/vendor/jquery-ui/jquery-ui.min.js',
227
				'@blitze_sitemaker/vendor/tinymce/tinymce.min.js',
228 2
				'@blitze_sitemaker/vendor/jqueryui-touch-punch/jquery.ui.touch-punch.min.js',
229 2
				'@blitze_sitemaker/vendor/twig.js/index.js',
230 2
				1000 =>  '@blitze_sitemaker/assets/blocks/manager.min.js',
231 2
			),
232 2
			'css'   => array(
233 2
				'@blitze_sitemaker/vendor/jquery-ui/themes/smoothness/jquery-ui.min.css',
234
				'@blitze_sitemaker/assets/blocks/manager.min.css',
235 2
			)
236 2
		);
237
238 2
		/**
239
		 * Event to set assets for available blocks
240
		 * Use array_merge_recursive to add new assets
241
		 *
242
		 * @event blitze.sitemaker.admin_bar.set_assets
243
		 * @var	array	assets		Array of assets to include of form array([js] => array(), [css] => array())
244
		 * @since 3.0.1-RC1
245
		 */
246
		$vars = array('assets');
247
		extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.admin_bar.set_assets', compact($vars)));
248 2
249 2
		$this->util->add_assets($assets);
250
	}
251 2
252 2
	/**
253
	 * Get routes with blocks
254
	 *
255
	 * @param string $current_route
256
	 * @return string
257
	 */
258
	public function get_route_options($current_route)
259
	{
260 4
		$routes_ary = $this->get_routes();
261
262 4
		$options = '<option value="">' . $this->translator->lang('SELECT') . '</option>';
263
		foreach ($routes_ary as $route)
264 4
		{
265 4
			$selected = ($route == $current_route) ? ' selected="selected"' : '';
266
			$options .= '<option value="' . $route . '"' . $selected . '>' . $route . '</option>';
267 4
		}
268 4
269 4
		return $options;
270
	}
271 4
272
	/**
273
	 * Get excluded position options
274
	 *
275
	 * @param array $ex_positions
276
	 * @return string
277
	 */
278
	public function get_excluded_position_options(array $ex_positions)
279
	{
280 4
		$options = '';
281
		foreach ($ex_positions as $position)
282 4
		{
283 4
			$options .= '<option value="' . $position . '" selected="selected">' . $position . '</option>';
284
		}
285 2
286 4
		return $options;
287
	}
288 4
289
	/**
290
	 * @return array
291
	 */
292
	protected function get_routes()
293
	{
294 4
		$factory = $this->phpbb_container->get('blitze.sitemaker.mapper.factory');
295
		$collection = $factory->create('routes')->find();
296 4
297 4
		$routes_ary = array();
298
		foreach ($collection as $entity)
299 4
		{
300 4
			/** @var \blitze\sitemaker\model\entity\route $entity */
301
			$route_name = $entity->get_route();
302
			$routes_ary[$route_name] = $route_name;
303 4
		}
304 4
305 4
		return $routes_ary;
306
	}
307 4
308
	/**
309
	 * @param array $controller_params
310
	 * @param array $class_params
311
	 * @return string
312
	 */
313
	protected function get_arguments(array $controller_params, array $class_params)
314
	{
315 2
		$arguments = array();
316
		foreach ($class_params as $param)
317 2
		{
318 2
			$name = $param->getName();
319
			$arguments[$name] = ($param->isOptional()) ? $param->getDefaultValue() : $controller_params[$name];
320 2
		}
321 2
322 2
		return join('/', $arguments);
323
	}
324 2
325
	/**
326
	 * @param string $controller_service
327
	 * @param string $controller_arguments
328
	 * @return bool
329
	 */
330
	protected function is_startpage($controller_service, $controller_arguments)
331
	{
332 2
		return ($this->config['sitemaker_startpage_controller'] == $controller_service && $this->config['sitemaker_startpage_params'] == $controller_arguments) ? true : false;
333
	}
334 2
335
	/**
336
	 * @return bool
337
	 */
338
	protected function startpage_is_set()
339
	{
340 2
		return ($this->config['sitemaker_startpage_controller']) ? true : false;
341
	}
342 2
343
	/**
344
	 * @return void
345
	 */
346
	protected function get_filemanager_settings()
347
	{
348
		$enabled = $this->filemanager->is_enabled();
349
350
		if ($enabled)
351
		{
352
			$this->filemanager->ensure_config_is_ready();
353
		}
354
355
		return array(
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('FILEMANAGE...ager->get_access_key()) returns the type array<string,string|boolean> which is incompatible with the documented return type void.
Loading history...
356
			'FILEMANAGER'		=> $enabled,
357
			'FILEMANAGER_AKEY'	=> $this->filemanager->get_access_key(),
358
		);
359
	}
360
}
361