admin_bar::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 10
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 1
rs 9.9332
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
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\services\blocks;
12
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
class admin_bar
16
{
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\controller\helper */
21
	protected $controller_helper;
22
23
	/** @var ContainerInterface */
24
	protected $phpbb_container;
25
26
	/** @var \phpbb\event\dispatcher_interface */
27
	protected $phpbb_dispatcher;
28
29
	/** @var \phpbb\template\template */
30
	protected $template;
31
32
	/** @var \phpbb\language\language */
33
	protected $translator;
34
35
	/** @var \phpbb\user */
36
	protected $user;
37
38
	/** @var \blitze\sitemaker\services\icons\picker */
39
	protected $icons;
40
41
	/** @var \blitze\sitemaker\services\util */
42
	protected $util;
43
44
	/** @var array */
45
	protected $tinymce_lang_mapping;
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param \phpbb\config\config							$config					Config object
51
	 * @param \phpbb\controller\helper						$controller_helper		Controller Helper object
52
	 * @param ContainerInterface							$phpbb_container		Service container
53
	 * @param \phpbb\event\dispatcher_interface				$phpbb_dispatcher		Event dispatcher object
54
	 * @param \phpbb\template\template						$template				Template object
55
	 * @param \phpbb\language\language						$translator				Language object
56
	 * @param \phpbb\user									$user					User object
57
	 * @param \blitze\sitemaker\services\icons\picker		$icons					Sitemaker icon picker object
58
	 * @param \blitze\sitemaker\services\util				$util					Sitemaker util object
59
	 * @param array											$lang_mapping			Lang mapping array for tinymce
60
	 */
61
	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\icons\picker $icons, \blitze\sitemaker\services\util $util, $lang_mapping)
62
	{
63
		$this->config = $config;
64 12
		$this->controller_helper = $controller_helper;
65
		$this->phpbb_container = $phpbb_container;
66 12
		$this->phpbb_dispatcher = $phpbb_dispatcher;
67 12
		$this->template = $template;
68 12
		$this->translator = $translator;
69 12
		$this->user = $user;
70 12
		$this->icons = $icons;
71 12
		$this->util = $util;
72 12
		$this->tinymce_lang_mapping = $lang_mapping;
73 12
	}
74 12
75 12
	/**
76 12
	 * Show admin bar
77 12
	 *
78
	 * @param array $route_info
79
	 */
80
	public function show(array $route_info)
81
	{
82
		$this->translator->add_lang(['block_manager', 'navbar_manager'], 'blitze/sitemaker');
83
84 2
		$this->phpbb_container->get('blitze.sitemaker.auto_lang')->add('blocks_admin');
85
86 2
		$route = $route_info['route'];
87
		$style_id = $route_info['style'];
88 2
		$navigation = $this->phpbb_container->get('blitze.sitemaker.menus.navigation');
89
90 2
		$this->get_available_blocks();
91 2
		$this->get_startpage_options();
92
		$this->set_javascript_data($route, $style_id);
93 2
		$this->set_assets();
94 2
95 2
		$this->template->assign_vars(array(
96 2
			'S_EDIT_MODE'		=> true,
97
			'S_ROUTE_OPS'		=> $this->get_route_options($route),
98 2
			'S_HIDE_BLOCKS'		=> $route_info['hide_blocks'],
99 2
			'S_EX_POSITIONS'	=> join(', ', $route_info['ex_positions']),
100 2
			'S_STYLE_OPTIONS'	=> style_select($style_id, true),
101 2
			'S_STARTPAGE'		=> $this->startpage_is_set(),
102 2
103 2
			'S_NAVBAR_MENU'		=> $this->config['sm_navbar_menu'],
104 2
			'S_MENU_OPTIONS'	=> $navigation->get_menu_options(),
105 2
			'U_MANAGE_MENUS'	=> $navigation->get_menus_admin_url(),
106
107 2
			'ICON_PICKER'		=> $this->icons->picker(),
108 2
			'SM_USER_LANG'		=> $this->user->data['user_lang'],
109 2
			'TINYMCE_LANG'		=> $this->get_tinymce_lang(),
110
		));
111
	}
112
113
	/**
114
	 * Set data used in javascript
115
	 * @param string $route
116 4
	 * @param int $style_id
117
	 */
