save_tree::prepare_tree()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 20
ccs 13
cts 13
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
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\action;
11
12
class save_tree extends base_action
13
{
14
	/**
15
	 * {@inheritdoc}
16
	 * @throws \blitze\sitemaker\exception\out_of_bounds
17
	 */
18 3
	public function execute()
19
	{
20 3
		$menu_id = $this->request->variable('menu_id', 0);
21 3
		$raw_tree = $this->request->variable('tree', array(0 => array('' => 0)));
22
23
		/** @type \blitze\sitemaker\model\mapper\items $item_mapper */
24 3
		$item_mapper = $this->mapper_factory->create('items');
25 3
		$menu_mapper = $this->mapper_factory->create('menus');
26
27 3
		if ($menu_mapper->load(array('menu_id', '=', $menu_id)) === null)
28 3
		{
29 1
			throw new \blitze\sitemaker\exception\out_of_bounds('menu_id');
30
		}
31
32 2
		$tree = $this->prepare_tree($raw_tree);
33
34 2
		return $item_mapper->update_items($menu_id, $tree);
35
	}
36
37
	/**
38
	 * @param array $raw_tree
39
	 * @return array
40
	 */
41 2
	protected function prepare_tree(array $raw_tree)
42
	{
43 2
		$tree = array();
44 2
		$raw_tree = array_values($raw_tree);
45
46 2
		for ($i = 0, $size = sizeof($raw_tree); $i < $size; $i++)
47
		{
48 2
			$item_id = (int) $raw_tree[$i]['item_id'];
49 2
			$parent_id = (int) $raw_tree[$i]['parent_id'];
50
51
			if ($item_id)
52 2
			{
53 2
				$tree[$item_id] = array(
54 2
					'item_id'	=> $item_id,
55 2
					'parent_id' => $parent_id,
56
				);
57 2
			}
58 2
		}
59
60 2
		return $tree;
61
	}
62
}
63