Completed
Push — develop ( 18ce4b...3c36de )
by Daniel
11:04
created

menu_block::set_path_info()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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