Passed
Push — develop ( b3eda6...9f2d35 )
by Daniel
03:59 queued 40s
created

blocks::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 9
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 10
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
class blocks extends routes
14
{
15
	/** @var \phpbb\config\config */
16
	protected $config;
17
18
	/** @var \phpbb\event\dispatcher_interface */
19
	protected $phpbb_dispatcher;
20
21
	/** @var \phpbb\template\template */
22
	protected $template;
23
24
	/** @var \phpbb\language\language */
25
	protected $translator;
26
27
	/** @var \blitze\sitemaker\services\blocks\factory */
28
	protected $block_factory;
29
30
	/** @var \blitze\sitemaker\services\groups */
31
	protected $groups;
32
33
	/** @var array */
34
	protected static $status_class = array(
35
		0	=> ' sm-inactive',
36
		1	=> '',
37
	);
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
43
	 * @param \phpbb\config\config							$config					Config object
44
	 * @param \phpbb\event\dispatcher_interface				$phpbb_dispatcher		Event dispatcher
45
	 * @param \phpbb\template\template						$template				Template object
46
	 * @param \phpbb\language\language						$translator				Language object
47
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
48
	 * @param \blitze\sitemaker\services\groups				$groups					Groups Object
49
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
50
	 * @param string										$php_ext				phpEx
51 42
	 */
52
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\template\template $template, \phpbb\language\language $translator, \blitze\sitemaker\services\blocks\factory $block_factory, \blitze\sitemaker\services\groups $groups, \blitze\sitemaker\model\mapper_factory $mapper_factory, $php_ext)
53 42
	{
54
		parent::__construct($cache, $config, $block_factory, $mapper_factory, $php_ext);
55 42
56 42
		$this->phpbb_dispatcher = $phpbb_dispatcher;
57 42
		$this->template = $template;
58 42
		$this->translator = $translator;
59 42
		$this->block_factory = $block_factory;
60 42
		$this->groups = $groups;
61
	}
62
63
	/**
64
	 * Display blocks for current route
65
	 *
66
	 * @param bool $edit_mode
67
	 * @param array $route_info
68
	 * @param int $style_id
69
	 * @param array $display_modes
70 23
	 */
71
	public function display($edit_mode, array $route_info, $style_id, array $display_modes)
72 23
	{
73 23
		$ex_positions = array_flip($route_info['ex_positions']);
74
		$users_groups = $this->groups->get_users_groups();
75 23
		$route_blocks = $this->get_blocks_for_route($route_info, $style_id, $edit_mode);
76
77 23
		$positions = array();
78 23
		foreach ($route_blocks as $position => $blocks)
79
		{
80 18
			$positions[$position] = $this->show_position($position, $blocks, $ex_positions, $users_groups, $display_modes, $edit_mode);
81 23
		}
82
83 23
		$this->template->assign_vars(array(
84 23
			'positions'		=> $positions,
85 23
			'S_HAS_BLOCKS'	=> sizeof($positions),
86 23
		));
87
88
		/**
89
		 * Event to modify block positions.
90
		 *
91
		 * @event blitze.sitemaker.modify_block_positions
92
		 * @var	array	positions		Array of block positions
93
		 * @since 3.0.1-RC1
94
		 */
95 23
		$vars = array('positions');
96 23
		extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.modify_block_positions', compact($vars)));
97 23
	}
98
99
	/**
100
	 * @param string $position
101
	 * @param array $blocks
102
	 * @param array $ex_positions
103
	 * @param array $users_groups
104
	 * @param array $display_modes
105
	 * @param bool $edit_mode
106
	 * @return array[]
107
	 */
108
	protected function show_position($position, array $blocks, array $ex_positions, array $users_groups, $display_modes, $edit_mode)
109 18
	{
110
		$pos_blocks = array();
111 18
		if (!$this->exclude_position($position, $ex_positions, $edit_mode))
112
		{
113 18
			foreach ($blocks as $index => $entity)
114 18
			{
115 18
				$pos_blocks[$index] = $this->render($display_modes, $edit_mode, $entity->to_array(), $users_groups, $index);
116 16
			}
117
		}
118 16
119 16
		return array_filter($pos_blocks);
120 16
	}
121 16
122
	/**
123 16
	 * Render block
124 16
	 *
125 16
	 * @param array $display_modes
126
	 * @param bool $edit_mode
127
	 * @param array $db_data
128
	 * @param array $users_groups
129
	 * @param int $index
130
	 * @return string[]
131
	 */
132
	public function render(array $display_modes, $edit_mode, array $db_data, array $users_groups, $index)
