Completed
Push — master ( efce96...e6a35d )
by Daniel
15:11
created

routes::get_parent_route()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 2
nop 5
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
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
	public $sub_route = false;
27
28
	/**
29
	 * Constructor
30
	 *
31
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
32
	 * @param \phpbb\config\config							$config					Config object
33
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
34 19
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
35
	 */
36 19
	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)
37 19
	{
38 19
		$this->cache = $cache;
39 19
		$this->config = $config;
40 19
		$this->block_factory = $block_factory;
41
		$this->mapper_factory = $mapper_factory;
42
	}
43
44
	/**
45
	 * @param string $current_route
46
	 * @param int $style_id
47
	 * @param bool|false $edit_mode
48 7
	 * @return array
49
	 */
50 7
	public function get_route_info($current_route, $style_id, $edit_mode = false)
51
	{
52 7
		$all_routes = $this->get_all_routes();
53 7
54 7
		$is_sub_route = false;
55
		if (isset($all_routes[$style_id][$current_route]))
56
		{
57
			$route_info = $all_routes[$style_id][$current_route];
58 2
		}
59
		else
60
		{
61
			$route_info = $this->get_default_route_info($all_routes, $current_route, $style_id, $edit_mode, $is_sub_route);
62
		}
63
		$route_info['is_sub_route'] = $is_sub_route;
64
65
		return $route_info;
66
	}
67
68 7
	/**
69
	 * @param array $route_info
70 7
	 * @param int $style_id
71 7
	 * @param bool $edit_mode
72
	 * @return array
73 7
	 */
74
	public function get_blocks_for_route(array $route_info, $style_id, $edit_mode)
75
	{
76
		$blocks = $this->get_cached_blocks($edit_mode);
77
		$route_id = $this->get_display_route_id($route_info, $style_id, $edit_mode);
78
79
		return (isset($blocks[$style_id][$route_id]) && !$route_info['hide_blocks']) ? $blocks[$style_id][$route_id] : array();
80
	}
81 21
82
	/**
83 21
	 * @param array $df_settings
84 21
	 * @param array $db_settings
85
	 * @return array
86 18
	 */
87 18
	public function sync_settings(array $df_settings, array $db_settings = array())
88 18
	{
89
		$settings = array();
90 18
		foreach ($df_settings as $field => $vars)
91 21
		{
92
			if (!is_array($vars))
93 21
			{
94
				continue;
95
			}
96
			$settings[$field] = $vars['default'];
97
		}
98
99
		return array_merge($settings, array_intersect_key($db_settings, $settings));
100
	}
101
102
	/**
103
	 * Clear blocks cache
104
	 */
105
	public function clear_cache()
106
	{
107
		$this->cache->destroy('sitemaker_blocks');
108
		$this->cache->destroy('sitemaker_block_routes');
109 7
	}
110
111 7
	/**
112 7
	 * @param bool $edit_mode
113 7
	 * @return array
114 7
	 */
115 7
	protected function get_cached_blocks($edit_mode)
116
	{
117 7
		if (($blocks = $this->cache->get('sitemaker_blocks')) === false || $edit_mode)
118
		{
119
			$blocks = $this->get_all_blocks();
120
			$this->cache_block($blocks, $edit_mode);
121
		}
122
123 7
		return $blocks;
124
	}
125 7
126 7
	/**
127
	 * @return array
128 7
	 */
129 7
	protected function get_all_blocks()
130
	{
131 7
		$block_mapper = $this->mapper_factory->create('blocks', 'blocks');
132 7
		$collection = $block_mapper->find();
133 7
134 7
		$blocks = array();
135
		foreach ($collection as $entity)
136 7
		{
137
			if (($block_instance = $this->block_factory->get_block($entity->get_name())) !== null)
138 7
			{
139 7
				$default_settings = $block_instance->get_config(array());
140 7
				$settings = $this->sync_settings($default_settings, $entity->get_settings());
141
142 7
				$entity->set_settings($settings);
143 7
144 7
				$style = $entity->get_style();
145
				$route_id = $entity->get_route_id();
146 7
				$position = $entity->get_position();
147
148
				$blocks[$style][$route_id][$position][] = $entity;
149
			}
150
		}
151
152 7
		return $blocks;
153
	}
