Completed
Push — master ( 2f974b...edb932 )
by Daniel
08:50
created

blocks   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 25
c 4
b 1
f 0
lcom 1
cbo 3
dl 0
loc 186
ccs 65
cts 65
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A display() 0 20 2
A render() 0 21 4
A show_position() 0 10 3
A _get_block_title() 0 4 2
A _block_is_viewable() 0 7 4
A _user_is_permitted() 0 4 3
A _exclude_position() 0 4 3
A _get_block_content() 0 14 3
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 7
	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 7
		parent::__construct($cache, $config, $block_factory, $mapper_factory);
45
46 7
		$this->template = $template;
47 7
		$this->user = $user;
48 7
		$this->block_factory = $block_factory;
49 7
		$this->groups = $groups;
50 7
	}
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 7
	public function display($edit_mode, array $route_info, $style_id, array $display_modes)
61
	{
62 7
		$ex_positions = $route_info['ex_positions'];
63 7
		$users_groups = $this->groups->get_users_groups();
64
65 7
		$positions = $this->get_blocks_for_route($route_info, $style_id, $edit_mode);
66
67 7
		$blocks_per_position = array();
68
69 7
		foreach ($positions as $position => $blocks)
70
		{
71 5
			$pos_count_key = "s_{$position}_count";
72 5
			$blocks_per_position[$pos_count_key] = 0;
73
74 5
			$this->show_position($position, $blocks, $ex_positions, $users_groups, $blocks_per_position[$pos_count_key], $display_modes, $edit_mode);
0 ignored issues
show
Documentation introduced by
$blocks_per_position[$pos_count_key] is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75 7
		}
76
77 7
		$this->template->assign_var('S_HAS_BLOCKS', sizeof($positions));
78 7
		$this->template->assign_vars(array_change_key_case($blocks_per_position, CASE_UPPER));
79 7
	}
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 5
	public function render(array $display_modes, $edit_mode, array $data, array $users_groups, &$position_counter)
91
	{
92 5
		$position = $data['position'];
93 5
		$service_name = $data['name'];
94
95 5
		if ($this->_block_is_viewable($data, $display_modes, $users_groups, $edit_mode) && ($block_instance = $this->block_factory->get_block($service_name)) !== null)
96 5
		{
97 5
			$block = $block_instance->display($data, $edit_mode);
98
99 5
			if ($content = $this->_get_block_content($block, $edit_mode))
100 5
			{
101 5
				$tpl_data = array_merge($data, array(
102 5
					'TITLE'		=> $this->_get_block_title($data['title'], $block['title']),
103 5
					'CONTENT'	=> $content,
104 5
				));
105
106 5
				$this->template->assign_block_vars($position, array_change_key_case($tpl_data, CASE_UPPER));
107 5
				$position_counter++;
108 5
			}
109 5
		}
110 5
	}
111
112
	/**
113
	 * @param string $position
114
	 * @param array $blocks
115
	 * @param array $ex_positions
116
	 * @param array $users_groups
117
	 * @param array $position_counter
118
	 * @param array $display_modes
119
	 * @param bool $edit_mode
120
	 */
121 5
	protected function show_position($position, array $blocks, array $ex_positions, array $users_groups, &$position_counter, $display_modes, $edit_mode)
122
	{
123 5
		if (!$this->_exclude_position($position, $ex_positions, $edit_mode))
124 5
		{
125 5
			foreach ($blocks as $entity)
126
			{
127 5
				$this->render($display_modes, $edit_mode, $entity->to_array(), $users_groups, $position_counter);
0 ignored issues
show
Bug introduced by
It seems like $position_counter defined by parameter $position_counter on line 121 can also be of type array; however, blitze\sitemaker\services\blocks\blocks::render() does only seem to accept integer, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
128 5
			}
129 5
		}
130 5
	}
131
132
	/**
133
	 * @param string $db_title
134
	 * @param string $df_title
135
	 * @return string
136
	 */
137 5
	protected function _get_block_title($db_title, $df_title)
138
	{
139 5
		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 5
	protected function _get_block_content(array $block, $edit_mode)
148
	{
149 5
		$content = '';
150 5
		if (!empty($block['content']))
151 5
		{
152 5
			$content = $block['content'];
153 5
		}
154
		else if ($edit_mode)
155 4
		{
156 1
			$content = $this->user->lang('BLOCK_NO_DATA');
157 1
		}
158
159 5
		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 5
	protected function _block_is_viewable(array $data, array $display_modes, array $users_groups, $edit_mode)
172
	{
173 5
		$type = $data['type'];
174 5
		$allowed_groups = $data['permission'];
175
176 5
		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 5
	protected function _exclude_position($position, array $ex_positions, $edit_mode)
196
	{
197 5
		return ($edit_mode === false && isset($ex_positions[$position])) ? true : false;
198
	}
199
}
200