133
	{
134
		$service_name = $db_data['name'];
135
136 16
		$block = array();
137 16
		if ($this->block_is_viewable($db_data, $display_modes, $users_groups, $edit_mode) && ($block_instance = $this->block_factory->get_block($service_name)) !== null)
138 16
		{
139
			$returned_data =  array(
140 18
				'title'		=> '',
141
				'data'		=> null,
142
				'content'	=> null,
143
			);
144
145
			try
146
			{
147
				$returned_data = array_merge($returned_data, $block_instance->display($db_data, $edit_mode));
148
			}
149
			catch (\Exception $e)
150
			{
151
				$this->set_edit_mode_content($returned_data, $edit_mode, $e->getMessage());
152 18
			}
153
154 18
			// we get the template after running 'display()' above so template can be set dynamically
155 18
			$returned_data['template'] = $block_instance->get_template();
156 18
157 18
			if ($this->block_has_content($returned_data, $edit_mode))
158
			{
159 18
				$returned_data['title'] = $this->get_block_title($db_data['title'], $returned_data['title']);
160 18
161 18
				$block = array_merge($db_data, $returned_data);
162
				$block['class'] .= self::$status_class[$block['status']];
163 18
			}
164
165
			/**
166
			 * Event to modify a rendered block.
167
			 *
168
			 * @event blitze.sitemaker.modify_rendered_block
169
			 * @var	array														block			Array of block properties
170
			 * @var	int															index			Display order/index in position
171 16
			 * @var	\blitze\sitemaker\services\blocks\driver\block_interface	block_instance	The block instance
172
			 * @since 3.0.1-RC1
173 16
			 */
174
			$vars = array('block', 'index', 'block_instance');
175
			extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.modify_rendered_block', compact($vars)));
176
		}
177
178
		return $block;
179
	}
180
181 16
	/**
182
	 * @param string $db_title
183 16
	 * @param string $df_title
184 16
	 * @return string
185 16
	 */
186 16
	protected function get_block_title($db_title, $df_title)
187 16
	{
188
		return ($db_title) ? $db_title : $this->translator->lang($df_title);
189 3
	}
190 1
191 1
	/**
192 1
	 * @param array $returned_data
193
	 * @param bool $edit_mode
194 16
	 * @return bool
195
	 */
196
	protected function block_has_content(array &$returned_data, $edit_mode)
197
	{
198
		if ($this->block_returns_nothing($returned_data))
199
		{
200
			return $this->set_edit_mode_content($returned_data, $edit_mode, 'BLOCK_NO_DATA');
201
		}
202
		else if ($this->block_is_missing_template($returned_data))
203
		{
204
			$returned_data['data'] = null;
205
			return $this->set_edit_mode_content($returned_data, $edit_mode, 'BLOCK_MISSING_TEMPLATE');
206 18
		}
207
208 18
		return true;
209 18
	}
210
211 18
	/**
212
	 * @param array $data
213
	 * @param bool $edit_mode
214
	 * @param string $lang_key
215
	 * @return bool
216
	 */
217
	protected function set_edit_mode_content(array &$data, $edit_mode, $lang_key)
218
	{
219 12
		if ($edit_mode)
220
		{
221 12
			$data['status'] = 0;
222
			$data['content'] = $this->translator->lang($lang_key);
223
224
			return true;
225
		}
226
227
		return false;
228
	}
229
230 18
	/**
231
	 * @param array $returned_data
232 18
	 * @return bool
233
	 */
234
	protected function block_returns_nothing(array $returned_data)
235
	{
236
		return !$returned_data['content'] && empty($returned_data['data']);
237
	}
238
239
	/**
240
	 * @param array $returned_data
241
	 * @return bool
242
	 */
243
	protected function block_is_missing_template(array $returned_data)
244
	{
245
		return is_array($returned_data['data']) && !$returned_data['template'];
246
	}
247
248
	/**
249
	 * Should we display this block?
250
	 *
251
	 * @param array $data
252
	 * @param array $display_modes
253
	 * @param array $users_groups
254
	 * @param bool $edit_mode
255
	 * @return bool
256
	 */
257
	protected function block_is_viewable(array $data, array $display_modes, array $users_groups, $edit_mode)
258
	{
259
		$type = $data['type'];
260
		$allowed_groups = $data['permission'];
261
262
		return ($display_modes[$type] && ($edit_mode || $this->user_is_permitted($allowed_groups, $users_groups))) ? true : false;
263
	}
264
265
	/**
266
	 * @param mixed $allowed_groups
267
	 * @param array $users_groups
268
	 * @return bool
269
	 */
270
	protected function user_is_permitted($allowed_groups, array $users_groups)
271
	{
272
		return (empty($allowed_groups) || sizeof(array_intersect($allowed_groups, $users_groups))) ? true : false;
273
	}
274
275
	/**
276
	 * @param string $position
277
	 * @param array $ex_positions
278
	 * @param bool $edit_mode
279
	 * @return bool
280
	 */
281
	protected function exclude_position($position, array $ex_positions, $edit_mode)
282
	{
283
		return ($edit_mode === false && isset($ex_positions[$position])) ? true : false;
284
	}
285
}
286