Passed
Push — renovate/configure ( c923d4...2c70da )
by
unknown
20:13
created

blocks::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 9
dl 0
loc 9
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
	 */
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
	{
54
		parent::__construct($cache, $config, $block_factory, $mapper_factory, $php_ext);
55
56
		$this->phpbb_dispatcher = $phpbb_dispatcher;
57
		$this->template = $template;
58
		$this->translator = $translator;
59
		$this->block_factory = $block_factory;
60
		$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
	 */
71
	public function display($edit_mode, array $route_info, $style_id, array $display_modes)
72
	{
73
		$ex_positions = array_flip($route_info['ex_positions']);
74
		$users_groups = $this->groups->get_users_groups();
75
		$route_blocks = $this->get_blocks_for_route($route_info, $style_id, $edit_mode);
76
77
		$positions = array();
78
		foreach ($route_blocks as $position => $blocks)
79
		{
80
			$positions[$position] = $this->show_position($position, $blocks, $ex_positions, $users_groups, $display_modes, $edit_mode);
81
		}
82
83
		$this->template->assign_vars(array(
84
			'positions'		=> $positions,
85
			'S_HAS_BLOCKS'	=> sizeof($positions),
86
		));
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
		$vars = array('positions');
96
		extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.modify_block_positions', compact($vars)));
97
	}
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
	{
110
		$pos_blocks = array();
111
		if (!$this->exclude_position($position, $ex_positions, $edit_mode))
112
		{
113
			foreach ($blocks as $index => $entity)
114
			{
115
				$pos_blocks[$index] = $this->render($display_modes, $edit_mode, $entity->to_array(), $users_groups, $index);
116
			}
117
		}
118
119
		return array_filter($pos_blocks);
120
	}
121
122
	/**
123
	 * Render block
124
	 *
125
	 * @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
		$block = array();
137
		if ($this->block_is_viewable($db_data, $display_modes, $users_groups, $edit_mode) && ($block_instance = $this->block_factory->get_block($service_name)) !== null)
138
		{
139
			$returned_data =  array(
140
				'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
				$returned_data['title'] = strtoupper(str_replace(['.', '-'], '_', $service_name));
152
				$this->set_edit_mode_content($returned_data, $edit_mode, $e->getMessage());
153
			}
154
155
			// we get the template after running 'display()' above so template can be set dynamically
156
			$returned_data['template'] = $block_instance->get_template();
157
158
			if ($this->block_has_content($returned_data, $edit_mode))
159
			{
160
				$returned_data['title'] = $this->get_block_title($db_data['title'], $returned_data['title']);
161
162
				$block = array_merge($db_data, $returned_data);
163
				$block['class'] .= self::$status_class[$block['status']];
164
			}
165
166
			/**
167
			 * Event to modify a rendered block.
168
			 *
169
			 * @event blitze.sitemaker.modify_rendered_block
170
			 * @var	array														block			Array of block properties
171
			 * @var	int															index			Display order/index in position
172
			 * @var	\blitze\sitemaker\services\blocks\driver\block_interface	block_instance	The block instance
173
			 * @since 3.0.1-RC1
174
			 */
175
			$vars = array('block', 'index', 'block_instance');
176
			extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.modify_rendered_block', compact($vars)));
177
		}
178
179
		return $block;
180
	}
181
182
	/**
183
	 * @param string $db_title
184
	 * @param string $df_title
185
	 * @return string
186
	 */
187
	protected function get_block_title($db_title, $df_title)
188
	{
189
		return ($db_title) ? $db_title : $this->translator->lang($df_title);
190
	}
191
192
	/**
193
	 * @param array $returned_data
194
	 * @param bool $edit_mode
195
	 * @return bool
196
	 */
197
	protected function block_has_content(array &$returned_data, $edit_mode)
198
	{
199
		if ($this->block_returns_nothing($returned_data))
200
		{
201
			return $this->set_edit_mode_content($returned_data, $edit_mode, 'BLOCK_NO_DATA');
202
		}
203
		else if ($this->block_is_missing_template($returned_data))
204
		{
205
			$returned_data['data'] = null;
206
			return $this->set_edit_mode_content($returned_data, $edit_mode, 'BLOCK_MISSING_TEMPLATE');
207
		}
208
209
		return true;
210
	}
211
212
	/**
213
	 * @param array $data
214
	 * @param bool $edit_mode
215
	 * @param string $lang_key
216
	 * @return bool
217
	 */
218
	protected function set_edit_mode_content(array &$data, $edit_mode, $lang_key)
219
	{
220
		if ($edit_mode)
221
		{
222
			$data['status'] = 0;
223
			$data['content'] = $this->translator->lang($lang_key);
224
225
			return true;
226
		}
227
228
		return false;
229
	}
230
231
	/**
232
	 * @param array $returned_data
233
	 * @return bool
234
	 */
235
	protected function block_returns_nothing(array $returned_data)
236
	{
237
		return !$returned_data['content'] && empty($returned_data['data']);
238
	}
239
240
	/**
241
	 * @param array $returned_data
242
	 * @return bool
243
	 */
244
	protected function block_is_missing_template(array $returned_data)
245
	{
246
		return is_array($returned_data['data']) && !$returned_data['template'];
247
	}
248
249
	/**
250
	 * Should we display this block?
251
	 *
252
	 * @param array $data
253
	 * @param array $display_modes
254
	 * @param array $users_groups
255
	 * @param bool $edit_mode
256
	 * @return bool
257
	 */
258
	protected function block_is_viewable(array $data, array $display_modes, array $users_groups, $edit_mode)
259
	{
260
		$type = $data['type'];
261
		$allowed_groups = $data['permission'];
262
263
		return ($display_modes[$type] && ($edit_mode || $this->user_is_permitted($allowed_groups, $users_groups))) ? true : false;
264
	}
265
266
	/**
267
	 * @param mixed $allowed_groups
268
	 * @param array $users_groups
269
	 * @return bool
270
	 */
271
	protected function user_is_permitted($allowed_groups, array $users_groups)
272
	{
273
		return (empty($allowed_groups) || sizeof(array_intersect($allowed_groups, $users_groups))) ? true : false;
274
	}
275
276
	/**
277
	 * @param string $position
278
	 * @param array $ex_positions
279
	 * @param bool $edit_mode
280
	 * @return bool
281
	 */
282
	protected function exclude_position($position, array $ex_positions, $edit_mode)
283
	{
284
		return ($edit_mode === false && isset($ex_positions[$position])) ? true : false;
285
	}
286
}
287