Completed
Push — develop ( 8abf2d...b28862 )
by Daniel
09:52
created

blocks::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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
class blocks extends routes
13
{
14
	/** @var \phpbb\config\config */
15
	protected $config;
16
17
	/** @var \phpbb\event\dispatcher_interface */
18
	protected $phpbb_dispatcher;
19
20
	/** @var \phpbb\template\template */
21
	protected $template;
22
23
	/** @var \phpbb\language\language */
24
	protected $translator;
25
26
	/** @var \blitze\sitemaker\services\blocks\factory */
27
	protected $block_factory;
28
29
	/** @var \blitze\sitemaker\services\groups */
30
	protected $groups;
31
32
	/** @var array */
33
	protected static $status_class = array(
34
		0	=> ' sm-inactive',
35
		1	=> '',
36
	);
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
42
	 * @param \phpbb\config\config							$config					Config object
43
	 * @param \phpbb\event\dispatcher_interface				$phpbb_dispatcher		Event dispatcher
44
	 * @param \phpbb\template\template						$template				Template object
45
	 * @param \phpbb\language\language						$translator				Language object
46
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
47
	 * @param \blitze\sitemaker\services\groups				$groups					Groups Object
48
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
49
	 * @param string										$php_ext				phpEx
50
	 */
51 39
	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)
52
	{
53 39
		parent::__construct($cache, $config, $block_factory, $mapper_factory, $php_ext);
54
55 39
		$this->phpbb_dispatcher = $phpbb_dispatcher;
56 39
		$this->template = $template;
57 39
		$this->translator = $translator;
58 39
		$this->block_factory = $block_factory;
59 39
		$this->groups = $groups;
60 39
	}
61
62
	/**
63
	 * Display blocks for current route
64
	 *
65
	 * @param bool $edit_mode
66
	 * @param array $route_info
67
	 * @param int $style_id
68
	 * @param array $display_modes
69
	 */
70 22
	public function display($edit_mode, array $route_info, $style_id, array $display_modes)
71
	{
72 22
		$ex_positions = array_flip($route_info['ex_positions']);
73 22
		$users_groups = $this->groups->get_users_groups();
74
75 22
		$route_blocks = $this->get_blocks_for_route($route_info, $style_id, $edit_mode);
76
77 22
		$positions = array();
78 22
		foreach ($route_blocks as $position => $blocks)
79
		{
80 17
			$positions[$position] = $this->show_position($position, $blocks, $ex_positions, $users_groups, $display_modes, $edit_mode);
81 22
		}
82
83 22
		$this->template->assign_vars(array(
84 22
			'positions'		=> $positions,
85 22
			'S_HAS_BLOCKS'	=> sizeof($positions),
86 22
		));
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(
96 22
			'positions',
97 22
		);
98 22
		extract($this->phpbb_dispatcher->trigger_event('blitze_sitemaker.modify_block_positions', compact($vars)));
1 ignored issue
show
Bug introduced by
$this->phpbb_dispatcher-...tions', compact($vars)) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
99 22
	}
100
101
	/**
102
	 * Render block
103
	 *
104
	 * @param array $display_modes
105
	 * @param bool $edit_mode
106
	 * @param array $db_data
107
	 * @param array $users_groups
108
	 * @param int $index
109
	 */
110 17
	public function render(array $display_modes, $edit_mode, array $db_data, array $users_groups, $index)
