Completed
Push — master ( 45ff52...3a2a32 )
by Daniel
09:47
created

routes::set_display_route_id()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
crap 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
class routes
13
{
14
	/** @var \phpbb\cache\driver\driver_interface */
15
	protected $cache;
16
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\user */
21
	protected $user;
22
23
	/** @var \blitze\sitemaker\services\blocks\factory */
24
	protected $block_factory;
25
26
	/** @var \blitze\sitemaker\model\mapper_factory */
27
	protected $mapper_factory;
28
29
	/** @var string */
30
	protected $php_ext;
31
32
	/** @var bool */
33
	protected $is_sub_route = false;
34
35
	/**
36
	 * Constructor
37
	 *
38
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
39
	 * @param \phpbb\config\config							$config					Config object
40
	 * @param \phpbb\user									$user					User object
41
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
42
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
43
	 * @param string										$php_ext				phpEx
44
	 */
45 35
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\user $user, \blitze\sitemaker\services\blocks\factory $block_factory, \blitze\sitemaker\model\mapper_factory $mapper_factory, $php_ext)
46
	{
47 35
		$this->cache = $cache;
48 35
		$this->config = $config;
49 35
		$this->user = $user;
50 35
		$this->block_factory = $block_factory;
51 35
		$this->mapper_factory = $mapper_factory;
52 35
		$this->php_ext = $php_ext;
53 35
	}
54
55
	/**
56
	 * @param string $current_route
57
	 * @param string $page_dir
58
	 * @param int $style_id
59
	 * @param bool|false $edit_mode
60
	 * @return array
61
	 */
62 21
	public function get_route_info($current_route, $page_dir, $style_id, $edit_mode = false)
63
	{
64 21
		$routes = $this->get_routes_for_style($style_id);
65 21
		$route_info = array();
66
67
		// does route have own settings?
68 21
		if (isset($routes[$current_route]))
69 21
		{
70 13
			$route_info = $routes[$current_route];
71 13
		}
72
73
		if ($edit_mode)
74 21
		{
75 6
			$route_info += $this->get_default_route_info($current_route, $style_id);
76 21
			return $route_info;
77
		}
78
79 15
		return $this->inherit_route_info($routes, $route_info, $current_route, $page_dir, $style_id);
80
	}
81
82
	/**
83
	 * @param array $route_info
84
	 * @param int $style_id
85
	 * @param bool $edit_mode
86
	 * @return array
87
	 */
88 21
	public function get_blocks_for_route(array $route_info, $style_id, $edit_mode)
89
	{
90 21
		$blocks = $this->get_cached_blocks($edit_mode);
91 21
		$route_id = $route_info['route_id'];
92
93 21
		return (isset($blocks[$style_id][$route_id])) ? $blocks[$style_id][$route_id] : array();
94
	}
95
96
	/**
97
	 * @param array $df_settings
98
	 * @param array $db_settings
99
	 * @return array
100
	 */
101 35
	public function sync_settings(array $df_settings, array $db_settings = array())
102
	{
103 35
		$settings = array();
104 35
		foreach ($df_settings as $field => $vars)
105
		{
106 32
			if (!is_array($vars))
107 32
			{
108 32
				continue;
109
			}
110 32
			$settings[$field] = $vars['default'];
111 35
		}
112
113 35
		return array_merge($settings, array_intersect_key($db_settings, $settings));
114
	}
115
116
	/**
117
	 * Clear blocks cache
118
	 */
119
	public function clear_cache()
120
	{
121
		$this->cache->destroy('sitemaker_blocks');
122
		$this->cache->destroy('sitemaker_block_routes');
123
	}
124
125
	/**
126
	 * @param array $routes
127
	 * @param array $route_info
128
	 * @param string $current_route
129
	 * @param string $page_dir
130
	 * @param int $style_id
131
	 * @return array
132
	 */
133 15
	protected function inherit_route_info(array $routes, array $route_info, $current_route, $page_dir, $style_id)
134
	{
135
		// if block does not have own settings, inherit settings from parent route if it exists
136
		// if block has own settings but no blocks, inherit route_id and has_blocks from parent route if it exists
137 15
		if (empty($route_info['has_blocks']))
138 15
		{
139 9
			unset($route_info['route_id'], $route_info['has_blocks']);
140 9
			$route_info += $this->get_parent_route($routes, $current_route, $page_dir, $style_id);
0 ignored issues
show
Unused Code introduced by
The call to routes::get_parent_route() has too many arguments starting with $style_id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
141 9
		}
142
143
		// fill in missing fields, while forcing route and style props to current route and style
144 15
		unset($route_info['style'], $route_info['route']);
145 15
		$route_info += $this->get_default_route_info($current_route, $style_id);
146
147 15
		return $this->set_display_route_id($routes, $route_info);
148
	}
149
150
	/**
151
	 * @param string $current_route
152
	 * @param int $style_id
153
	 * @return array
154
	 */
155 21
	protected function get_default_route_info($current_route, $style_id)
156
	{
157
		return array(
158 21
			'route_id'		=> 0,
159 21
			'route'			=> $current_route,
160 21
			'style'			=> $style_id,
161 21
			'hide_blocks'	=> false,
162 21
			'ex_positions'	=> array(),
163 21
			'has_blocks'	=> false,
164 21
			'is_sub_route'	=> $this->is_sub_route,
165 21
		);
166
	}
167
168
	/**
169
	 * @param array $routes
170
	 * @param string $current_route
171
	 * @param string $page_dir
172
	 * @return array
173
	 */
