Passed
Push — master ( 4b81e6...2edb7f )
by Adrian
02:27
created

AbstractTableDao::getPrimaryKeyConditions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
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
	/**
129
	 * @param array $arrayValues
130
	 * @return bool|mixed|\PDOStatement
131
	 * @throws QueryException
132
	 */
133
	public function insertRow( array $arrayValues ) {
134
		$result = QueryBuild::insert( $this->table )->setFieldsByArray( $arrayValues )->execute();
135
		$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...
136
		return $result;
137
	}
138
139
	/**
140
	 * @param bool $nullToZero
141
	 * @return null|mixed
142
	 * @throws QueryException
143
	 */
144
	public function getMaxOrder( $nullToZero = false ) {
145
		if ( empty( $this->orderField ) )
146
			throw new QueryException( 'Order field is not defined' );
147
148
		$result = QueryBuild::select( $this->table )
149
			->fieldExpression( "MAX(`{$this->orderField}`)", "max_order_column_alias" )
150
			->first()
151
			->column( 'max_order_column_alias' )
152
			->execute();
153
154
		if ( null === $result && $nullToZero ) {
0 ignored issues
show
introduced by
The condition null === $result is always false.
Loading history...
155
			return 0;
156
		}
157
158
		return $result;
159
	}
160
161
	/**
162
	 * @param array $updates_ord
163
	 * @return bool|\PDOStatement
164
	 * @throws QueryException
165
	 */
166
	public function saveOrder( $updates_ord = array() ) {
167
		if ( empty( $this->orderField ) )
168
			throw new QueryException( 'Order field is not defined' );
169
170
		$query = /** @lang text */
171
			"UPDATE `{$this->table}` SET `{$this->orderField}` = CASE `{$this->primary[0]}` \r\n";
172
		foreach ( $updates_ord as $position => $id ) {
173
			$pos = $position + 1;
174
			$query .= " WHEN '$id' THEN '$pos' \r\n";
175
		}
176
		$query .= "ELSE `{$this->orderField}` END";
177
178
		return PdoWrapperService::getInstance()->query( $query, [] );
179
	}
180
181
182
	/**
183
	 * @param mixed
184
	 * @return array
185
	 * @throws QueryException
186
	 */
187
	protected function getPrimaryKeyConditions( $id ) {
188
189
		$id = (array)$id;
190
191
		if ( count( $this->primary ) !== count( $id ) )
192
			throw new QueryException( 'Invalid primary key', QueryException::QUERY_CRUD_INVALID_PRIMARY );
193
194
		$conditions = [];
195
196
		foreach ( $this->primary as $index => $key )
197
			$conditions[ $key ] = $id[ $index ];
198
199
		return $conditions;
200
	}
201
202
	/**
203
	 * @return int
204
	 */
205
	public function getInsertId(): int {
206
		return $this->insertId;
207
	}
208
209
210
}