Completed
Push — develop ( 86b038...fd19b8 )
by Daniel
09:51
created

navigation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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