Completed
Push — master ( 60a459...efce96 )
by Daniel
08:46
created

menu_block::pre_parse()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 4
nop 1
crap 3
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\user */
23
	protected $user;
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\user								$user				User 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 16
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\user $user, \blitze\sitemaker\model\mapper_factory $mapper_factory, \blitze\sitemaker\services\menus\display $tree, $php_ext)
45
	{
46 16
		$this->cache = $cache;
47 16
		$this->config = $config;
48 16
		$this->user = $user;
49 16
		$this->mapper_factory = $mapper_factory;
50 16
		$this->tree = $tree;
51 16
		$this->php_ext = $php_ext;
52 16
	}
53
54
	/**
55
	 * @param int $menu_id
56
	 * @return array
57
	 */
58 14
	protected function get_menu($menu_id)
59
	{
60 14
		if (($data = $this->cache->get('sitemaker_menus')) === false)
61 14
		{
62 14
			$data = $this->get_all_menus();
63
64 14
			$this->cache->put('sitemaker_menus', $data);
65 14
		}
66
67 14
		return (isset($data[$menu_id])) ? $data[$menu_id] : array();
68
	}
69
70
	/**
71
	 * @return array
72
	 */
73 14
	protected function get_all_menus()
74
	{
75 14
		$item_mapper = $this->mapper_factory->create('menus', 'items');
76
77 14
		$collection = $item_mapper->find();
78
79 14
		$data = array();
80 14
		foreach ($collection as $entity)
81
		{
82 14
			$row = $entity->to_array();
83 14
			$this->set_path_info($row);
84 14
			$this->pre_parse($row);
85
86 14
			$data[$row['menu_id']][$row['item_id']] = $row;
87 14
		}
88
89 14
		return $data;
90
	}
91
92
	/**
93
	 * @param int $menu_id
94
	 * @param bool $editing
95
	 * @return string
96
	 */
97 8
	protected function get_message($menu_id, $editing)
98
	{
99 8
		$msg_key = '';
100
		if ($editing)
101 8
		{
102 4
			$msg_key = ($menu_id) ? 'MENU_NO_ITEMS' : 'SELECT_MENU';
103 4
		}
104
105 8
		return $this->user->lang($msg_key);
106
	}
107
108
	/**
109
	 * @param array $row
110
	 */
111 14
	protected function set_path_info(array &$row)
112
	{
113 14
		$url_info = parse_url($row['item_url']);
114
115 14
		$row['host'] = (isset($url_info['host'])) ? $url_info['host'] : '';
116 14
		$row['url_path'] = (isset($url_info['path'])) ? $url_info['path'] : '';
117 14
		$row['url_query'] = (isset($url_info['query'])) ? explode('&', $url_info['query']) : array();
118 14
	}
119
120
	/**
121
	 * @param array $row
122
	 */
123 14
	protected function pre_parse(array &$row)
124
	{
125 14
		$row['is_navigable'] = $this->is_navigable($row);
126 14
		$row['is_expandable'] = ($row['is_navigable'] && !$row['item_target']) ? true : false;
127 14
	}
128
129
	/**
130
	 * @param array $row
131
	 * @return bool
132
	 */
133 14
	 protected function is_navigable(array $row)
134
	 {
135 14
	 	return (!$row['host'] && (!file_exists('.' . $row['item_url']) || pathinfo($row['item_url'], PATHINFO_EXTENSION ) === $this->php_ext)) ? true : false;
136
	 }
137
138
	/**
139
	 * @return array
140
	 */
141 2
	protected function get_menu_options()
142
	{
143 2
		$collection = $this->mapper_factory->create('menus', 'menus')->find();
144
145 2
		$options = array();
146 2
		foreach ($collection as $entity)
147
		{
148 2
			$options[$entity->get_menu_id()] = $entity->get_menu_name();
149 2
		}
150
151 2
		return $options;
152
	}
153
}
154