AbstractTableDao::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Adi
5
 * Date: 2/24/2019
6
 * Time: 6:54 PM
7
 */
8
9
namespace Qpdb\QueryBuilder\Abstracts;
10
11
12
use Qpdb\PdoWrapper\PdoWrapperService;
13
use Qpdb\QueryBuilder\Dependencies\QueryException;
14
use Qpdb\QueryBuilder\QueryBuild;
15
16
abstract class AbstractTableDao
17
{
18
19
	/**
20
	 * @var string
21
	 */
22
	protected $table;
23
24
	/**
25
	 * @var array
26
	 */
27
	protected $primary;
28
29
	/**
30
	 * @var string
31
	 */
32
	protected $orderField;
33
34
	/**
35
	 * @var integer
36
	 */
37
	protected $insertId;
38
39
40
	/**
41
	 * AbstractTableCrud constructor.
42
	 */
43
	public function __construct() {
44
		$this->table = $this->getTableName();
45
		$this->primary = (array)$this->getPrimaryKey();
46
		$this->orderField = $this->getOrderField();
47
	}
48
49
50
	/**
51
	 * @return string
52
	 */
53
	abstract protected function getTableName();
54
55
	/**
56
	 * @return string|array
57
	 */
58
	abstract protected function getPrimaryKey();
59
60
	/**
61
	 * @return string
62
	 */
63
	abstract protected function getOrderField();
64
65
66
	/**
67
	 * @param       $id
68
	 * @param array $fields
69
	 * @return array|bool
70
	 * @throws QueryException
71
	 */
72
	public function getRowById( $id, array $fields = [] ) {
73
		$conditions = $this->getPrimaryKeyConditions( $id );
74
		$result = QueryBuild::select( $this->table )->fields( $fields );
75
		foreach ( $conditions as $field => $value )
76
			$result->whereEqual( $field, $value );
77
78
		return $result->first()->execute();
79
	}
80
81
	/**
82
	 * @param       $fieldName
83
	 * @param       $fieldValue
84
	 * @param array $fields
85
	 * @return array
86
	 * @throws QueryException
87
	 */
88
	public function getFirstRowByCondition( $fieldName, $fieldValue, $fields = [] ) {
89
		return QueryBuild::select( $this->table )
90
			->fields( $fields )
91
			->whereEqual( $fieldName, $fieldValue )
92
			->first()
93
			->execute();
94
	}
95
96
97
	/**
98
	 * @param $id
99
	 * @return array|int|null
100
	 * @throws QueryException
101
	 */
102
	public function deleteRowById( $id ) {
103
		$conditions = $this->getPrimaryKeyConditions( $id );
104
		$result = QueryBuild::delete( $this->table );
105
		foreach ( $conditions as $field => $value )
106
			$result->whereEqual( $field, $value );
107
108
		return $result->execute();
109
	}
110
111
	/**
112
	 * @param       $id
113
	 * @param array $arrayUpdater
114
	 * @return array|int|null
115
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
116
	 */
117
	public function updateRowById( $id, array $arrayUpdater ) {
118
		$conditions = $this->getPrimaryKeyConditions( $id );
119
		$result = QueryBuild::update( $this->table );
120
		foreach ( $conditions as $field => $value )
121
			$result->whereEqual( $field, $value );
122
		$result->setFieldsByArray( $arrayUpdater );
123
124
		return $result->execute();
125
	}
126
127
	/**
128
	 * @param       $fieldName
129
	 * @param       $fieldValue
130
	 * @param array $arrayUpdater
131
	 * @return array|int|null
132
	 * @throws QueryException
133
	 */
134
	public function updateRowsByFieldFilter( $fieldName, $fieldValue, array $arrayUpdater ) {
135
		return QueryBuild::update($this->table)
136
			->whereEqual($fieldName, $fieldValue)
137
			->setFieldsByArray($arrayUpdater)
138
			->execute();
139
	}
140
141
142
	/**
143
	 * @param array $arrayValues
144
	 * @return bool|mixed|\PDOStatement
145
	 * @throws QueryException
146
	 */
147
	public function insertRow( array $arrayValues ) {
148
		$result = QueryBuild::insert( $this->table )->setFieldsByArray( $arrayValues )->execute();
149
		$this->insertId = PdoWrapperService::getInstance()->lastInsertId();
0 ignored issues
show
Documentation Bug introduced by
The property $insertId was declared of type integer, but Qpdb\PdoWrapper\PdoWrapp...tance()->lastInsertId() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
150
151
		return $result;
152
	}
153
154
	/**
155
	 * @param bool $nullToZero
156
	 * @return null|mixed
157
	 * @throws QueryException
158
	 */
159
	public function getMaxOrder( $nullToZero = false ) {
160
		if ( empty( $this->orderField ) )
161
			throw new QueryException( 'Order field is not defined' );
162
163
		$result = QueryBuild::select( $this->table )
164
			->fieldExpression( "MAX(`{$this->orderField}`)", "max_order_column_alias" )
165
			->first()
166
			->column( 'max_order_column_alias' )
167
			->execute();
168
169
		if ( null === $result && $nullToZero ) {
0 ignored issues
show
introduced by
The condition null === $result is always false.
Loading history...
170
			return 0;
171
		}
172
173
		return $result;
174
	}
175
176
	/**
177
	 * @param array $updates_ord
178
	 * @return bool|\PDOStatement
179
	 * @throws QueryException
180
	 */
181
	public function saveOrder( $updates_ord = array() ) {
182
		if ( empty( $this->orderField ) )
183
			throw new QueryException( 'Order field is not defined' );
184
185
		$query = /** @lang text */
186
			"UPDATE `{$this->table}` SET `{$this->orderField}` = CASE `{$this->primary[0]}` \r\n";
187
		foreach ( $updates_ord as $position => $id ) {
188
			$pos = $position + 1;
189
			$query .= " WHEN '$id' THEN '$pos' \r\n";
190
		}
191
		$query .= "ELSE `{$this->orderField}` END";
192
193
		return PdoWrapperService::getInstance()->query( $query, [] );
194
	}
195
196
197
	/**
198
	 * @param mixed
199
	 * @return array
200
	 * @throws QueryException
201
	 */
202
	protected function getPrimaryKeyConditions( $id ) {
203
204
		$id = (array)$id;
205
206
		if ( count( $this->primary ) !== count( $id ) )
207
			throw new QueryException( 'Invalid primary key', QueryException::QUERY_CRUD_INVALID_PRIMARY );
208
209
		$conditions = [];
210
211
		foreach ( $this->primary as $index => $key )
212
			$conditions[ $key ] = $id[ $index ];
213
214
		return $conditions;
215
	}
216
217
	/**
218
	 * @return int
219
	 */
220
	public function getInsertId() {
221
		return $this->insertId;
222
	}
223
224
225
}