Completed
Push — master ( fc784d...7c0139 )
by Daniel
09:49
created

base_mapper   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 98.8%

Importance

Changes 0
Metric Value
wmc 26
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 239
ccs 82
cts 83
cp 0.988
rs 10

15 Methods

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