blocks::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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