Completed
Push — master ( db8e66...00afbe )
by Daniel
09:03
created

routes   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 296
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 4
dl 0
loc 296
ccs 120
cts 124
cp 0.9677
rs 9.2
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A get_route_info() 0 17 2
A get_blocks_for_route() 0 7 3
A sync_settings() 0 14 3
A clear_cache() 0 5 1
A get_cached_blocks() 0 10 3
B get_all_blocks() 0 25 3
A get_all_routes() 0 20 3
A get_default_route_info() 0 20 3
A get_parent_route() 0 17 2
A get_virtual_parent() 0 16 3
A get_routes_for_style() 0 4 2
A get_display_route_id() 0 11 3
A cache_block() 0 7 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 \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 phpEx */
30
	protected $php_ext;
31
32
	public $sub_route = false;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
38
	 * @param \phpbb\config\config							$config					Config object
39
	 * @param \phpbb\user									$user					User object
40
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
41
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
42
	 * @param string										$php_ext				phpEx
43
	 */
44 33
	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)
45
	{
46 33
		$this->cache = $cache;
47 33
		$this->config = $config;
48 33
		$this->user = $user;
49 33
		$this->block_factory = $block_factory;
50 33
		$this->mapper_factory = $mapper_factory;
51 33
		$this->php_ext = $php_ext;
52 33
	}
53
54
	/**
55
	 * @param string $current_route
56
	 * @param string $page_dir
57
	 * @param int $style_id
58
	 * @param bool|false $edit_mode
59
	 * @return array
60
	 */
61 19
	public function get_route_info($current_route, $page_dir, $style_id, $edit_mode = false)
62
	{
63 19
		$all_routes = $this->get_all_routes();
64
65 19
		$is_sub_route = false;
66 19
		if (isset($all_routes[$style_id][$current_route]))
67 19
		{
68 14
			$route_info = $all_routes[$style_id][$current_route];
69 14
		}
70
		else
71
		{
72 9
			$route_info = $this->get_default_route_info($all_routes, $current_route, $page_dir, $style_id, $edit_mode, $is_sub_route);
73
		}
74 19
		$route_info['is_sub_route'] = $is_sub_route;
75
76 19
		return $route_info;
77
	}
78
79
	/**
80
	 * @param array $route_info
81
	 * @param int $style_id
82
	 * @param bool $edit_mode
83
	 * @return array
84
	 */
85 19
	public function get_blocks_for_route(array $route_info, $style_id, $edit_mode)
86
	{
87 19
		$blocks = $this->get_cached_blocks($edit_mode);
88 19
		$route_id = $this->get_display_route_id($route_info, $style_id, $edit_mode);
89
90 19
		return (isset($blocks[$style_id][$route_id]) && !$route_info['hide_blocks']) ? $blocks[$style_id][$route_id] : array();
91
	}
92
93
	/**
94
	 * @param array $df_settings
95
	 * @param array $db_settings
96
	 * @return array
97
	 */
98 33
	public function sync_settings(array $df_settings, array $db_settings = array())
99
	{
100 33
		$settings = array();
101 33
		foreach ($df_settings as $field => $vars)
102
		{
103 30
			if (!is_array($vars))
104 30
			{
105 30
				continue;
106
			}
107 30
			$settings[$field] = $vars['default'];
108 33
		}
109
110 33
		return array_merge($settings, array_intersect_key($db_settings, $settings));
111
	}
112
113
	/**
114
	 * Clear blocks cache
115
	 */
116
	public function clear_cache()
117
	{
118
		$this->cache->destroy('sitemaker_blocks');
119
		$this->cache->destroy('sitemaker_block_routes');
120
	}
121
122
	/**
123
	 * @param bool $edit_mode
124
	 * @return array
125
	 */
126 19
	protected function get_cached_blocks($edit_mode)
127
	{
128 19
		if (($blocks = $this->cache->get('sitemaker_blocks')) === false || $edit_mode)
129 19
		{
130 19
			$blocks = $this->get_all_blocks();
131 19
			$this->cache_block($blocks, $edit_mode);
132 19
		}
133
134 19
		return $blocks;
135
	}
136
137
	/**
138
	 * @return array
139
	 */
140 19
	protected function get_all_blocks()
141
	{
142 19
		$block_mapper = $this->mapper_factory->create('blocks', 'blocks');
143 19
		$collection = $block_mapper->find();
144
145 19
		$blocks = array();
146 19
		foreach ($collection as $entity)
147
		{
148 19
			if (($block_instance = $this->block_factory->get_block($entity->get_name())) !== null)
149 19
			{
150 19
				$default_settings = $block_instance->get_config(array());
151 19
				$settings = $this->sync_settings($default_settings, $entity->get_settings());
152
153 19
				$entity->set_settings($settings);
154
155 19
				$style = $entity->get_style();
156 19
				$route_id = $entity->get_route_id();
157 19
				$position = $entity->get_position();
158
159 19
				$blocks[$style][$route_id][$position][] = $entity;
160 19
			}
161 19
		}
162
163 19
		return $blocks;
164
	}