111
	{
112 17
		$service_name = $db_data['name'];
113
114 17
		$block = array();
115 17
		if ($this->_block_is_viewable($db_data, $display_modes, $users_groups, $edit_mode) && ($block_instance = $this->block_factory->get_block($service_name)) !== null)
116 17
		{
117 15
			$returned_data = $block_instance->display($db_data, $edit_mode);
118
119 15
			if ($content = $this->_get_block_content($returned_data, $edit_mode))
120 15
			{
121 15
				$returned_data['title'] = $this->_get_block_title($db_data['title'], $returned_data['title']);
122 15
				$returned_data['content'] = $content;
123
124 15
				$block = array_merge($db_data, $returned_data);
125 15
				$block['class'] .= self::$status_class[$block['status']];
126 15
			}
127
128
			/**
129
			 * Event to modify a rendered block.
130
			 *
131
			 * @event blitze_sitemaker.modify_rendered_block
132
			 * @var	array	block		Array of block properties
133
			 * @var	int		index		Display order/index in position
134
			 * @var \blitze\sitemaker\services\blocks\driver\block_interface	$block_instance
135
			 * @since 3.0.1-RC1
136
			 */
137
			$vars = array(
138 15
				'block',
139 15
				'index',
140 15
				'block_instance',
141 15
			);
142 15
			extract($this->phpbb_dispatcher->trigger_event('blitze_sitemaker.modify_rendered_block', compact($vars)));
1 ignored issue
show
Bug introduced by
$this->phpbb_dispatcher-...block', compact($vars)) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
143 15
		}
144
145 17
		return $block;
146
	}
147
148
	/**
149
	 * @param string $position
150
	 * @param array $blocks
151
	 * @param array $ex_positions
152
	 * @param array $users_groups
153
	 * @param array $display_modes
154
	 * @param bool $edit_mode
155
	 */
156 17
	protected function show_position($position, array $blocks, array $ex_positions, array $users_groups, $display_modes, $edit_mode)
157
	{
158 17
		$pos_blocks = array();
159 17
		if (!$this->_exclude_position($position, $ex_positions, $edit_mode))
160 17
		{
161 17
			foreach ($blocks as $index => $entity)
162
			{
163 17
				$pos_blocks[$index] = $this->render($display_modes, $edit_mode, $entity->to_array(), $users_groups, $index);
164 17
			}
165 17
		}
166
167 17
		return array_filter($pos_blocks);
168
	}
169
170
	/**
171
	 * @param string $db_title
172
	 * @param string $df_title
173
	 * @return string
174
	 */
175 15
	protected function _get_block_title($db_title, $df_title)
176
	{
177 15
		return ($db_title) ? $db_title : $this->translator->lang($df_title);
178
	}
179
180
	/**
181
	 * @param array $returned_data
182
	 * @param bool $edit_mode
183
	 * @return string|null
184
	 */
185 15
	protected function _get_block_content(array &$returned_data, $edit_mode)
186
	{
187 15
		$content = '';
188 15
		if (!empty($returned_data['content']))
189 15
		{
190 15
			$content = $returned_data['content'];
191 15
		}
192
		else if ($edit_mode)
193 3
		{
194 1
			$returned_data['status'] = 0;
195 1
			$content = $this->translator->lang('BLOCK_NO_DATA');
196 1
		}
197
198 15
		return $content;
199
	}
200
201
	/**
202
	 * Should we display this block?
203
	 *
204
	 * @param array $data
205
	 * @param array $display_modes
206
	 * @param array $users_groups
207
	 * @param bool $edit_mode
208
	 * @return bool
209
	 */
210 17
	protected function _block_is_viewable(array $data, array $display_modes, array $users_groups, $edit_mode)
211
	{
212 17
		$type = $data['type'];
213 17
		$allowed_groups = $data['permission'];
214
215 17
		return ($display_modes[$type] && ($edit_mode || $this->_user_is_permitted($allowed_groups, $users_groups))) ? true : false;
216
	}
217
218
	/**
219
	 * @param mixed $allowed_groups
220
	 * @param array $users_groups
221
	 * @return bool
222
	 */
223 12
	protected function _user_is_permitted($allowed_groups, array $users_groups)
224
	{
225 12
		return (empty($allowed_groups) || sizeof(array_intersect($allowed_groups, $users_groups))) ? true : false;
226
	}
227
228
	/**
229
	 * @param string $position
230
	 * @param array $ex_positions
231
	 * @param bool $edit_mode
232
	 * @return bool
233
	 */
234 17
	protected function _exclude_position($position, array $ex_positions, $edit_mode)
235
	{
236 17
		return ($edit_mode === false && isset($ex_positions[$position])) ? true : false;
237
	}
238
}
239