Completed
Push — master ( 926e5e...2f974b )
by Daniel
21:21
created

items   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 130
ccs 60
cts 60
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A load() 0 13 2
A find() 0 15 2
A save() 0 15 2
A add_items() 0 16 2
A update_items() 0 6 1
A create_entity() 0 4 1
A prep_items_for_storage() 0 11 2
A get_sql_where() 0 4 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2015 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\model\menus\mapper;
11
12
use blitze\sitemaker\model\base_mapper;
13
use blitze\sitemaker\services\menus\nestedset;
14
15
class items extends base_mapper
16
{
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \blitze\sitemaker\services\menus\nestedset */
21
	protected $tree;
22
23
	/** @var string */
24
	protected $_entity_class = 'blitze\sitemaker\model\menus\entity\item';
25
26
	/** @var string */
27
	protected $_entity_pkey = 'item_id';
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @param \phpbb\db\driver\driver_interface				$db					Database object
33
	 * @param \blitze\sitemaker\model\base_collection		$collection			Entity collection
34
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory		Mapper factory object
35
	 * @param string										$entity_table		Menu Items table
36
	 * @param \phpbb\config\config							$config				Config object
37
	 */
38 42
	public function  __construct(\phpbb\db\driver\driver_interface $db, \blitze\sitemaker\model\base_collection $collection, \blitze\sitemaker\model\mapper_factory $mapper_factory, $entity_table, \phpbb\config\config $config)
39
	{
40 42
		parent::__construct($db, $collection, $mapper_factory, $entity_table);
41
42 42
		$this->config = $config;
43 42
		$this->tree = new nestedset(
44 42
			$db,
45 42
			new \phpbb\lock\db('sitemaker.table_lock.menu_items_table', $this->config, $db),
46 42
			$this->_entity_table
47 42
		);
48 42
	}
49
50 11
	public function load(array $condition = array())
51
	{
52 11
		$sql_where = join(' AND ', $this->_get_condition($condition));
53 11
		$row = $this->tree
54 11
			->set_sql_where($sql_where)
55 11
			->get_item_info();
56
57
		if ($row)
58 11
		{
59 8
			return $this->create_entity($row);
60
		}
61 3
		return null;
62
	}
63
64 26
	public function find(array $condition = array())
65
	{
66 26
		$sql_where = join(' AND ', $this->_get_condition($condition));
67 26
		$tree_data = $this->tree
68 26
			->set_sql_where($sql_where)
69 26
			->get_all_tree_data();
70
71 26
		$this->_collection->clear();
72 26
		foreach ($tree_data as $id => $row)
73
		{
74 22
			$this->_collection[$id] = $this->create_entity($row);
75 26
		}
76
77 26
		return $this->_collection;
78
	}
79
80 7
	public function save($entity)
81
	{
82 7
		$sql_data = $entity->to_db();
83
84 7
		$this->tree->set_sql_where($this->get_sql_where($entity->get_menu_id()));
85
86 7
		if ($entity->get_item_id())
87 7
		{
88 5
			return $this->tree->update_item($entity->get_item_id(), $sql_data);
89
		}
90
		else
91
		{
92 2
			return $this->tree->insert($sql_data);
93
		}
94
	}
95
96 7
	public function add_items($menu_id, $parent_id, $string)
97
	{
98 7
		$items = $this->tree->string_to_nestedset($string, array('item_title' => '', 'item_url' => ''), array('menu_id' => $menu_id));
99
100 6
		$new_item_ids = array();
101 6
		if (sizeof($items))
102 6
		{
103 6
			$branch = $this->prep_items_for_storage($items);
104
105 6
			$new_item_ids = $this->tree
106 6
				->set_sql_where($this->get_sql_where($menu_id))
107 6
				->add_branch($branch, $parent_id);
108 5
		}
109
110 5
		return $this->find(array('item_id' => $new_item_ids));
111
	}
112
113 3
	public function update_items($menu_id, array $items)
114
	{
115 3
		return $this->tree
116 3
			->set_sql_where($this->get_sql_where($menu_id))
117 3
			->update_tree($items);
118
	}
119
120
	/**
121
	 * Create the entity
122
	 */
123 32
	public function create_entity(array $row)
124
	{
125 32
		return new $this->_entity_class($row, $this->config['enable_mod_rewrite']);
126
	}
127
128 6
	protected function prep_items_for_storage($items)
129
	{
130 6
		$branch = array();
131 6
		foreach ($items as $key => $row)
132
		{
133 6
			$entity = $this->create_entity($row);
134 6
			$branch[$key] = $entity->to_db();
135 6
		}
136
137 6
		return $branch;
138
	}
139
140 16
	protected function get_sql_where($menu_id)
141
	{
142 16
		return '%smenu_id = ' . (int) $menu_id;
143
	}
144
}
145