Passed
Push — renovate/configure ( 69e50f...2a0f25 )
by
unknown
03:34
created

admin_bar::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
c 0
b 0
f 0
nc 1
nop 10
dl 0
loc 12
rs 9.9332

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
		$this->controller_helper = $controller_helper;
65
		$this->phpbb_container = $phpbb_container;
66
		$this->phpbb_dispatcher = $phpbb_dispatcher;
67
		$this->template = $template;
68
		$this->translator = $translator;
69
		$this->user = $user;
70
		$this->icons = $icons;
71
		$this->util = $util;
72
		$this->tinymce_lang_mapping = $lang_mapping;
73
	}
74
75
	/**
76
	 * Show admin bar
77
	 *
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
		$this->phpbb_container->get('blitze.sitemaker.auto_lang')->add('blocks_admin');
85
86
		$route = $route_info['route'];
87
		$style_id = $route_info['style'];
88
89
		$this->get_available_blocks();
90
		$this->get_startpage_options();
91
		$this->set_javascript_data($route, $style_id);
92
		$this->set_assets();
93
94
		$this->template->assign_vars(array(
95
			'S_EDIT_MODE'		=> true,
96
			'S_ROUTE_OPS'		=> $this->get_route_options($route),
97
			'S_HIDE_BLOCKS'		=> $route_info['hide_blocks'],
98
			'S_EX_POSITIONS'	=> join(', ', $route_info['ex_positions']),
99
			'S_STYLE_OPTIONS'	=> style_select($style_id, true),
100
			'S_STARTPAGE'		=> $this->startpage_is_set(),
101
102
			'ICON_PICKER'		=> $this->icons->picker(),
103
			'SM_USER_LANG'		=> $this->user->data['user_lang'],
104
			'TINYMCE_LANG'		=> $this->get_tinymce_lang(),
105
		));
106
	}
107
108
	/**
109
	 * 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
		$is_default_route = $u_default_route = false;
117
		if ($this->config['sitemaker_default_layout'])
118
		{
119
			$u_default_route = $this->get_default_route_url($is_default_route, $style_id, $route, $board_url);
120
		}
121
122
		$this->template->assign_vars(array(
123
			'S_IS_DEFAULT'		=> $is_default_route,
124
125
			'BLOCK_ACTIONS'		=> $this->get_block_actions(),
126
			'PAGE_URL'			=> build_url(array('style')),
127
128
			'UA_BOARD_URL'		=> $board_url,
129
			'UA_ROUTE'			=> $route,
130
			'UA_STYLE_ID'		=> $style_id,
131
			'UA_SCRIPT_PATH'	=> $this->user->page['root_script_path'],
132
			'UA_MODREWRITE'		=> $this->config['enable_mod_rewrite'],
133
			'UA_WEB_ROOT_PATH'	=> $this->util->get_web_path(),
134
			'UA_UPLOAD_URL'		=> $this->controller_helper->route('blitze_sitemaker_image_upload'),
135
			'UA_NAVBAR_MANAGER'	=> $this->controller_helper->route('blitze_sitemaker_navbar_manager', array(
136
				'style'	=> $this->template->get_user_style()[0]
137
			)),
138
139
			'U_VIEW_DEFAULT'	=> $u_default_route,
140
		));
141
	}
142
143
	/**
144
	 * @param bool $is_default_route
145
	 * @param int $style_id
146
	 * @param string $route
147
	 * @param string $board_url
148
	 * @return string
149
	 */
150
	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
		$df_style_id = (int) $df_style_id;
154
155
		$is_default_route = ([$df_route, $df_style_id] === [$route, $style_id]) ? true : false;
156
		$u_default_route = $board_url . '/' . $df_route;
157
158
		if ($df_style_id !== $style_id)
159
		{
160
			$u_default_route .= (strpos($df_route, '?') !== false ? '&amp;' : '?') . 'style=' . $df_style_id;
161
		}
162
163
		return reapply_sid($u_default_route);
164
	}
165
166
	/**
167
	 * @return string
168
	 */