154 7
155 7
	/**
156 7
	 * @return array|mixed
157 7
	 */
158
	protected function get_all_routes()
159 7
	{
160 7
		if (($all_routes = $this->cache->get('sitemaker_block_routes')) === false)
161
		{
162 7
			$route_mapper = $this->mapper_factory->create('blocks', 'routes');
163 7
			$collection = $route_mapper->find();
164 7
165 7
			$all_routes = array();
166
			foreach ($collection as $entity)
167 7
			{
168 7
				$route = $entity->get_route();
169
				$style = $entity->get_style();
170 7
				$all_routes[$style][$route] = $entity->to_array();
171
			}
172
173
			$this->cache->put('sitemaker_block_routes', $all_routes);
174
		}
175
176
		return $all_routes;
177
	}
178
179
	/**
180 2
	 * @param array $all_routes
181
	 * @param string $current_route
182 2
	 * @param int $style_id
183
	 * @param bool $edit_mode
184 2
	 * @return array
185 2
	 */
186 2
	protected function get_default_route_info(array $all_routes, $current_route, $style_id, $edit_mode, &$is_sub_route)
187 2
	{
188 2
		$default_route = $this->get_parent_route($all_routes, $current_route, $style_id, $edit_mode, $is_sub_route);
189 2
190 2
		$default_info = array(
191
			'route_id'		=> 0,
192 2
			'hide_blocks'	=> false,
193
			'ex_positions'	=> array(),
194
			'has_blocks'	=> false,
195
		);
196
197
		$route_info = ($edit_mode === false && isset($all_routes[$style_id][$default_route])) ? $all_routes[$style_id][$default_route] : $default_info;
198
		$route_info['route'] = $current_route;
199
		$route_info['style'] = $style_id;
200
201 7
		return $route_info;
202
	}
203 7
204 7
	/**
205 7
	 * @param array $all_routes
206 4
	 * @param string $current_route
207 4
	 * @param int $style_id
208 4
	 * @param bool $edit_mode
209
	 * @param bool $is_sub_route
210 7
	 * @return string
211
	 */
212
	protected function get_parent_route(array $all_routes, $current_route, $style_id, $edit_mode, &$is_sub_route)
213
	{
214
		$data = $all_routes[$style_id];
215
		$data[$current_route] = array();
216
		$routes = array_keys($data);
217 7
		sort($routes);
218
		$index = array_search($current_route, $routes);
219 7
220 7
		$default_route = $this->config['sitemaker_default_layout'];
221 6
		if ($edit_mode === false && isset($routes[$index - 1]) && strpos($current_route, $routes[$index - 1]) !== false)
222 6
		{
223 7
			$is_sub_route = true;
224
			$default_route = $routes[$index - 1];
225
		}
226
227
		return $default_route;
228
	}
229
230
	/**
231
	 * @param array $route_info
232
	 * @param int $style_id
233
	 * @param bool $edit_mode
234
	 * @return int
235
	 */
236
	protected function get_display_route_id(array $route_info, $style_id, $edit_mode)
237
	{
238
		$route_id = $route_info['route_id'];
239
		if ($edit_mode === false && !$route_info['has_blocks'])
240
		{
241
			$default_route = $this->get_route_info($this->config['sitemaker_default_layout'], $style_id, $edit_mode);
242
			$route_id = $default_route['route_id'];
243
		}
244
245
		return (int) $route_id;
246
	}
247
248
	/**
249
	 * @param array $blocks
250
	 * @param bool $edit_mode
251
	 */
252
	protected function cache_block(array $blocks, $edit_mode)
253
	{
254
		if ($edit_mode === false)
255
		{
256
			$this->cache->put('sitemaker_blocks', $blocks);
257
		}
258
	}
259
}
260