Completed
Push — master ( 422577...df7035 )
by Daniel
05:56
created

base_mapper::get_condition()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
ccs 19
cts 19
cp 1
rs 6.7272
cc 7
eloc 17
nc 6
nop 1
crap 7
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 117
	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 117
		$this->db = $db;
43 117
		$this->collection = $collection;
44 117
		$this->mapper_factory = $mapper_factory;
45 117
		$this->entity_table = $entity_table;
46 117
	}
47
48
	/**
49
	 * Get the collection
50
	 */
51
	public function get_collection()
52
	{
53
		return $this->collection;
54
	}
55
56
	/**
57
	* {@inheritdoc}
58
	*/
59 55
	public function load(array $condition = array())
60
	{
61 55
		$sql_where = $this->get_sql_condition($condition);
62 55
		$results = $this->db->sql_query($this->find_sql($sql_where));
63 55
		$row = $this->db->sql_fetchrow($results);
64 55
		$this->db->sql_freeresult($results);
65
66
		if ($row)
67 55
		{
68 44
			return $this->create_entity($row);
69
		}
70 19
		return null;
71
	}
72
73
	/**
74
	* {@inheritdoc}
75
	*/
76 42
	public function find(array $condition = array())
77
	{
78 42
		$sql_where = $this->get_sql_condition($condition);
79 42
		$results = $this->db->sql_query($this->find_sql($sql_where));
80 42
		$this->collection->clear();
81
82 42
		while ($row = $this->db->sql_fetchrow($results))
83
		{
84 39
			$this->collection[$row[$this->entity_pkey]] = $this->create_entity($row);
85 39
		}
86 42
		$this->db->sql_freeresult($results);
87
88 42
		return $this->collection;
89
	}
90
91
	/**
92
	* {@inheritdoc}
93
	*/
94 26
	public function save(\blitze\sitemaker\model\entity_interface $entity)
95
	{
96 26
		$accessor = 'get_' . $this->entity_pkey;
97 26
		if (is_null($entity->$accessor()))
98 26
		{
99 11
			$entity = $this->insert($entity);
100 11
		}
101
		else
102
		{
103 16
			$this->update($entity);
104
		}
105
106 25
		return $entity;
107
	}
108
109
	/**
110
	* {@inheritdoc}
111
	*/
112 15
	public function delete($condition)
113
	{
114 15
		if ($condition instanceof $this->entity_class) {
115 15
			$accessor = 'get_' . $this->entity_pkey;
116 10
			$criteria = array($this->entity_pkey, '=', $condition->$accessor());
117 10
		}
118 10
		else
119
		{
120 15
			$criteria = $condition;
121 15
		}
122 15
		$sql_where = $this->get_sql_condition($criteria);
0 ignored issues
show
Bug introduced by
It seems like $criteria defined by $condition on line 120 can also be of type object<blitze\sitemaker\model\entity_interface>; however, blitze\sitemaker\model\b...er::get_sql_condition() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
123
		$this->db->sql_query('DELETE FROM ' . $this->entity_table . (sizeof($sql_where) ? ' WHERE ' . join(' AND ', $sql_where) : ''));
124
	}
125
126
	/**
127 71
	 * {@inheritdoc}
128
	 */
129 71
	public function create_entity(array $row)
130
	{
131
		return new $this->entity_class($row);
132
	}
133
134
	/**
135
	 * Insert a new row in the table corresponding to the specified entity
136
	 * @param \blitze\sitemaker\model\entity_interface $entity
137
	 * @return \blitze\sitemaker\model\entity_interface
138 11
	 * @throws \blitze\sitemaker\exception\unexpected_value
139
	 */
140 11
	protected function insert(\blitze\sitemaker\model\entity_interface $entity)
141 11
	{
142 11
		if ($entity instanceof $this->entity_class)
143
		{
144 11
			$this->db->sql_query('INSERT INTO ' . $this->entity_table . ' ' . $this->db->sql_build_array('INSERT', $entity->to_db()));
145 11
146
			$mutator = 'set_' . $this->entity_pkey;
147 11
			$entity->$mutator((int) $this->db->sql_nextid());
148
149
			return $entity;
150
		}
151
152
		throw new \blitze\sitemaker\exception\unexpected_value('INVALID_ENTITY');
153
	}
154
155
	/**
156
	 * Update the row in the table corresponding to the specified entity
157
	 * @param \blitze\sitemaker\model\entity_interface $entity
158
	 * @return mixed
159 16
	 * @throws \blitze\sitemaker\exception\unexpected_value
160
	 */
161 16
	protected function update(\blitze\sitemaker\model\entity_interface $entity)
162 16
	{
163 16
		if ($entity instanceof $this->entity_class)
164
		{
165 16
			$accessor = 'get_' . $this->entity_pkey;
166 16
167 15
			return $this->db->sql_query('UPDATE ' . $this->entity_table . '
168
				SET ' . $this->db->sql_build_array('UPDATE', $entity->to_db()) . '
169
				WHERE ' . $this->entity_pkey . ' = ' . (int) $entity->$accessor());
170
		}
171
172
		throw new \blitze\sitemaker\exception\unexpected_value('INVALID_ENTITY');
173
	}
174
175
	/**
176
	 * @param array $sql_where
177 28
	 * @return string
178
	 */
179 28
	protected function find_sql(array $sql_where)
180 28
	{
181
		return 'SELECT * FROM ' . $this->entity_table .
182
			(sizeof($sql_where) ? ' WHERE ' . join(' AND ', $sql_where) : '');
183
	}
184
185
	/**
186
	 * @param array $condition
187 107
	 * @return array
188
	 */
189 107
	protected function get_sql_condition(array $condition)
190 107
	{
191
		$sql_where = array();
192 107
		$condition = $this->ensure_multi_array($condition);
193
194 78
		foreach ($condition as $info)
195
		{
196 78
			list($field, $operator, $value) = $info;
197
198 78
			switch (gettype($value))
199 9
			{
200 9
				case 'array':
201 78
					$sql_where[] = $this->db->sql_in_set($field, $value, ($operator == '=') ? false : true);
202 23
				break;
203 23
				case 'string':
204 77
					$sql_where[] = $field . " $operator '" . $this->db->sql_escape($value) . "'";
205 77
				break;
206 77
				case 'boolean':
207 77
				case 'integer':
208 78
					$sql_where[] = $field . " $operator " . (int) $value;
209 107
				break;
210
			}
211 107
		}
212
213
		return $sql_where;
214
	}
215
216
	/**
217
	 * @param array $condition
218 107
	 * @return mixed
219
	 */
220 107
	protected function ensure_multi_array(array $condition)
221
	{
222
		return array_filter((is_array(current($condition))) ? $condition : array($condition));
223
	}
224
}
225