165
166
	/**
167
	 * @return array|mixed
168
	 */
169 19
	protected function get_all_routes()
170
	{
171 19
		if (($all_routes = $this->cache->get('sitemaker_block_routes')) === false)
172 19
		{
173 19
			$route_mapper = $this->mapper_factory->create('blocks', 'routes');
174 19
			$collection = $route_mapper->find();
175
176 19
			$all_routes = array();
177 19
			foreach ($collection as $entity)
178
			{
179 19
				$route = $entity->get_route();
180 19
				$style = $entity->get_style();
181 19
				$all_routes[$style][$route] = $entity->to_array();
182 19
			}
183
184 19
			$this->cache->put('sitemaker_block_routes', $all_routes);
185 19
		}
186
187 19
		return $all_routes;
188
	}
189
190
	/**
191
	 * @param array $all_routes
192
	 * @param string $current_route
193
	 * @param string $page_dir
194
	 * @param int $style_id
195
	 * @param bool $edit_mode
196
	 * @return array
197
	 */
198 9
	protected function get_default_route_info(array $all_routes, $current_route, $page_dir, $style_id, $edit_mode, &$is_sub_route)
199
	{
200
		$default_info = array(
201 9
			'route_id'		=> 0,
202 9
			'hide_blocks'	=> false,
203 9
			'ex_positions'	=> array(),
204 9
			'has_blocks'	=> false,
205 9
		);
206
207 9
		if (!$edit_mode)
208 9
		{
209 7
			$default_route = $this->get_parent_route($all_routes, $current_route, $page_dir, $style_id, $is_sub_route);
210 7
			$default_info = (isset($all_routes[$style_id][$default_route])) ? $all_routes[$style_id][$default_route] : $default_info;
211 7
		}
212
213 9
		$default_info['route'] = $current_route;
214 9
		$default_info['style'] = $style_id;
215
216 9
		return $default_info;
217
	}
218
219
	/**
220
	 * @param array $all_routes
221
	 * @param string $current_route
222
	 * @param string $page_dir
223
	 * @param int $style_id
224
	 * @param bool $is_sub_route
225
	 * @return string
226
	 */
227 7
	protected function get_parent_route(array $all_routes, $current_route, $page_dir, $style_id, &$is_sub_route)
228
	{
229 7
		$parent_route = $this->config['sitemaker_default_layout'];
0 ignored issues
show
Unused Code introduced by
$parent_route is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
230
231
		if ($page_dir)
232 7
		{
233 3
			$is_sub_route = true;
234 3
			$parent_route = ltrim(dirname($page_dir) . '/index.php', './');
235 3
		}
236
		else
237
		{
238 4
			$routes = $this->get_routes_for_style($all_routes, $style_id);
239 4
			$parent_route = $this->get_virtual_parent($routes, $current_route, $is_sub_route);
240
		}
241
242 7
		return $parent_route;
243
	}
244
245
	/**
246
	 * @param array $routes
247
	 * @param string $current_route
248
	 * @param bool $is_sub_route
249
	 * @return string
250
	 */
251 4
	protected function get_virtual_parent(array $routes, $current_route, &$is_sub_route)
252
	{
253 4
		$routes[$current_route] = array();
254 4
		$routes = array_keys($routes);
255 4
		sort($routes);
256 4
		$index = (int) array_search($current_route, $routes);
257
258 4
		$parent_route = '';
259 4
		if (isset($routes[$index - 1]) && strpos($current_route, $routes[$index - 1]) !== false)
260 4
		{
261 1
			$is_sub_route = true;
262 1
			$parent_route = $routes[$index - 1];
263 1
		}
264
265 4
		return $parent_route;
266
	}
267
268
	/**
269
	 * @param array $all_routes
270
	 * @param int $style_id
271
	 * @return array
272
	 */
273 4
	protected function get_routes_for_style(array $all_routes, $style_id)
274
	{
275 4
		return (isset($all_routes[$style_id])) ? $all_routes[$style_id] : array();
276
	}
277
278
	/**
279
	 * @param array $route_info
280
	 * @param int $style_id
281
	 * @param bool $edit_mode
282
	 * @return int
283
	 */
284 19
	protected function get_display_route_id(array $route_info, $style_id, $edit_mode)
285
	{
286 19
		$route_id = $route_info['route_id'];
287 19
		if ($edit_mode === false && !$route_info['has_blocks'])
288 19
		{
289 6
			$default_route = $this->get_route_info($this->config['sitemaker_default_layout'], '', $style_id, $edit_mode);
290 6
			$route_id = $default_route['route_id'];
291 6
		}
292
293 19
		return (int) $route_id;
294
	}
295
296
	/**
297
	 * @param array $blocks
298
	 * @param bool $edit_mode
299
	 */
300 19
	protected function cache_block(array $blocks, $edit_mode)
301
	{
302 19
		if ($edit_mode === false)
303 19
		{
304 15
			$this->cache->put('sitemaker_blocks', $blocks);
305 15
		}
306 19
	}
307
}
308