Completed
Push — develop ( f77be0...c76de5 )
by Daniel
08:12
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 11
CRAP Score 1

Importance

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

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\auth\auth */
17
	protected $auth;
18
19
	/** @var \phpbb\config\config */
20
	protected $config;
21
22
	/** @var ContainerInterface */
23
	protected $phpbb_container;
24
25
	/** @var \phpbb\template\template */
26
	protected $template;
27
28
	/** @var \phpbb\language\language */
29
	protected $translator;
30
31
	/** @var \phpbb\user */
32
	protected $user;
33
34
	/** @var \blitze\sitemaker\services\icon_picker */
35
	protected $icons;
36
37
	/** @var \blitze\sitemaker\services\util */
38
	protected $util;
39
40
	/** @var string phpEx */
41
	protected $php_ext;
42
43
	/**
44
	 * Constructor
45
	 *
46
	 * @param \phpbb\auth\auth							$auth					Auth object
47
	 * @param \phpbb\config\config						$config					Config object
48
	 * @param ContainerInterface						$phpbb_container		Service container
49
	 * @param \phpbb\template\template					$template				Template object
50
	 * @param \phpbb\language\language					$translator				Language object
51
	 * @param \phpbb\user								$user					User object
52
	 * @param \blitze\sitemaker\services\icon_picker	$icons					Sitemaker icon picker object
53
	 * @param \blitze\sitemaker\services\util			$util					Sitemaker util object
54
	 * @param string									$php_ext				phpEx
55
	 */
56 12
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, ContainerInterface $phpbb_container, \phpbb\template\template $template, \phpbb\language\language $translator, \phpbb\user $user, \blitze\sitemaker\services\icon_picker $icons, \blitze\sitemaker\services\util $util, $php_ext)
57
	{
58 12
		$this->auth = $auth;
59 12
		$this->config = $config;
60 12
		$this->phpbb_container = $phpbb_container;
61 12
		$this->template = $template;
62 12
		$this->translator = $translator;
63 12
		$this->user = $user;
64 12
		$this->icons = $icons;
65 12
		$this->util = $util;
66 12
		$this->php_ext = $php_ext;
67 12
	}
68
69
	/**
70
	 * Show admin bar
71
	 *
72
	 * @param array $route_info
73
	 */
74 4
	public function show(array $route_info)
75
	{
76 4
		$this->translator->add_lang('block_manager', 'blitze/sitemaker');
77
78 2
		$this->phpbb_container->get('blitze.sitemaker.auto_lang')->add('blocks_admin');
79
80 2
		$route = $route_info['route'];
81 2
		$style_id = $route_info['style'];
82
83 2
		$this->get_available_blocks();
84 2
		$this->get_startpage_options();
85 2
		$this->set_javascript_data($route, $style_id);
86 2
		$this->set_assets();
87
88 2
		$this->template->assign_vars(array(
89 2
			'S_EDIT_MODE'		=> true,
90 2
			'S_ROUTE_OPS'		=> $this->get_route_options($route),
91 2
			'S_HIDE_BLOCKS'		=> $route_info['hide_blocks'],
92 2
			'S_POSITION_OPS'	=> $this->get_excluded_position_options($route_info['ex_positions']),
93 2
			'S_EX_POSITIONS'	=> join(', ', $route_info['ex_positions']),
94 2
			'S_STYLE_OPTIONS'	=> style_select($style_id, true),
95 2
			'S_STARTPAGE'		=> $this->startpage_is_set(),
96
97 2
			'ICON_PICKER'		=> $this->icons->picker(),
98 2
		));
99 2
	}
100
101
	/**
102
	 * Set data used in javascript
103
	 * @param string $route
104
	 * @param int $style_id
105
	 */
106 4
	public function set_javascript_data($route, $style_id)
107
	{
108 4
		$board_url = generate_board_url();
109 4
		$ajax_url = $board_url . ((!$this->config['enable_mod_rewrite']) ? '/app.' . $this->php_ext : '');
110
111 4
		$is_default_route = $u_default_route = false;
112 4
		if ($this->config['sitemaker_default_layout'])
113 4
		{
114 1
			$is_default_route = ($this->config['sitemaker_default_layout'] === $route) ? true : false;
115 1
			$u_default_route .= $board_url . '/' . $this->config['sitemaker_default_layout'];
116 1
			$u_default_route = reapply_sid($u_default_route);
117 1
		}
118
119 4
		$this->template->assign_vars(array(
120 4
			'S_IS_DEFAULT'		=> $is_default_route,
121
122 4
			'PAGE_URL'			=> build_url(array('style')),
123
124 4
			'UA_AJAX_URL'		=> $ajax_url,
125 4
			'UA_BOARD_URL'		=> $board_url,
126 4
			'UA_ROUTE'			=> $route,
127 4
			'UA_STYLE_ID'		=> $style_id,
128 4
			'UA_SCRIPT_PATH'	=> $this->user->page['root_script_path'],
129 4
			'UA_WEB_ROOT_PATH'	=> $this->util->get_web_path(),
130 4
			'UA_FILEMANAGER'	=> $this->can_use_filemanager(),
131 4
			'UA_RF_ACCESS_KEY'	=> sha1($this->user->data['user_form_salt'] . 'filemanager'),
132
133 4
			'U_VIEW_DEFAULT'	=> $u_default_route,
134 4
		));
135 4
	}
136
137
	/**
138
	 * Get all available sitemaker blocks
139
	 */
140 3
	public function get_available_blocks()
141
	{
142 3
		$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks.factory')->get_all_blocks();
143
144 3
		foreach ($blocks as $service => $name)
145
		{
146 3
			$this->template->assign_block_vars('block', array(
147 3
				'NAME'		=> $name,
148 3
				'SERVICE'	=> $service)
149 3
			);
150 3
		}
151 3
	}