169
	protected function get_tinymce_lang()
170
	{
171
		$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
		else
178
		{
179
			return preg_replace_callback('/_([a-z0-9]+)/i', function ($parts)
180
			{
181
				return strtoupper($parts[0]);
182
			}, $user_lang);
183
		}
184
	}
185
186
	/**
187
	 * @return array
188
	 */
189
	protected function get_block_actions()
190
	{
191
		$list = array(
192
			'add_block',
193
			'copy_route',
194
			'edit_block',
195
			'handle_custom_action',
196
			'save_block',
197
			'save_blocks',
198
			'set_default_route',
199
			'set_route_prefs',
200
			'set_startpage',
201
			'update_block',
202
			'update_column_width',
203
		);
204
205
		$actions = array();
206
		foreach ($list as $action)
207
		{
208
			$actions[$action] = $this->controller_helper->route('blitze_sitemaker_blocks_admin', array('action' => $action));
209
		}
210
211
		return $actions;
212
	}
213
214
	/**
215
	 * Get all available sitemaker blocks
216
	 */
217
	public function get_available_blocks()
218
	{
219
		$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
				'NAME'		=> $name,
225
				'SERVICE'	=> $service
226
			));
227
		}
228
	}
229
230
	/**
231
	 * Provide options to set/unset current page as landing page
232
	 */
233
	public function get_startpage_options()
234
	{
235
		$symfony_request = $this->phpbb_container->get('symfony_request');
236
		$controller = $symfony_request->attributes->get('_controller');
237
238
		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
			list($namespace, $extension) = explode('\\', $controller_class);
249
			$controller_arguments = $this->get_arguments($controller_params, $class_params);
250
251
			$this->template->assign_vars(array(
252
				'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
261
	/**
262
	 * Add js/css
263
	 */
264
	public function set_assets()
265
	{
266
		$assets = array(
267
			'css'	=> array(
268
				'@blitze_sitemaker/assets/blocks/manager.min.css',
269
			),
270
			'js'	=> array(
271
				'@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
		 *
281
		 * @event blitze.sitemaker.admin_bar.set_assets
282
		 * @var	array	assets		Array of assets to include of form array([js] => array(), [css] => array())
283
		 * @since 3.0.1-RC1
284
		 */
285
		$vars = array('assets');
286
		extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.admin_bar.set_assets', compact($vars)));
287
288
		$this->util->add_assets($assets);
289
	}
290
291
	/**
292
	 * Get routes with blocks
293
	 *
294
	 * @param string $current_route
295
	 * @return string
296
	 */
297
	public function get_route_options($current_route)
298
	{
299
		$routes_ary = $this->get_routes();
300
301
		$options = '<option value="">' . $this->translator->lang('SELECT') . '</option>';
302
		foreach ($routes_ary as $route)
303
		{
304
			$selected = ($route == $current_route) ? ' selected="selected"' : '';
305
			$options .= '<option value="' . $route . '"' . $selected . '>' . $route . '</option>';
306
		}
307
308
		return $options;
309
	}
310
311
	/**
312
	 * @return array
313
	 */
314
	protected function get_routes()
315
	{
316
		$factory = $this->phpbb_container->get('blitze.sitemaker.mapper.factory');
317
		$collection = $factory->create('routes')->find();
318
319
		$routes_ary = array();
320
		foreach ($collection as $entity)
321
		{
322
			/** @var \blitze\sitemaker\model\entity\route $entity */
323
			$route_name = $entity->get_route();
324
			$routes_ary[$route_name] = $route_name;
325
		}
326
327
		return $routes_ary;
328
	}
329
330
	/**
331
	 * @param array $controller_params
332
	 * @param array $class_params
333
	 * @return string
334
	 */
335
	protected function get_arguments(array $controller_params, array $class_params)
336
	{
337
		$arguments = array();
338
		foreach ($class_params as $param)
339
		{
340
			$name = $param->getName();
341
			$arguments[$name] = ($param->isOptional()) ? $param->getDefaultValue() : $controller_params[$name];
342
		}
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