Completed
Push — develop ( de7659...415144 )
by Daniel
09:44
created

base_mapper   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.13%

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 29
c 7
b 1
f 1
lcom 1
cbo 3
dl 0
loc 254
ccs 82
cts 89
cp 0.9213
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A insert() 0 14 2
A update() 0 13 2
A find_sql() 0 5 2
A get_sql_condition() 0 18 3
A ensure_multi_array() 0 4 2
A get_sql_where_array() 0 4 2
A get_sql_where_string() 0 4 1
A get_sql_where_integer() 0 4 1
A get_sql_where_boolean() 0 4 1
A __construct() 0 7 1
A get_collection() 0 4 1
A load() 0 13 2
A find() 0 14 2
A save() 0 14 2
A delete() 0 18 4
A create_entity() 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;
11
12
abstract class base_mapper implements mapper_interface
13
{
14
	/** @var \phpbb\db\driver\driver_interface */
15
	protected $db;
16
17
	/** @var \blitze\sitemaker\model\base_collection */
18
	protected $collection;
19
20
	/** @var \blitze\sitemaker\model\mapper_factory */
21
	protected $mapper_factory;
22
23
	/** @var string */
24
	protected $entity_table;
25
26
	/** @var string */
27
	protected $entity_class;
28
29
	/** @var string */
30
	protected $entity_pkey;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param \phpbb\db\driver\driver_interface				$db					Database object
36
	 * @param \blitze\sitemaker\model\base_collection		$collection			Entity collection
37
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory		Mapper factory object
38
	 * @param string										$entity_table
39
	 */
40 118
	public function  __construct(\phpbb\db\driver\driver_interface $db, \blitze\sitemaker\model\base_collection $collection, \blitze\sitemaker\model\mapper_factory $mapper_factory, $entity_table)
41
	{
42 118
		$this->db = $db;
43 118
		$this->collection = $collection;
44 118
		$this->mapper_factory = $mapper_factory;
45 118
		$this->entity_table = $entity_table;
46 118
	}
47
48
	/**
49
	 * Get the collection
50
	 */
51
	public function get_collection()
52
	{
53
		return $this->collection;
54
	}
55
56
	/**
57
	 * {@inheritdoc}
58
	 */
59 56
	public function load(array $condition = array())
60
	{
61 56
		$sql_where = $this->get_sql_condition($condition);
62 56
		$results = $this->db->sql_query($this->find_sql($sql_where));
63 56
		$row = $this->db->sql_fetchrow($results);
64 56
		$this->db->sql_freeresult($results);
65
66
		if ($row)
67 56
		{
68 45
			return $this->create_entity($row);
69
		}
70 20
		return null;
71
	}
72
73
	/**
74
	 * {@inheritdoc}
75
	 */
76 43
	public function find(array $condition = array())
77
	{
78 43
		$sql_where = $this->get_sql_condition($condition);
79 43
		$results = $this->db->sql_query($this->find_sql($sql_where));
80 43
		$this->collection->clear();
81
82 43
		while ($row = $this->db->sql_fetchrow($results))
83
		{
84 40
			$this->collection[$row[$this->entity_pkey]] = $this->create_entity($row);
85 40
		}
86 43
		$this->db->sql_freeresult($results);
87
88 43
		return $this->collection;
89
	}
90
91
	/**
92
	 * {@inheritdoc}
93
	 */
94 27
	public function save(\blitze\sitemaker\model\entity_interface $entity)
95
	{
96 27
		$accessor = 'get_' . $this->entity_pkey;
97 27
		if (is_null($entity->$accessor()))
98 27
		{
99 12
			$entity = $this->insert($entity);
100 12
		}
101
		else
102
		{
103 16
			$this->update($entity);
104
		}
105
106 26
		return $entity;
107
	}
108
109
	/**
110
	 * {@inheritdoc}
111
	 */
112 15
	public function delete($condition)
113
	{
114 15
		if (!is_array($condition))
115 15
		{
116 10
			if ($condition instanceof $this->entity_class)
117 10
			{
118 10
				$accessor = 'get_' . $this->entity_pkey;
119 10
				$condition = array($this->entity_pkey, '=', $condition->$accessor());
120 10
			}
121
			else
122
			{
123
				throw new \blitze\sitemaker\exception\unexpected_value('INVALID_ENTITY');
124
			}
125 10
		}
126
127 15
		$sql_where = $this->get_sql_condition($condition);
128 15
		$this->db->sql_query('DELETE FROM ' . $this->entity_table . (sizeof($sql_where) ? ' WHERE ' . join(' AND ', $sql_where) : ''));
129 15
	}
130
131
	/**
132
	 * {@inheritdoc}
133
	 */
134 72
	public function create_entity(array $row)
135
	{
136 72
		return new $this->entity_class($row);
137
	}
138
139
	/**
140
	 * Insert a new row in the table corresponding to the specified entity
141
	 * @param \blitze\sitemaker\model\entity_interface $entity
142
	 * @return \blitze\sitemaker\model\entity_interface
143
	 * @throws \blitze\sitemaker\exception\unexpected_value
144
	 */
145 12
	protected function insert(\blitze\sitemaker\model\entity_interface $entity)
146
	{
147 12
		if ($entity instanceof $this->entity_class)
148 12
		{
149 12
			$this->db->sql_query('INSERT INTO ' . $this->entity_table . ' ' . $this->db->sql_build_array('INSERT', $entity->to_db()));
150
151 12
			$mutator = 'set_' . $this->entity_pkey;
152 12
			$entity->$mutator((int) $this->db->sql_nextid());
153
154 12
			return $entity;
155
		}
156
157
		throw new \blitze\sitemaker\exception\unexpected_value('INVALID_ENTITY');
158
	}
159
160
	/**
161
	 * Update the row in the table corresponding to the specified entity
162
	 * @param \blitze\sitemaker\model\entity_interface $entity
163
	 * @return mixed
164
	 * @throws \blitze\sitemaker\exception\unexpected_value
165
	 */
166 16
	protected function update(\blitze\sitemaker\model\entity_interface $entity)
167
	{
168 16
		if ($entity instanceof $this->entity_class)
169 16
		{
170 16
			$accessor = 'get_' . $this->entity_pkey;
171
172 16
			return $this->db->sql_query('UPDATE ' . $this->entity_table . '
173 16
				SET ' . $this->db->sql_build_array('UPDATE', $entity->to_db()) . '
174 15
				WHERE ' . $this->entity_pkey . ' = ' . (int) $entity->$accessor());
175
		}
176
177
		throw new \blitze\sitemaker\exception\unexpected_value('INVALID_ENTITY');
178
	}
179
180
	/**
181
	 * @param array $sql_where
182
	 * @return string
183
	 */
184 28
	protected function find_sql(array $sql_where)
185
	{
186 28
		return 'SELECT * FROM ' . $this->entity_table .
187 28
			(sizeof($sql_where) ? ' WHERE ' . join(' AND ', $sql_where) : '');
188
	}
189
190
	/**
191
	 * @param array $condition
192
	 * @return array
193
	 */
194 108
	protected function get_sql_condition(array $condition)
195
	{
196 108
		$sql_where = array();
197 108
		$condition = $this->ensure_multi_array($condition);
198
199 108
		foreach ($condition as $info)
200
		{
201 79
			list($field, $operator, $value) = $info;
202
203 79
			$callable = 'get_sql_where_' . gettype($value);
204 79
			if (is_callable(array($this, $callable)))
205 79
			{
206 79
				$sql_where[] = call_user_func_array(array($this, $callable), array($field, $value, $operator));
207 79
			}
208 108
		}
209
210 108
		return $sql_where;
211
	}
212
213
	/**
214
	 * @param array $condition
215
	 * @return mixed
216
	 */
217 108
	protected function ensure_multi_array(array $condition)
218
	{
219 108
		return array_filter((is_array(current($condition))) ? $condition : array($condition));
220
	}
221
222
	/**
223
	 * @param string $field
224
	 * @param array $value
225
	 * @param string $operator
226
	 * @return string
227
	 */
228 9
	protected function get_sql_where_array($field, array $value, $operator)
229
	{
230 9
		return $this->db->sql_in_set($field, $value, ($operator == '=') ? false : true);
231
	}
232
233
	/**
234
	 * @param string $field
235
	 * @param string $value
236
	 * @param string $operator
237
	 * @return string
238
	 */
239 24
	protected function get_sql_where_string($field, $value, $operator)
240
	{
241 24
		return $field . " $operator '" . $this->db->sql_escape($value) . "'";
242
	}
243
244
	/**
245
	 * @param string $field
246
	 * @param string $value
247
	 * @param string $operator
248
	 * @return string
249
	 */
250 78
	protected function get_sql_where_integer($field, $value, $operator)
251
	{
252 78
		return $field . " $operator " . (int) $value;
253
	}
254
255
	/**
256
	 * @param string $field
257
	 * @param bool $value
258
	 * @param string $operator
259
	 * @return string
260
	 */
261
	protected function get_sql_where_boolean($field, $value, $operator)
262
	{
263
		return $this->get_sql_where_integer($field, (int) $value, $operator);
264
	}
265
}
266