Passed
Push — templating ( 0cf8c2...d1ec7e )
by Daniel
03:46
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
	 * Render block
101
	 *
102
	 * @param array $display_modes
103
	 * @param bool $edit_mode
104
	 * @param array $db_data
105
	 * @param array $users_groups
106
	 * @param int $index
107
	 * @return string[]
108
	 */
109
	public function render(array $display_modes, $edit_mode, array $db_data, array $users_groups, $index)
110
	{
111
		$service_name = $db_data['name'];
112
113
		$block = array();
114
		if ($this->block_is_viewable($db_data, $display_modes, $users_groups, $edit_mode) && ($block_instance = $this->block_factory->get_block($service_name)) !== null)
115
		{
116
			$returned_data = array_merge(
117
				array(
118
					'title'		=> '',
119
					'data'		=> null,
120
					'content'	=> null,
121
				),
122
				$block_instance->display($db_data, $edit_mode)
123
			);
124
125
			// we get the template after running 'display()' above so template can be set dynamically
126
			$returned_data['template']	= $block_instance->get_template();
127
128
			if ($this->block_has_content($returned_data, $edit_mode))
129
			{
130
				$returned_data['title'] = $this->get_block_title($db_data['title'], $returned_data['title']);
131
132
				$block = array_merge($db_data, $returned_data);
133
				$block['class'] .= self::$status_class[$block['status']];
134
			}
135
136
			/**
137
			 * Event to modify a rendered block.
138
			 *
139
			 * @event blitze.sitemaker.modify_rendered_block
140
			 * @var	array														block			Array of block properties
141
			 * @var	int															index			Display order/index in position
142
			 * @var	\blitze\sitemaker\services\blocks\driver\block_interface	block_instance	The block instance
143
			 * @since 3.0.1-RC1
144
			 */
145
			$vars = array('block', 'index', 'block_instance');
146
			extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.modify_rendered_block', compact($vars)));
147
		}
148
149
		return $block;
150
	}
151
152
	/**
153
	 * @param string $position
154
	 * @param array $blocks
155
	 * @param array $ex_positions
156
	 * @param array $users_groups
157
	 * @param array $display_modes
158
	 * @param bool $edit_mode
159
	 * @return array[]
160
	 */
161
	protected function show_position($position, array $blocks, array $ex_positions, array $users_groups, $display_modes, $edit_mode)
162
	{
163
		$pos_blocks = array();
164
		if (!$this->exclude_position($position, $ex_positions, $edit_mode))
165
		{
166
			foreach ($blocks as $index => $entity)
167
			{
168
				$pos_blocks[$index] = $this->render($display_modes, $edit_mode, $entity->to_array(), $users_groups, $index);
169
			}
170
		}
171
172
		return array_filter($pos_blocks);
173
	}
174
175
	/**
176
	 * @param string $db_title
177
	 * @param string $df_title
178
	 * @return string
179
	 */
180
	protected function get_block_title($db_title, $df_title)
181
	{
182
		return ($db_title) ? $db_title : $this->translator->lang($df_title);
183
	}
184
185
	/**
186
	 * @param array $returned_data
187
	 * @param bool $edit_mode
188
	 * @return bool
189
	 */
190
	protected function block_has_content(array &$returned_data, $edit_mode)
191
	{
192
        if ($this->block_returns_nothing($returned_data))
193
        {
194
            return $this->set_edit_mode_content($returned_data, $edit_mode, 'BLOCK_NO_DATA');
195
		}
196
		else if ($this->block_is_missing_template($returned_data))
197
		{
198
			$returned_data['data'] = null;
199
			return $this->set_edit_mode_content($returned_data, $edit_mode, 'BLOCK_MISSING_TEMPLATE');
200
		}
201
202
		return true;
203
	}
204
205
	/**
206
	 * @param array $data
207
     * @param bool $edit_mode
208
     * @param string $lang_key
209
	 * @return bool
210
	 */
211
	protected function set_edit_mode_content(array &$data, $edit_mode, $lang_key)
212
	{
213
        if ($edit_mode)
214
        {
215
            $data['status'] = 0;
216
            $data['content'] = $this->translator->lang($lang_key);
217
218
            return true;
219
        }
220
221
        return false;
222
	}
223
224
	/**
225
	 * @param array $returned_data
226
	 * @return bool
227
	 */
228
	protected function block_returns_nothing(array $returned_data)
229
	{
230
		return !$returned_data['content'] && empty($returned_data['data']);
231
	}
232
233
    /**
234
     * @param array $returned_data
235
     * @return bool
236
     */
237
    protected function block_is_missing_template(array $returned_data)
238
    {
239
        return is_array($returned_data['data']) && !$returned_data['template'];
240
    }
241
242
	/**
243
	 * Should we display this block?
244
	 *
245
	 * @param array $data
246
	 * @param array $display_modes
247
	 * @param array $users_groups
248
	 * @param bool $edit_mode
249
	 * @return bool
250
	 */
251
	protected function block_is_viewable(array $data, array $display_modes, array $users_groups, $edit_mode)
252
	{
253
		$type = $data['type'];
254
		$allowed_groups = $data['permission'];
255
256
		return ($display_modes[$type] && ($edit_mode || $this->user_is_permitted($allowed_groups, $users_groups))) ? true : false;
257
	}
258
259
	/**
260
	 * @param mixed $allowed_groups
261
	 * @param array $users_groups
262
	 * @return bool
263
	 */
264
	protected function user_is_permitted($allowed_groups, array $users_groups)
265
	{
266
		return (empty($allowed_groups) || sizeof(array_intersect($allowed_groups, $users_groups))) ? true : false;
267
	}
268
269
	/**
270
	 * @param string $position
271
	 * @param array $ex_positions
272
	 * @param bool $edit_mode
273
	 * @return bool
274
	 */
275
	protected function exclude_position($position, array $ex_positions, $edit_mode)
276
	{
277
		return ($edit_mode === false && isset($ex_positions[$position])) ? true : false;
278
	}
279
}
280