118 4
	public function set_javascript_data($route, $style_id)
119 4
	{
120 4
		$board_url = generate_board_url();
121 4
		$is_default_route = $u_default_route = false;
122 1
		if ($this->config['sitemaker_default_layout'])
123 1
		{
124 1
			$u_default_route = $this->get_default_route_url($is_default_route, $style_id, $route, $board_url);
125 1
		}
126
127 4
		$this->template->assign_vars(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_file_upload'),
140 4
			'UA_NAVBAR_MANAGER'	=> $this->controller_helper->route('blitze_sitemaker_navbar_manager', array(
141 4
				'style'	=> $this->template->get_user_style()[0]
142
			)),
143 4
144 4
			'U_VIEW_DEFAULT'	=> $u_default_route,
145 4
		));
146
	}
147
148
	/**
149
	 * @param bool $is_default_route
150 4
	 * @param int $style_id
151
	 * @param string $route
152
	 * @param string $board_url
153 4
	 * @return string
154 4
	 */
155 4
	protected function get_default_route_url(&$is_default_route, $style_id, $route, $board_url)
156 4
	{
157 4
		[$df_route, $df_style_id] = array_filter(explode(':', $this->config['sitemaker_default_layout'])) + array('', $style_id);
158 4
		$df_style_id = (int) $df_style_id;
159 4
160 4
		$is_default_route = ([$df_route, $df_style_id] === [$route, $style_id]) ? true : false;
161 4
		$u_default_route = $board_url . '/' . $df_route;
162 4
163 4
		if ($df_style_id !== $style_id)
164
		{
165 4
			$u_default_route .= (strpos($df_route, '?') !== false ? '&amp;' : '?') . 'style=' . $df_style_id;
166 4
		}
167
168 4
		return reapply_sid($u_default_route);
169 4
	}
170
171 4
	/**
172
	 * @return string
173
	 */
174
	protected function get_tinymce_lang()
175
	{
176
		$user_lang = $this->user->data['user_lang'];
177 3
178
		if (isset($this->tinymce_lang_mapping[$user_lang]))
179 3
		{
180
			return $this->tinymce_lang_mapping[$user_lang];
181 3
		}
182
		else
183 3
		{
184 3
			return preg_replace_callback('/_([a-z0-9]+)/i', function ($parts)
185 3
			{
186 3
				return strtoupper($parts[0]);
187 3
			}, $user_lang);
188 3
		}
189
	}
190
191
	/**
192
	 * @return array
193 5
	 */
194
	protected function get_block_actions()
195 5
	{
196 5
		$list = array(
197
			'add_block',
198
			'copy_route',
199 5
			'edit_block',
200 2
			'handle_custom_action',
201 2
			'save_block',
202 2
			'save_blocks',
203 2
			'set_default_route',
204
			'set_route_prefs',
205 2
			'set_startpage',
206 2
			'update_block',
207
			'update_column_width',
208 2
		);
209 2
210
		$actions = array();
211 2
		foreach ($list as $action)
212 2
		{
213 2
			$actions[$action] = $this->controller_helper->route('blitze_sitemaker_blocks_admin', array('action' => $action));
214 2
		}
215 2
216 2
		return $actions;
217 2
	}
218 2
219 5
	/**
220
	 * Get all available sitemaker blocks
221
	 */
222
	public function get_available_blocks()
223
	{
224 2
		$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks.factory')->get_all_blocks();
225
226
		foreach ($blocks as $service => $name)
227
		{
228 2
			$this->template->assign_block_vars('block', array(
229 2
				'NAME'		=> $name,
230 2
				'SERVICE'	=> $service
231 2
			));
232 2
		}
233 2
	}
234
235 2
	/**
236 2
	 * Provide options to set/unset current page as landing page
237
	 */
238 2
	public function get_startpage_options()
