Passed
Push — renovate/configure ( 569ee5...22eea0 )
by
unknown
12:33
created

navigation::is_navigable()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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