Completed
Push — master ( 926e5e...2f974b )
by Daniel
21:21
created

blocks::_block_is_viewable()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.2
cc 4
eloc 4
nc 6
nop 4
crap 4
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
use blitze\sitemaker\services\blocks\routes;
13
14
class blocks extends routes
15
{
16
	/** @var \phpbb\config\config */
17
	protected $config;
18
19
	/** @var \phpbb\template\template */
20
	protected $template;
21
22
	/** @var \phpbb\user */
23
	protected $user;
24
25
	/** @var \blitze\sitemaker\services\blocks\factory */
26
	protected $block_factory;
27
28
	/** @var \blitze\sitemaker\services\groups */
29
	protected $groups;
30
31
	/**
32
	 * Constructor
33
	 *
34
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
35
	 * @param \phpbb\config\config							$config					Config object
36
	 * @param \phpbb\template\template						$template				Template object
37
	 * @param \phpbb\user									$user					User object
38
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
39
	 * @param \blitze\sitemaker\services\groups				$groups					Groups Object
40
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
41
	 */
42 6
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\template\template $template, \phpbb\user $user, \blitze\sitemaker\services\blocks\factory $block_factory, \blitze\sitemaker\services\groups $groups, \blitze\sitemaker\model\mapper_factory $mapper_factory)
43
	{
44 6
		parent::__construct($cache, $config, $block_factory, $mapper_factory);
45
46 6
		$this->template = $template;
47 6
		$this->user = $user;
48 6
		$this->block_factory = $block_factory;
49 6
		$this->groups = $groups;
50 6
	}
51
52
	/**
53
	 * Display blocks for current route
54
	 *
55
	 * @param bool $edit_mode
56
	 * @param array $route_info
57
	 * @param int $style_id
58
	 * @param $display_modes
59
	 */
60 6
	public function display($edit_mode, array $route_info, $style_id, array $display_modes)
61
	{
62 6
		$ex_positions = $route_info['ex_positions'];
63 6
		$users_groups = $this->groups->get_users_groups();
64
65 6
		$positions = $this->get_blocks_for_route($route_info, $style_id, $edit_mode);
66
67 6
		$blocks_per_position = array();
68
69 6
		foreach ($positions as $position => $blocks)
70
		{
71 4
			$pos_count_key = "s_{$position}_count";
72 4
			$blocks_per_position[$pos_count_key] = 0;
73
74 4
			$this->show_position($position, $blocks, $ex_positions, $users_groups, $blocks_per_position[$pos_count_key], $display_modes, $edit_mode);
75 6
		}
76
77 6
		$this->template->assign_var('S_HAS_BLOCKS', sizeof($positions));
78 6
		$this->template->assign_vars(array_change_key_case($blocks_per_position, CASE_UPPER));
79 6
	}
80
81
	/**
82
	 * Render block
83
	 *
84
	 * @param array $display_modes
85
	 * @param bool $edit_mode
86
	 * @param array $data
87
	 * @param array $users_groups
88
	 * @param int $position_counter
89
	 */
90 4
	public function render(array $display_modes, $edit_mode, array $data, array $users_groups, &$position_counter)
91
	{
92 4
		$position = $data['position'];
93 4
		$service_name = $data['name'];
94
95 4
		if ($this->_block_is_viewable($data, $display_modes, $users_groups, $edit_mode) && ($block_instance = $this->block_factory->get_block($service_name)) !== null)
96 4
		{
97 4
			$block = $block_instance->display($data, $edit_mode);
98
99 4
			if ($content = $this->_get_block_content($block, $edit_mode))
100 4
			{
101 4
				$tpl_data = array_merge($data, array(
102 4
					'TITLE'		=> $this->_get_block_title($data['title'], $block['title']),
103 4
					'CONTENT'	=> $content,
104 4
				));
105
106 4
				$this->template->assign_block_vars($position, array_change_key_case($tpl_data, CASE_UPPER));
107 4
				$position_counter++;
108 4
			}
109 4
		}
110 4
	}
111
112
	/**
113
	 * @param string $position
114
	 * @param array $blocks
115
	 * @param array $ex_positions
116
	 * @param array $users_groups
117
	 * @param array $blocks_per_position
0 ignored issues
show
Documentation introduced by
There is no parameter named $blocks_per_position. Did you maybe mean $position?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
118
	 * @param array $display_modes
119
	 * @param bool $edit_mode
120
	 */
121 4
	protected function show_position($position, array $blocks, array $ex_positions, array $users_groups, &$position_counter, $display_modes, $edit_mode)
122
	{
123 4
		if (!$this->_exclude_position($position, $ex_positions, $edit_mode))
124 4
		{
125 4
			foreach ($blocks as $entity)
126
			{
127 4
				$this->render($display_modes, $edit_mode, $entity->to_array(), $users_groups, $position_counter);
128 4
			}
129 4
		}
130 4
	}
131
132
	/**
133
	 * @param string $db_title
134
	 * @param string $df_title
135
	 * @return string
136
	 */
137 4
	protected function _get_block_title($db_title, $df_title)
138
	{
139 4
		return ($db_title) ? $db_title : $this->user->lang($df_title);
140
	}
141
142
	/**
143
	 * @param array $block
144
	 * @param bool $edit_mode
145
	 * @return string|null
146
	 */
147 4
	protected function _get_block_content(array $block, $edit_mode)
148
	{
149 4
		$content = '';
150 4
		if (!empty($block['content']))
151 4
		{
152 4
			$content = $block['content'];
153 4
		}
154
		else if ($edit_mode)
155
		{
156
			$content = $this->user->lang('BLOCK_NO_DATA');
157
		}
158
159 4
		return $content;
160
	}
161
162
	/**
163
	 * Should we display this block?
164
	 *
165
	 * @param array $data
166
	 * @param array $display_modes
167
	 * @param array $users_groups
168
	 * @param bool $edit_mode
169
	 * @return bool
170
	 */
171 4
	protected function _block_is_viewable(array $data, array $display_modes, array $users_groups, $edit_mode)
172
	{
173 4
		$type = $data['type'];
174 4
		$allowed_groups = $data['permission'];
175
176 4
		return ($display_modes[$type] && ($edit_mode || $this->_user_is_permitted($allowed_groups, $users_groups))) ? true : false;
177
	}
178
179
	/**
180
	 * @param mixed $allowed_groups
181
	 * @param array $users_groups
182
	 * @return bool
183
	 */
184 4
	protected function _user_is_permitted($allowed_groups, array $users_groups)
185
	{
186 4
		return (empty($allowed_groups) || sizeof(array_intersect($allowed_groups, $users_groups))) ? true : false;
187
	}
188
189
	/**
190
	 * @param string $position
191
	 * @param array $ex_positions
192
	 * @param bool $edit_mode
193
	 * @return bool
194
	 */
195 4
	protected function _exclude_position($position, array $ex_positions, $edit_mode)
196
	{
197 4
		return ($edit_mode === false && isset($ex_positions[$position])) ? true : false;
198
	}
199
}
200