239
	{
240
		$symfony_request = $this->phpbb_container->get('symfony_request');
241
		$controller = $symfony_request->attributes->get('_controller');
242
243
		if ($controller)
244
		{
245
			list($controller_service, $controller_method) = explode(':', $controller);
246
			$controller_params	= $symfony_request->attributes->get('_route_params');
247
			$controller_object	= $this->phpbb_container->get($controller_service);
248 2
			$controller_class	= get_class($controller_object);
249 2
250
			$r = new \ReflectionMethod($controller_class, $controller_method);
251 2
			$class_params = $r->getParameters();
252 2
253
			list($namespace, $extension) = explode('\\', $controller_class);
254
			$controller_arguments = $this->get_arguments($controller_params, $class_params);
255
256
			$this->template->assign_vars(array(
257
				'CONTROLLER_NAME'	=> $controller_service,
258
				'CONTROLLER_METHOD'	=> $controller_method,
259
				'CONTROLLER_PARAMS'	=> $controller_arguments,
260 4
				'S_IS_STARTPAGE'	=> $this->is_startpage($controller_service, $controller_arguments),
261
				'UA_EXTENSION'		=> $namespace . '/' . $extension,
262 4
			));
263
		}
264 4
	}
265 4
266
	/**
267 4
	 * Add js/css
268 4
	 */
269 4
	public function set_assets()
270
	{
271 4
		$assets = array(
272
			'css'	=> array(
273
				'@blitze_sitemaker/assets/blocks/manager.min.css',
274
			),
275
			'js'	=> array(
276
				'@blitze_sitemaker/assets/runtime.min.js',
277
				'@blitze_sitemaker/assets/tinymce/tinymce.min.js',
278
				'@blitze_sitemaker/assets/blocks/manager.min.js',
279
			)
280 4
		);
281
282 4
		/**
283 4
		 * Event to set assets for available blocks
284
		 * Use array_merge_recursive to add new assets
285 2
		 *
286 4
		 * @event blitze.sitemaker.admin_bar.set_assets
287
		 * @var	array	assets		Array of assets to include of form array([js] => array(), [css] => array())
288 4
		 * @since 3.0.1-RC1
289
		 */
290
		$vars = array('assets');
291
		extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.admin_bar.set_assets', compact($vars)));
292
293
		$this->util->add_assets($assets);
294 4
	}
295
296 4
	/**
297 4
	 * Get routes with blocks
298
	 *
299 4
	 * @param string $current_route
300 4
	 * @return string
301
	 */
302
	public function get_route_options($current_route)
303 4
	{
304 4
		$routes_ary = $this->get_routes();
305 4
306
		$options = '<option value="">' . $this->translator->lang('SELECT') . '</option>';
307 4
		foreach ($routes_ary as $route)
308
		{
309
			$selected = ($route == $current_route) ? ' selected="selected"' : '';
310
			$options .= '<option value="' . $route . '"' . $selected . '>' . $route . '</option>';
311
		}
312
313
		return $options;
314
	}
315 2
316
	/**
317 2
	 * @return array
318 2
	 */
319
	protected function get_routes()
320 2
	{
321 2
		$factory = $this->phpbb_container->get('blitze.sitemaker.mapper.factory');
322 2
		$collection = $factory->create('routes')->find();
323
324 2
		$routes_ary = array();
325
		foreach ($collection as $entity)
326
		{
327
			/** @var \blitze\sitemaker\model\entity\route $entity */
328
			$route_name = $entity->get_route();
329
			$routes_ary[$route_name] = $route_name;
330
		}
331
332 2
		return $routes_ary;
333
	}
334 2
335
	/**
336
	 * @param array $controller_params
337
	 * @param array $class_params
338
	 * @return string
339
	 */
340 2
	protected function get_arguments(array $controller_params, array $class_params)
341
	{
342 2
		$arguments = array();
343
		foreach ($class_params as $param)
344
		{
345
			$name = $param->getName();
346
			$arguments[$name] = ($param->isOptional()) ? $param->getDefaultValue() : $controller_params[$name];
347
		}
348
349
		return join('/', $arguments);
350
	}
351
352
	/**
353
	 * @param string $controller_service
354
	 * @param string $controller_arguments
355
	 * @return bool
356
	 */
357
	protected function is_startpage($controller_service, $controller_arguments)
358
	{
359
		return ($this->config['sitemaker_startpage_controller'] == $controller_service && $this->config['sitemaker_startpage_params'] == $controller_arguments) ? true : false;
360
	}
361
362
	/**
363
	 * @return bool
364
	 */
365
	protected function startpage_is_set()
366
	{
367
		return ($this->config['sitemaker_startpage_controller']) ? true : false;
368
	}
369
}
370