174 9
	protected function get_parent_route(array $routes, $current_route, $page_dir)
175
	{
176
		if ($page_dir)
177 9
		{
178 3
			$route = ltrim(dirname($page_dir) . '/index.php', './');
179 3
			$parent_route = $this->get_parent_route_info($routes, $route);
180 3
		}
181 6
		else if ($current_route === 'viewtopic.' . $this->php_ext)
182 6
		{
183 1
			$route = 'viewforum.' . $this->php_ext;
184 1
			$parent_route = $this->get_parent_route_info($routes, $route);
185 1
		}
186
		else
187
		{
188 5
			$parent_route = $this->get_virtual_parent($routes, $current_route);
189
		}
190
191 9
		return $parent_route;
192
	}
193
194
	/**
195
	 * @param array $routes_data
196
	 * @param string $current_route
197
	 * @return string
198
	 */
199 5
	protected function get_virtual_parent(array $routes_data, $current_route)
200
	{
201 5
		$routes = array_keys($routes_data);
202 5
		$routes[] = $current_route;
203 5
		sort($routes);
204 5
		$index = (int) array_search($current_route, $routes);
205
206 5
		$parent_route = array();
207 5
		if (isset($routes[$index - 1]) && strpos($current_route, $routes[$index - 1]) !== false)
208 5
		{
209 1
			$parent_route = $routes_data[$routes[$index - 1]];
210 1
			$this->is_sub_route = $parent_route['has_blocks'];
211 1
		}
212
213 5
		return $parent_route;
214
	}
215
216
	/**
217
	 * @param array $routes
218
	 * @param string $route
219
	 * @return int
220
	 */
221 4
	protected function get_parent_route_info(array $routes, $route)
222
	{
223 4
		$route_info = array();
224 4
		if (isset($routes[$route]))
225 4
		{
226 4
			$this->is_sub_route = $routes[$route]['has_blocks'];
227 4
			$route_info = $routes[$route];
228 4
		}
229
230 4
		return $route_info;
231
	}
232
233
	/**
234
	 * We get blocks to display by route id, so we update the route id here,
235
	 * to show blocks from default route if current route or it's parent has no blocks
236
	 *
237
	 * @param array $routes
238
	 * @param array $route_info
239
	 * @return array
240
	 */
241 15
	protected function set_display_route_id(array $routes, array $route_info)
242
	{
243 15
		$default_route = $this->config['sitemaker_default_layout'];
244 15
		if (!$route_info['has_blocks'] && isset($routes[$default_route]))
245 15
		{
246 3
			$route_info['route_id'] = $routes[$default_route]['route_id'];
247 3
		}
248
249 15
		return $route_info;
250
	}
251
252
	/**
253
	 * @return array
254
	 */
255 21
	protected function get_all_blocks()
256
	{
257 21
		$block_mapper = $this->mapper_factory->create('blocks', 'blocks');
258 21
		$collection = $block_mapper->find();
259
260 21
		$blocks = array();
261 21
		foreach ($collection as $entity)
262
		{
263 21
			if (($block_instance = $this->block_factory->get_block($entity->get_name())) !== null)
264 21
			{
265 21
				$default_settings = $block_instance->get_config(array());
266 21
				$settings = $this->sync_settings($default_settings, $entity->get_settings());
267
268 21
				$entity->set_settings($settings);
269
270 21
				$style = $entity->get_style();
271 21
				$route_id = $entity->get_route_id();
272 21
				$position = $entity->get_position();
273
274 21
				$blocks[$style][$route_id][$position][] = $entity;
275 21
			}
276 21
		}
277
278 21
		return $blocks;
279
	}
280
281
	/**
282
	 * @return array|mixed
283
	 */
284 21
	protected function get_all_routes()
285
	{
286 21
		if (($all_routes = $this->cache->get('sitemaker_block_routes')) === false)
287 21
		{
288 21
			$route_mapper = $this->mapper_factory->create('blocks', 'routes');
289 21
			$collection = $route_mapper->find();
290
291 21
			$all_routes = array();
292 21
			foreach ($collection as $entity)
293
			{
294 21
				$route = $entity->get_route();
295 21
				$style = $entity->get_style();
296 21
				$all_routes[$style][$route] = $entity->to_array();
297 21
			}
298
299 21
			$this->cache->put('sitemaker_block_routes', $all_routes);
300 21
		}
301
302 21
		return $all_routes;
303
	}
304
305
	/**
306
	 * @param int $style_id
307
	 * @return array
308
	 */
309 21
	protected function get_routes_for_style($style_id)
310
	{
311 21
		$all_routes = $this->get_all_routes();
312 21
		return (isset($all_routes[$style_id])) ? $all_routes[$style_id] : array();
313
	}
314
315
	/**
316
	 * @param bool $edit_mode
317
	 * @return array
318
	 */
319 21
	protected function get_cached_blocks($edit_mode)
320
	{
321 21
		if (($blocks = $this->cache->get('sitemaker_blocks')) === false || $edit_mode)
322 21
		{
323 21
			$blocks = $this->get_all_blocks();
324 21
			$this->cache_block($blocks, $edit_mode);
325 21
		}
326
327 21
		return $blocks;
328
	}
329
330
	/**
331
	 * @param array $blocks
332
	 * @param bool $edit_mode
333
	 */
334 21
	protected function cache_block(array $blocks, $edit_mode)
335
	{
336 21
		if ($edit_mode === false)
337 21
		{
338 16
			$this->cache->put('sitemaker_blocks', $blocks);
339 16
		}
340 21
	}
341
}
342