Passed
Push — develop ( 4eb606...0e8a98 )
by Daniel
03:34 queued 11s
created

admin_bar::__construct()   A

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
89
		$this->get_available_blocks();
90 2
		$this->get_startpage_options();
91 2
		$this->set_javascript_data($route, $style_id);
92
		$this->set_assets();
93 2
94 2
		$this->template->assign_vars(array(
95 2
			'S_EDIT_MODE'		=> true,
96 2
			'S_ROUTE_OPS'		=> $this->get_route_options($route),
97
			'S_HIDE_BLOCKS'		=> $route_info['hide_blocks'],
98 2
			'S_EX_POSITIONS'	=> join(', ', $route_info['ex_positions']),
99 2
			'S_STYLE_OPTIONS'	=> style_select($style_id, true),
100 2
			'S_STARTPAGE'		=> $this->startpage_is_set(),
101 2
102 2
			'ICON_PICKER'		=> $this->icons->picker(),
103 2
			'SM_USER_LANG'		=> $this->user->data['user_lang'],
104 2
			'TINYMCE_LANG'		=> $this->get_tinymce_lang(),
105 2
		));
106
	}
107 2
108 2
	/**
109 2
	 * Set data used in javascript
110
	 * @param string $route
111
	 * @param int $style_id
112
	 */
113
	public function set_javascript_data($route, $style_id)
114
	{
115
		$board_url = generate_board_url();
116 4
		$is_default_route = $u_default_route = false;
117
		if ($this->config['sitemaker_default_layout'])
118 4
		{
119 4
			$u_default_route = $this->get_default_route_url($is_default_route, $style_id, $route, $board_url);
120 4
		}
121 4
122 1
		$this->template->assign_vars(array(
123 1
			'S_IS_DEFAULT'		=> $is_default_route,
124 1
125 1
			'BLOCK_ACTIONS'		=> $this->get_block_actions(),
126
			'PAGE_URL'			=> build_url(array('style')),
127 4
128 4
			'UA_BOARD_URL'		=> $board_url,
129
			'UA_ROUTE'			=> $route,
130 4
			'UA_STYLE_ID'		=> $style_id,
131 4
			'UA_SCRIPT_PATH'	=> $this->user->page['root_script_path'],
132 4
			'UA_MODREWRITE'		=> $this->config['enable_mod_rewrite'],
133 4
			'UA_WEB_ROOT_PATH'	=> $this->util->get_web_path(),
134
			'UA_UPLOAD_URL'		=> $this->controller_helper->route('blitze_sitemaker_image_upload'),
135 4
			'UA_NAVBAR_MANAGER'	=> $this->controller_helper->route('blitze_sitemaker_navbar_manager', array(
136 4
				'style'	=> $this->template->get_user_style()[0]
137 4
			)),
138 4
139 4
			'U_VIEW_DEFAULT'	=> $u_default_route,
140 4
		));
141 4
	}
142
143 4
	/**
144 4
	 * @param bool $is_default_route
145 4
	 * @param int $style_id
146
	 * @param string $route
147
	 * @param string $board_url
148
	 * @return string
149
	 */
150 4
	protected function get_default_route_url(&$is_default_route, $style_id, $route, $board_url)
151
	{
152
		[$df_route, $df_style_id] = array_filter(explode(':', $this->config['sitemaker_default_layout'])) + array('', $style_id);
153 4
		$df_style_id = (int) $df_style_id;
154 4
155 4
		$is_default_route = ([$df_route, $df_style_id] === [$route, $style_id]) ? true : false;
156 4
		$u_default_route = $board_url . '/' . $df_route;
157 4
158 4
		if ($df_style_id !== $style_id)
159 4
		{
160 4
			$u_default_route .= (strpos($df_route, '?') !== false ? '&amp;' : '?') . 'style=' . $df_style_id;
161 4
		}
162 4
163 4
		return reapply_sid($u_default_route);
164
	}
165 4
166 4
	/**
167
	 * @return string
168 4
	 */
169 4
	protected function get_tinymce_lang()
170
	{
171 4
		$user_lang = $this->user->data['user_lang'];
172
173
		if (isset($this->tinymce_lang_mapping[$user_lang]))
174
		{
175
			return $this->tinymce_lang_mapping[$user_lang];
176
		}
177 3
		else
178
		{
179 3
			return preg_replace_callback('/_([a-z0-9]+)/i', function ($parts)
180
			{
181 3
				return strtoupper($parts[0]);
182
			}, $user_lang);
183 3
		}
184 3
	}
185 3
186 3
	/**
187 3
	 * @return array
188 3
	 */
189
	protected function get_block_actions()
190
	{
191
		$list = array(
192
			'add_block',
193 5
			'copy_route',
194
			'edit_block',
195 5
			'handle_custom_action',
196 5
			'save_block',
197
			'save_blocks',
198
			'set_default_route',
199 5
			'set_route_prefs',
200 2
			'set_startpage',
201 2
			'update_block',
202 2
			'update_column_width',
203 2
		);
204
205 2
		$actions = array();
206 2
		foreach ($list as $action)
207
		{
208 2
			$actions[$action] = $this->controller_helper->route('blitze_sitemaker_blocks_admin', array('action' => $action));
209 2
		}
210
211 2
		return $actions;
212 2
	}