152
153
	/**
154
	 * Provide options to set/unset current page as landing page
155
	 */
156 5
	public function get_startpage_options()
157
	{
158 5
		$symfony_request = $this->phpbb_container->get('symfony_request');
159 5
		$controller = $symfony_request->attributes->get('_controller');
160
161
		if ($controller)
162 5
		{
163 2
			list($controller_service, $controller_method) = explode(':', $controller);
164 2
			$controller_params	= $symfony_request->attributes->get('_route_params');
165 2
			$controller_object	= $this->phpbb_container->get($controller_service);
166 2
			$controller_class	= get_class($controller_object);
167
168 2
			$r = new \ReflectionMethod($controller_class, $controller_method);
169 2
			$class_params = $r->getParameters();
170
171 2
			list($namespace, $extension) = explode('\\', $controller_class);
172 2
			$controller_arguments = $this->get_arguments($controller_params, $class_params);
173
174 2
			$this->template->assign_vars(array(
175 2
				'CONTROLLER_NAME'	=> $controller_service,
176 2
				'CONTROLLER_METHOD'	=> $controller_method,
177 2
				'CONTROLLER_PARAMS'	=> $controller_arguments,
178 2
				'S_IS_STARTPAGE'	=> $this->is_startpage($controller_service, $controller_arguments),
179 2
				'UA_EXTENSION'		=> $namespace . '/' . $extension,
180 2
			));
181 2
		}
182 5
	}
183
184
	/**
185
	 * Add js/css
186
	 */
187 2
	public function set_assets()
188
	{
189 2
		$this->util->add_assets(array(
190
			'js'	=> array(
191 2
				'@blitze_sitemaker/vendor/jquery-ui/jquery-ui.min.js',
192 2
				'@blitze_sitemaker/vendor/tinymce/tinymce.min.js',
193 2
				'@blitze_sitemaker/vendor/jqueryui-touch-punch/jquery.ui.touch-punch.min.js',
194 2
				'@blitze_sitemaker/vendor/twig.js/index.js',
195 2
				100 =>  '@blitze_sitemaker/assets/blocks/manager.min.js',
196 2
			),
197
			'css'   => array(
198 2
				'@blitze_sitemaker/vendor/jquery-ui/themes/smoothness/jquery-ui.min.css',
199 2
				'@blitze_sitemaker/assets/blocks/manager.min.css',
200
			)
201 2
		));
202 2
	}
203
204
	/**
205
	 * Get routes with blocks
206
	 *
207
	 * @param string $current_route
208
	 * @return string
209
	 */
210 4
	public function get_route_options($current_route)
211
	{
212 4
		$routes_ary = $this->get_routes();
213
214 4
		$options = '<option value="">' . $this->translator->lang('SELECT') . '</option>';
215 4
		foreach ($routes_ary as $route)
216
		{
217 4
			$selected = ($route == $current_route) ? ' selected="selected"' : '';
218 4
			$options .= '<option value="' . $route . '"' . $selected . '>' . $route . '</option>';
219 4
		}
220
221 4
		return $options;
222
	}
223
224
	/**
225
	 * Get excluded position options
226
	 *
227
	 * @param array $ex_positions
228
	 * @return string
229
	 */
230 4
	public function get_excluded_position_options(array $ex_positions)
231
	{
232 4
		$options = '';
233 4
		foreach ($ex_positions as $position)
234
		{
235 2
			$options .= '<option value="' . $position . '" selected="selected">' . $position . '</option>';
236 4
		}
237
238 4
		return $options;
239
	}
240
241
	/**
242
	 * @return array
243
	 */
244 4
	protected function get_routes()
245
	{
246 4
		$factory = $this->phpbb_container->get('blitze.sitemaker.mapper.factory');
247 4
		$collection = $factory->create('routes')->find();
248
249 4
		$routes_ary = array();
250 4
		foreach ($collection as $entity)
251
		{
252
			/** @var \blitze\sitemaker\model\entity\route $entity */
253 4
			$route_name = $entity->get_route();
254 4
			$routes_ary[$route_name] = $route_name;
255 4
		}
256
257 4
		return $routes_ary;
258
	}
259
260
	/**
261
	 * @param array $controller_params
262
	 * @param array $class_params
263
	 * @return string
264
	 */
265 2
	protected function get_arguments(array $controller_params, array $class_params)
266
	{
267 2
		$arguments = array();
268 2
		foreach ($class_params as $param)
269
		{
270 2
			$name = $param->getName();
271 2
			$arguments[$name] = ($param->isOptional()) ? $param->getDefaultValue() : $controller_params[$name];
272 2
		}
273
274 2
		return join('/', $arguments);
275
	}
276
277
	/**
278
	 * @param string $controller_service
279
	 * @param string $controller_arguments
280
	 * @return bool
281
	 */
282 2
	protected function is_startpage($controller_service, $controller_arguments)
283
	{
284 2
		return ($this->config['sitemaker_startpage_controller'] == $controller_service && $this->config['sitemaker_startpage_params'] == $controller_arguments) ? true : false;
285
	}
286
287
	/**
288
	 * @return bool
289
	 */
290 2
	protected function startpage_is_set()
291
	{
292 2
		return ($this->config['sitemaker_startpage_controller']) ? true : false;
293
	}
294
295
	/**
296
	 * @return bool
297
	 */
298 4
	protected function can_use_filemanager()
299
	{
300 4
		return $this->config['sm_filemanager'] && $this->auth->acl_get('u_sm_filemanager');
301
	}
302
}
303