Completed
Push — develop ( 7a7d04...968c4b )
by Daniel
20:22
created

navigation::get_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\menus;
12
13
class navigation
14
{
15
	/** @var \phpbb\cache\driver\driver_interface */
16
	protected $cache;
17
18
	/** @var \blitze\sitemaker\model\mapper_factory */
19
	protected $mapper_factory;
20
21
	/** @var \blitze\sitemaker\services\navbar */
22
	protected $navbar;
23
24
	/** @var \blitze\sitemaker\services\menus\display */
25
	protected $tree;
26
27
	/** @var string */
28
	protected $php_ext;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param \phpbb\cache\driver\driver_interface		$cache				Cache driver interface
34 19
	 * @param \blitze\sitemaker\model\mapper_factory	$mapper_factory		Mapper factory object
35
	 * @param \blitze\sitemaker\services\navbar			$navbar				Navbar object
36 19
	 * @param \blitze\sitemaker\services\menus\display	$tree				Menu tree display object
37 19
	 * @param string									$php_ext			php file extension
38 19
	 */
39 19
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \blitze\sitemaker\model\mapper_factory $mapper_factory, \blitze\sitemaker\services\navbar $navbar, \blitze\sitemaker\services\menus\display $tree, $php_ext)
40 19
	{
41
		$this->cache = $cache;
42
		$this->mapper_factory = $mapper_factory;
43
		$this->navbar = $navbar;
44
		$this->tree = $tree;
45
		$this->php_ext = $php_ext;
46
	}
47
48
	/**
49 17
	 * @param string $style
50
	 * @return array
51 17
	 */
52
	public function get_settings($style)
53 17
	{
54 17
		return $this->navbar->get_settings($style);
55 8
	}
56
57
	/**
58 9
	 * @param int $menu_id
59 9
	 * @param bool $is_navigation
60 1
	 * @param array $settings
61 1
	 * @return array
62
	 */
63
	public function build_menu($menu_id, $is_navigation = false, array $settings = array())
64 8
	{
65 8
		$data = $this->get_menu($menu_id);
66
67
		if (!sizeof($data))
68 9
		{
69
			return [];
70
		}
71
72
		if (!$is_navigation)
73
		{
74 17
			$list = $this->tree->display_list($data['items']);
75
		}
76 2
		else
77
		{
78 2
			$this->tree->set_params($settings);
79 2
			$list = $this->tree->display_navlist($data);
80 17
		}
81 2
82 2
		return array_merge($list, array(
83
			'is_nav' => $is_navigation
84 2
		));
85
	}
86
87
	/**
88
	 * @return string[]
89
	 */
90
	public function get_menu_options()
91 17
	{
92
		$collection = $this->mapper_factory->create('menus')->find();
93 17
94 17
		$options = array();
95 17
		foreach ($collection as $entity)
96
		{
97 17
			$options[$entity->get_menu_id()] = $entity->get_menu_name();
98 17
		}
99
100 17
		return $options;
101
	}
102
103
	/**
104
	 * @param int $menu_id
105
	 * @return array
106 17
	 */
107
	protected function get_menu($menu_id)
108 17
	{
109
		if (($data = $this->cache->get('sitemaker_menus')) === false)
110 17
		{
111
			$data = $this->get_all_menus();
112 17
113 17
			$this->cache->put('sitemaker_menus', $data);
114
		}
115 17
116 17
		return (isset($data[$menu_id])) ? $data[$menu_id] : array();
117 17
	}
118
119 17
	/**
120
	 * @return array
121 17
	 */
122 17
	protected function get_all_menus()
123 17
	{
124 17
		$item_mapper = $this->mapper_factory->create('items');
125 17
126
		$collection = $item_mapper->find();
127 17
128
		$data = array();
129
		foreach ($collection as $entity)
130
		{
131
			$row = $entity->to_array();
132
			$this->set_path_info($row);
133 17
			$this->pre_parse($row);
134
135 17
			$data[$row['menu_id']]['items'][$row['item_id']] = $row;
136
137 17
			if ($row['is_navigable'])
138 17
			{
139 17
				$data[$row['menu_id']]['paths'][$row['item_id']] = $this->get_matchable_url($row);
140 17
			}
141
		}
142
143
		return $data;
144
	}
145 17
146
	/**
147 17
	 * @param array $row
148 17
	 */
149 17
	protected function set_path_info(array &$row)
150 17
	{
151
		$url_info = parse_url($row['item_url']);
152
153
		$row['host'] = (isset($url_info['host'])) ? $url_info['host'] : '';
154
		$row['url_path'] = (isset($url_info['path'])) ? $url_info['path'] : '';
155
		$row['url_query'] = (isset($url_info['query'])) ? explode('&', $url_info['query']) : array();
156 17
	}
157
158 17
	/**
159
	 * @param array $row
160
	 */
161
	protected function pre_parse(array &$row)
162
	{
163
		$row['is_navigable'] = $this->is_navigable($row);
164
		$row['is_expandable'] = ($row['is_navigable'] && !$row['item_target']) ? true : false;
165 17
		$row['url_path'] = str_replace('/index.' . $this->php_ext, '/', $row['url_path']);
166
	}
167 17
168
	/**
169
	 * @param array $row
170
	 * @return bool
171
	 */
172
	protected function is_navigable(array $row)
173
	{
174 17
		return (!$this->is_local($row) || substr($row['item_url'], 0, 1) === '#' || $this->is_not_php_file($row['url_path'])) ? false : true;
175
	}
176 17
177 17
	/**
178
	 * @param array $row
179
	 * @return bool
180
	 */
181
	protected function is_local(array $row)
182
	{
183
		return ($row['item_url'] && !$row['host']) ? true : false;
184 17
	}
185
186 17
	/**
187
	 * @param string $url_path
188 17
	 * @return bool
189
	 */
190 17
	protected function is_not_php_file($url_path)
191
	{
192
		$extension = pathinfo($url_path, PATHINFO_EXTENSION);
193
		return ($extension && $extension !== $this->php_ext) ? true : false;
194
	}
195
196
	/**
197
	 * @param array
198
	 * @return string
199
	 */
200
	protected function get_matchable_url(array $row)
201
	{
202
		sort($row['url_query']);
203
204
		$row['url_path'] = ($row['url_path'] === '/') ? '/index.' . $this->php_ext : $row['url_path'];
205
206
		return $row['url_path'] . ((sizeof($row['url_query'])) ? '?' . join('&', $row['url_query']) : '');
207
	}
208
}
209