Completed
Push — develop ( f85365...cc1e8e )
by Daniel
15:30 queued 09:18
created

routes::get_parent_route_info()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
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 \blitze\sitemaker\services\blocks\factory */
21
	protected $block_factory;
22
23
	/** @var \blitze\sitemaker\model\mapper_factory */
24
	protected $mapper_factory;
25
26
	/** @var string */
27
	protected $php_ext;
28
29
	/** @var bool */
30
	protected $is_sub_route = false;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
36
	 * @param \phpbb\config\config							$config					Config object
37
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
38
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
39
	 * @param string										$php_ext				phpEx
40
	 */
41 38
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \blitze\sitemaker\services\blocks\factory $block_factory, \blitze\sitemaker\model\mapper_factory $mapper_factory, $php_ext)
42
	{
43 38
		$this->cache = $cache;
44 38
		$this->config = $config;
45 38
		$this->block_factory = $block_factory;
46 38
		$this->mapper_factory = $mapper_factory;
47 38
		$this->php_ext = $php_ext;
48 38
	}
49
50
	/**
51
	 * @param string $current_route
52
	 * @param string $page_dir
53
	 * @param int $style_id
54
	 * @param bool|false $edit_mode
55
	 * @return array
56
	 */
57 21
	public function get_route_info($current_route, $page_dir, $style_id, $edit_mode = false)
58
	{
59 21
		$routes = $this->get_routes_for_style($style_id);
60 21
		$route_info = array();
61
62
		// does route have own settings?
63 21
		if (isset($routes[$current_route]))
64 21
		{
65 13
			$route_info = $routes[$current_route];
66 13
		}
67
68
		if ($edit_mode)
69 21
		{
70 6
			$route_info += $this->get_default_route_info($current_route, $style_id);
71 6
			return $route_info;
72
		}
73
74 15
		return $this->inherit_route_info($routes, $route_info, $current_route, $page_dir, $style_id);
75
	}
76
77
	/**
78
	 * @param array $route_info
79
	 * @param int $style_id
80
	 * @param bool $edit_mode
81
	 * @return array
82
	 */
83 21
	public function get_blocks_for_route(array $route_info, $style_id, $edit_mode)
84
	{
85 21
		$blocks = $this->get_cached_blocks($edit_mode);
86 21
		$route_id = $route_info['route_id'];
87
88 21
		return (isset($blocks[$style_id][$route_id])) ? $blocks[$style_id][$route_id] : array();
89
	}
90
91
	/**
92
	 * @param array $df_settings
93
	 * @param array $db_settings
94
	 * @return array
95
	 */
96 35
	public function sync_settings(array $df_settings, array $db_settings = array())
97
	{
98 35
		$settings = array();
99 35
		foreach ($df_settings as $field => $vars)
100
		{
101 32
			if (!is_array($vars))
102 32
			{
103 32
				continue;
104
			}
105 32
			$settings[$field] = $vars['default'];
106 35
		}
107
108 35
		return array_merge($settings, array_intersect_key($db_settings, $settings));
109
	}
110
111
	/**
112
	 * Clear blocks cache
113
	 */
114 1
	public function clear_cache()
115
	{
116 1
		$this->cache->destroy('sitemaker_blocks');
117 1
		$this->cache->destroy('sitemaker_block_routes');
118 1
	}
119
120
	/**
121
	 * @param array $routes
122
	 * @param array $route_info
123
	 * @param string $current_route
124
	 * @param string $page_dir
125
	 * @param int $style_id
126
	 * @return array
127
	 */
128 15
	protected function inherit_route_info(array $routes, array $route_info, $current_route, $page_dir, $style_id)
129
	{
130
		// if block does not have own settings, inherit settings from parent route if it exists
131
		// if block has own settings but no blocks, inherit route_id and has_blocks from parent route if it exists
132 15
		if (empty($route_info['has_blocks']))
133 15
		{
134 9
			unset($route_info['route_id'], $route_info['has_blocks']);
135 9
			$route_info += $this->get_parent_route($routes, $current_route, $page_dir);
136 9
		}
137
138
		// fill in missing fields, while forcing route and style props to current route and style
139 15
		unset($route_info['style'], $route_info['route']);
140 15
		$route_info += $this->get_default_route_info($current_route, $style_id);
141
142 15
		return $this->set_display_route_id($routes, $route_info);
143
	}
144
145
	/**
146
	 * @param string $current_route
147
	 * @param int $style_id
148
	 * @return array
149
	 */
150 21
	protected function get_default_route_info($current_route, $style_id)
151
	{
152
		return array(
153 21
			'route_id'		=> 0,
154 21
			'route'			=> $current_route,
155 21
			'style'			=> $style_id,
156 21
			'hide_blocks'	=> false,
157 21
			'ex_positions'	=> array(),
158 21
			'has_blocks'	=> false,
159 21
			'is_sub_route'	=> $this->is_sub_route,
160 21
		);
161
	}
162
163
	/**
164
	 * @param array $routes
165
	 * @param string $current_route
166
	 * @param string $page_dir
167
	 * @return array
168
	 */
169 9
	protected function get_parent_route(array $routes, $current_route, $page_dir)