213 2
214 2
	/**
215 2
	 * Get all available sitemaker blocks
216 2
	 */
217 2
	public function get_available_blocks()
218 2
	{
219 5
		$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks.factory')->get_all_blocks();
220
221
		foreach ($blocks as $service => $name)
222
		{
223
			$this->template->assign_block_vars('block', array(
224 2
				'NAME'		=> $name,
225
				'SERVICE'	=> $service
226
			));
227
		}
228 2
	}
229 2
230 2
	/**
231 2
	 * Provide options to set/unset current page as landing page
232 2
	 */
233 2
	public function get_startpage_options()
234
	{
235 2
		$symfony_request = $this->phpbb_container->get('symfony_request');
236 2
		$controller = $symfony_request->attributes->get('_controller');
237
238 2
		if ($controller)
239
		{
240
			list($controller_service, $controller_method) = explode(':', $controller);
241
			$controller_params	= $symfony_request->attributes->get('_route_params');
242
			$controller_object	= $this->phpbb_container->get($controller_service);
243
			$controller_class	= get_class($controller_object);
244
245
			$r = new \ReflectionMethod($controller_class, $controller_method);
246
			$class_params = $r->getParameters();
247
248 2
			list($namespace, $extension) = explode('\\', $controller_class);
249 2
			$controller_arguments = $this->get_arguments($controller_params, $class_params);
250
251 2
			$this->template->assign_vars(array(
252 2
				'CONTROLLER_NAME'	=> $controller_service,
253
				'CONTROLLER_METHOD'	=> $controller_method,
254
				'CONTROLLER_PARAMS'	=> $controller_arguments,
255
				'S_IS_STARTPAGE'	=> $this->is_startpage($controller_service, $controller_arguments),
256
				'UA_EXTENSION'		=> $namespace . '/' . $extension,
257
			));
258
		}
259
	}
260 4
261
	/**
262 4
	 * Add js/css
263
	 */
264 4
	public function set_assets()
265 4
	{
266
		$assets = array(
267 4
			'css'	=> array(
268 4
				'@blitze_sitemaker/assets/blocks/manager.min.css',
269 4
			),
270
			'js'	=> array(
271 4
				'@blitze_sitemaker/assets/runtime.min.js',
272
				'@blitze_sitemaker/assets/tinymce/tinymce.min.js',
273
				'@blitze_sitemaker/assets/blocks/manager.min.js',
274
			)
275
		);
276
277
		/**
278
		 * Event to set assets for available blocks
279
		 * Use array_merge_recursive to add new assets
280 4
		 *
281
		 * @event blitze.sitemaker.admin_bar.set_assets
282 4
		 * @var	array	assets		Array of assets to include of form array([js] => array(), [css] => array())
283 4
		 * @since 3.0.1-RC1
284
		 */
285 2
		$vars = array('assets');
286 4
		extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.admin_bar.set_assets', compact($vars)));
287
288 4
		$this->util->add_assets($assets);
289
	}
290
291
	/**
292
	 * Get routes with blocks
293
	 *
294 4
	 * @param string $current_route
295
	 * @return string
296 4
	 */
297 4
	public function get_route_options($current_route)
298
	{
299 4
		$routes_ary = $this->get_routes();
300 4
301
		$options = '<option value="">' . $this->translator->lang('SELECT') . '</option>';
302
		foreach ($routes_ary as $route)
303 4
		{
304 4
			$selected = ($route == $current_route) ? ' selected="selected"' : '';
305 4
			$options .= '<option value="' . $route . '"' . $selected . '>' . $route . '</option>';
306
		}
307 4
308
		return $options;
309
	}
310
311
	/**
312
	 * @return array
313
	 */
314
	protected function get_routes()
315 2
	{
316
		$factory = $this->phpbb_container->get('blitze.sitemaker.mapper.factory');
317 2
		$collection = $factory->create('routes')->find();
318 2
319
		$routes_ary = array();
320 2
		foreach ($collection as $entity)
321 2
		{
322 2
			/** @var \blitze\sitemaker\model\entity\route $entity */
323
			$route_name = $entity->get_route();
324 2
			$routes_ary[$route_name] = $route_name;
325
		}
326
327
		return $routes_ary;
328
	}
329
330
	/**
331
	 * @param array $controller_params
332 2
	 * @param array $class_params
333
	 * @return string
334 2
	 */
335
	protected function get_arguments(array $controller_params, array $class_params)
336
	{
337
		$arguments = array();
338
		foreach ($class_params as $param)
339
		{
340 2
			$name = $param->getName();
341
			$arguments[$name] = ($param->isOptional()) ? $param->getDefaultValue() : $controller_params[$name];
342 2
		}
343
344
		return join('/', $arguments);
345
	}
346
347
	/**
348
	 * @param string $controller_service
349
	 * @param string $controller_arguments
350
	 * @return bool
351
	 */
352
	protected function is_startpage($controller_service, $controller_arguments)
353
	{
354
		return ($this->config['sitemaker_startpage_controller'] == $controller_service && $this->config['sitemaker_startpage_params'] == $controller_arguments) ? true : false;
355
	}
356
357
	/**
358
	 * @return bool
359
	 */
360
	protected function startpage_is_set()
361
	{
362
		return ($this->config['sitemaker_startpage_controller']) ? true : false;
363
	}
364
}
365