Completed
Push — master ( 2f974b...edb932 )
by Daniel
08:50
created

menu   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 14
Bugs 4 Features 1
Metric Value
wmc 19
c 14
b 4
f 1
lcom 1
cbo 3
dl 0
loc 173
ccs 71
cts 71
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A get_config() 0 12 1
B display() 0 24 2
A _get_menu() 0 11 3
A _get_all_menus() 0 17 2
A _get_message() 0 10 3
A _set_path_info() 0 7 3
A get_menu_options() 0 12 2
A get_depth_options() 0 10 2
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\blocks;
11
12
/**
13
* Menu Block
14
* @package phpBB Sitemaker Menu
15
*/
16
class menu extends \blitze\sitemaker\services\blocks\driver\block
17
{
18
	/** @var \phpbb\cache\driver\driver_interface */
19
	protected $cache;
20
21
	/** @var \phpbb\config\config */
22
	protected $config;
23
24
	/** @var \phpbb\user */
25
	protected $user;
26
27
	/** @var \blitze\sitemaker\model\mapper_factory */
28
	protected $mapper_factory;
29
30
	/** @var \blitze\sitemaker\services\menus\display */
31
	protected $tree;
32
33
	/**
34
	 * Constructor
35
	 *
36
	 * @param \phpbb\cache\driver\driver_interface		$cache				Cache driver interface
37
	 * @param \phpbb\config\config						$config				Config object
38
	 * @param \phpbb\user								$user				User object
39
	 * @param \blitze\sitemaker\model\mapper_factory	$mapper_factory		Mapper factory object
40
	 * @param \blitze\sitemaker\services\menus\display	$tree				Menu tree display object
41
	 */
42 10
	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)
43
	{
44 10
		$this->cache = $cache;
45 10
		$this->config = $config;
46 10
		$this->user = $user;
47 10
		$this->mapper_factory = $mapper_factory;
48 10
		$this->tree = $tree;
49 10
	}
50
51
	/**
52
	 * {@inheritdoc}
53
	 */
54 1
	public function get_config(array $settings)
55
	{
56 1
		$menu_options = $this->get_menu_options();
57 1
		$depth_options = $this->get_depth_options();
58
59
		return array(
60 1
			'legend1'       => $this->user->lang('SETTINGS'),
61 1
			'menu_id'		=> array('lang' => 'MENU', 'validate' => 'int', 'type' => 'select', 'options' => $menu_options, 'default' => 0, 'explain' => false),
62 1
			'expanded'		=> array('lang' => 'EXPANDED', 'validate' => 'bool', 'type' => 'checkbox', 'options' => array(1 => ''), 'default' => 0, 'explain' => false),
63 1
			'max_depth'		=> array('lang' => 'MAX_DEPTH', 'validate' => 'int', 'type' => 'select', 'options' => $depth_options, 'default' => 3, 'explain' => false),
64 1
		);
65
	}
66
67
	/**
68
	 * {@inheritdoc}
69
	 */
70 9
	public function display(array $db_data, $editing = false)
71
	{
72 9
		$title = $this->user->lang('MENU');
73 9
		$menu_id = $db_data['settings']['menu_id'];
74
75 9
		$data = $this->_get_menu($menu_id);
76
77 9
		if (!sizeof($data))
78 9
		{
79
			return array(
80 4
				'title'		=> $title,
81 4
				'content'	=> $this->_get_message($menu_id, $editing),
82 4
			);
83
		}
84
85 5
		$this->tree->set_params($db_data['settings']);
86 6
		$this->tree->display_list($data, $this->ptemplate, 'tree');
87 5
		$this->tree->generate_breadcrumb($data);
88
89
		return array(
90 5
			'title'     => $title,
91 5
			'content'   => $this->ptemplate->render_view('blitze/sitemaker', 'blocks/menu.html', 'menu_block'),
92 5
		);
93
	}
94
95
	/**
96
	 * @param int $menu_id
97
	 * @return array
98
	 */
99 9
	private function _get_menu($menu_id)
100
	{
101 9
		if (($data = $this->cache->get('sitemaker_menus')) === false)
102 9
		{
103 9
			$data = $this->_get_all_menus();
104
105 9
			$this->cache->put('sitemaker_menus', $data);
106 9
		}
107
108 9
		return (isset($data[$menu_id])) ? $data[$menu_id] : array();
109
	}
110
111
	/**
112
	 * @return array
113
	 */
114 9
	private function _get_all_menus()
115
	{
116 9
		$item_mapper = $this->mapper_factory->create('menus', 'items');
117
118 9
		$collection = $item_mapper->find();
119
120 9
		$data = array();
121 9
		foreach ($collection as $entity)
122
		{
123 9
			$row = $entity->to_array();
124 9
			$this->_set_path_info($row);
125
126 9
			$data[$row['menu_id']][$row['item_id']] = $row;
127 9
		}
128
129 9
		return $data;
130
	}
131
132
	/**
133
	 * @param int $menu_id
134
	 * @param bool $editing
135
	 * @return string
136
	 */
137 4
	private function _get_message($menu_id, $editing)
138
	{
139 4
		$msg_key = '';
140
		if ($editing)
141 4
		{
142 2
			$msg_key = ($menu_id) ? 'MENU_NO_ITEMS' : 'SELECT_MENU';
143 2
		}
144
145 4
		return $this->user->lang($msg_key);
146
	}
147
148
	/**
149
	 * @param array $data
150
	 */
151 9
	private function _set_path_info(array &$data)
152
	{
153 9
		$url_info = parse_url($data['item_url']);
154
155 9
		$data['url_path']	= (isset($url_info['path'])) ? $url_info['path'] : '';
156 9
		$data['url_query']	= (isset($url_info['query'])) ? explode('&', $url_info['query']) : array();
157 9
	}
158
159
	/**
160
	 * @return array
161
	 */
162 1
	protected function get_menu_options()
163
	{
164 1
		$collection = $this->mapper_factory->create('menus', 'menus')->find();
165
166 1
		$options = array();
167 1
		foreach ($collection as $entity)
168
		{
169 1
			$options[$entity->get_menu_id()] = $entity->get_menu_name();
170 1
		}
171
172 1
		return $options;
173
	}
174
175
	/**
176
	 * @return array
177
	 */
178 1
	protected function get_depth_options()
179
	{
180 1
		$options = array();
181 1
		for ($i = 3; $i < 10; $i++)
182
		{
183 1
			$options[$i] = $i;
184 1
		}
185
186 1
		return $options;
187
	}
188
}
189