170
	{
171
		if ($page_dir)
172 9
		{
173 3
			$route = ltrim(dirname($page_dir) . '/index.php', './');
174 3
			$parent_route = $this->get_parent_route_info($routes, $route);
175 3
		}
176 6
		else if ($current_route === 'viewtopic.' . $this->php_ext)
177 6
		{
178 1
			$route = 'viewforum.' . $this->php_ext;
179 1
			$parent_route = $this->get_parent_route_info($routes, $route);
180 1
		}
181
		else
182
		{
183 5
			$parent_route = $this->get_virtual_parent($routes, $current_route);
184
		}
185
186 9
		return $parent_route;
187
	}
188
189
	/**
190
	 * @param array $routes_data
191
	 * @param string $current_route
192
	 * @return string
193
	 */
194 5
	protected function get_virtual_parent(array $routes_data, $current_route)
195
	{
196 5
		$routes = array_keys($routes_data);
197 5
		$routes[] = $current_route;
198 5
		sort($routes);
199 5
		$index = (int) array_search($current_route, $routes);
200
201 5
		$parent_route = array();
202 5
		if (isset($routes[$index - 1]) && strpos($current_route, $routes[$index - 1]) !== false)
203 5
		{
204 1
			$parent_route = $routes_data[$routes[$index - 1]];
205 1
			$this->is_sub_route = $parent_route['has_blocks'];
206 1
		}
207
208 5
		return $parent_route;
209
	}
210
211
	/**
212
	 * @param array $routes
213
	 * @param string $route
214
	 * @return int
215
	 */
216 4
	protected function get_parent_route_info(array $routes, $route)
217
	{
218 4
		$route_info = array();
219 4
		if (isset($routes[$route]))
220 4
		{
221 4
			$this->is_sub_route = $routes[$route]['has_blocks'];
222 4
			$route_info = $routes[$route];
223 4
		}
224
225 4
		return $route_info;
226
	}
227
228
	/**
229
	 * We get blocks to display by route id, so we update the route id here,
230
	 * to show blocks from default route if current route or it's parent has no blocks
231
	 *
232
	 * @param array $routes
233
	 * @param array $route_info
234
	 * @return array
235
	 */
236 15
	protected function set_display_route_id(array $routes, array $route_info)
237
	{
238 15
		$default_route = $this->config['sitemaker_default_layout'];
239 15
		if (!$route_info['has_blocks'] && isset($routes[$default_route]))
240 15
		{
241 3
			$route_info['route_id'] = $routes[$default_route]['route_id'];
242 3
		}
243
244 15
		return $route_info;
245
	}
246
247
	/**
248
	 * @return array
249
	 */
250 21
	protected function get_all_blocks()
251
	{
252 21
		$block_mapper = $this->mapper_factory->create('blocks', 'blocks');
253 21
		$collection = $block_mapper->find();
254
255 21
		$blocks = array();
256 21
		foreach ($collection as $entity)
257
		{
258 21
			if (($block_instance = $this->block_factory->get_block($entity->get_name())) !== null)
259 21
			{
260 21
				$default_settings = $block_instance->get_config(array());
261 21
				$settings = $this->sync_settings($default_settings, $entity->get_settings());
262
263 21
				$entity->set_settings($settings);
264
265 21
				$style = $entity->get_style();
266 21
				$route_id = $entity->get_route_id();
267 21
				$position = $entity->get_position();
268
269 21
				$blocks[$style][$route_id][$position][] = $entity;
270 21
			}
271 21
		}
272
273 21
		return $blocks;
274
	}
275
276
	/**
277
	 * @return array|mixed
278
	 */
279 21
	protected function get_all_routes()
280
	{
281 21
		if (($all_routes = $this->cache->get('sitemaker_block_routes')) === false)
282 21
		{
283 21
			$route_mapper = $this->mapper_factory->create('blocks', 'routes');
284 21
			$collection = $route_mapper->find();
285
286 21
			$all_routes = array();
287 21
			foreach ($collection as $entity)
288
			{
289 21
				$route = $entity->get_route();
290 21
				$style = $entity->get_style();
291 21
				$all_routes[$style][$route] = $entity->to_array();
292 21
			}
293
294 21
			$this->cache->put('sitemaker_block_routes', $all_routes);
295 21
		}
296
297 21
		return $all_routes;
298
	}
299
300
	/**
301
	 * @param int $style_id
302
	 * @return array
303
	 */
304 21
	protected function get_routes_for_style($style_id)
305
	{
306 21
		$all_routes = $this->get_all_routes();
307 21
		return (isset($all_routes[$style_id])) ? $all_routes[$style_id] : array();
308
	}
309
310
	/**
311
	 * @param bool $edit_mode
312
	 * @return array
313
	 */
314 21
	protected function get_cached_blocks($edit_mode)
315
	{
316 21
		if (($blocks = $this->cache->get('sitemaker_blocks')) === false || $edit_mode)
317 21
		{
318 21
			$blocks = $this->get_all_blocks();
319 21
			$this->cache_block($blocks, $edit_mode);
320 21
		}
321
322 21
		return $blocks;
323
	}
324
325
	/**
326
	 * @param array $blocks
327
	 * @param bool $edit_mode
328
	 */
329 21
	protected function cache_block(array $blocks, $edit_mode)
330
	{
331 21
		if ($edit_mode === false)
332 21
		{
333 16
			$this->cache->put('sitemaker_blocks', $blocks);
334 16
		}
335 21
	}
336
}
337