Passed
Push — develop ( d98504...d3493c )
by Daniel
04:11
created

routes::get_virtual_parent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 28
ccs 13
cts 13
cp 1
crap 3
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\services\blocks;
12
13
class routes extends parent_route
14
{
15
	/** @var \phpbb\cache\driver\driver_interface */
16
	protected $cache;
17
18
	/** @var \blitze\sitemaker\services\blocks\factory */
19
	protected $block_factory;
20
21
	/** @var \blitze\sitemaker\model\mapper_factory */
22
	protected $mapper_factory;
23
24
	/** @var string */
25
	protected $php_ext;
26
27
	/** @var bool */
28
	protected $is_sub_route = false;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param \phpbb\cache\driver\driver_interface			$cache					Cache driver interface
34
	 * @param \phpbb\config\config							$config					Config object
35
	 * @param \blitze\sitemaker\services\blocks\factory		$block_factory			Blocks factory object
36
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory			Mapper factory object
37
	 * @param string										$php_ext				phpEx
38
	 */
39
	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)
40
	{
41 42
		parent::__construct($cache, $config, $mapper_factory, $php_ext);
42
43 42
		$this->cache = $cache;
44 42
		$this->block_factory = $block_factory;
45 42
		$this->mapper_factory = $mapper_factory;
46 42
		$this->php_ext = $php_ext;
47 42
	}
48 42
49
	/**
50
	 * @param string $current_route
51
	 * @param string $page_dir
52
	 * @param int $forum_id
53
	 * @param int $style_id
54
	 * @param bool $edit_mode
55
	 * @return array
56
	 */
57 25
	public function get_route_info($current_route, $page_dir, $forum_id, $style_id, $edit_mode = false)
58
	{
59 25
		$routes = $this->get_routes_for_style($style_id);
60 25
		$current_route = str_replace('viewtopic.' . $this->php_ext, 'viewforum.' . $this->php_ext, $current_route);
61
		$route_info = array();
62
63 25
		// does route have own settings?
64 25
		if (isset($routes[$current_route]))
65 17
		{
66 17
			$route_info = $routes[$current_route];
67
		}
68
69 25
		if ($edit_mode)
70 9
		{
71 9
			$route_info += $this->get_default_route_info($current_route, $style_id);
72
			return $route_info;
73
		}
74 16
75
		return $this->inherit_route_info($routes, $route_info, $current_route, $page_dir, $forum_id, $style_id);
76
	}
77
78
	/**
79
	 * @param array $route_info
80
	 * @param bool $edit_mode
81
	 * @return array
82
	 */
83 23
	public function get_blocks_for_route(array $route_info, $edit_mode)
84
	{
85 23
		$blocks = $this->get_cached_blocks($edit_mode);
86 23
		$route_id = $route_info['route_id'];
87
		$style_id = $route_info['style'];
88 23
89
		return (isset($blocks[$style_id][$route_id])) ? $blocks[$style_id][$route_id] : array();
90
	}
91
92
	/**
93
	 * @param array $df_settings
94
	 * @param array $db_settings
95
	 * @return array
96 38
	 */
97
	public function sync_settings(array $df_settings, array $db_settings = array())
98 38
	{
99 38
		$settings = array();
100
		foreach ($df_settings as $field => $vars)
101 34
		{
102 34
			if (!is_array($vars))
103 34
			{
104
				continue;
105 34
			}
106 38
			$settings[$field] = $vars['default'];
107
		}
108 38
109
		return array_merge($settings, array_intersect_key($db_settings, $settings));
110
	}
111
112
	/**
113
	 * Clear blocks cache
114 1
	 */
115
	public function clear_cache()
116 1
	{
117 1
		$this->cache->destroy('sitemaker_blocks');
118 1
		$this->cache->destroy('sitemaker_block_routes');
119
	}
120
121
	/**
122
	 * @param array $condition
123
	 * @return array
124
	 */
125
	protected function get_all_blocks(array $condition)
126
	{
127
		$collection = $this->mapper_factory->create('blocks')->find($condition);
128 16
129
		$blocks = array();
130
		foreach ($collection as $entity)
131
		{
132 16
			if (($block_instance = $this->block_factory->get_block($entity->get_name())) !== null)
133 16
			{
134 10
				$default_settings = $block_instance->get_config(array());
135 10
				$settings = $this->sync_settings($default_settings, $entity->get_settings());
136 10
				$entity->set_settings($settings);
137
138
				$blocks[$entity->get_style()][$entity->get_route_id()][$entity->get_position()][] = $entity;
139 16
			}
140 16
		}
141
142 16
		return $blocks;
143
	}
144
145
	/**
146
	 * @param int $style_id
147
	 * @return array
148
	 */
149
	protected function get_routes_for_style($style_id)
150 25
	{
151
		$all_routes = $this->get_all_routes();
152
		return (isset($all_routes[$style_id])) ? $all_routes[$style_id] : array();
153 25
	}
154 25
155 25
	/**
156 25
	 * @param bool $edit_mode
157 25
	 * @return array
158 25
	 */
159 25
	protected function get_cached_blocks($edit_mode)
160 25
	{
161
		if (($blocks = $this->cache->get('sitemaker_blocks')) === false || $edit_mode)
162
		{
163
			$condition = (!$edit_mode) ? array('status', '=', 1) : array();
164
			$blocks = $this->get_all_blocks($condition);
165
			$this->cache_block($blocks, $edit_mode);
166
		}
167
168
		return $blocks;
169 10
	}
170
171
	/**
172 10
	 * @param array $blocks
173 4
	 * @param bool $edit_mode
174 4
	 */
175 4
	protected function cache_block(array $blocks, $edit_mode)
176 6
	{
177 6
		if ($edit_mode === false)
178 1
		{
179 1
			$this->cache->put('sitemaker_blocks', $blocks);
180 1
		}
181
	}
182
}
183