Passed
Push — master ( 13ce29...e039be )
by Adrian
02:19
created

AbstractTableCrud::getPrimaryKeyConditions()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 4
nc 6
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Adi
5
 * Date: 7/29/2018
6
 * Time: 12:58 AM
7
 */
8
9
namespace Qpdb\QueryBuilder\AbstractCruds;
10
11
12
use Qpdb\QueryBuilder\DB\DbService;
13
use Qpdb\QueryBuilder\Dependencies\QueryException;
14
use Qpdb\QueryBuilder\QueryBuild;
15
16
abstract class AbstractTableCrud
17
{
18
19
	/**
20
	 * @var string
21
	 */
22
	protected $table;
23
24
	/**
25
	 * @var string|array
26
	 */
27
	protected $primary;
28
29
	/**
30
	 * @var string
31
	 */
32
	protected $orderField;
33
34
	/**
35
	 * @var mixed
36
	 */
37
	protected $lastInsertId;
38
39
40
	abstract protected function setTable();
41
42
	abstract protected function setPrimaryKey();
43
44
	abstract protected function setOrderField();
45
46
47
	public function __construct()
48
	{
49
		$this->setTable();
50
		$this->setPrimaryKey();
51
		$this->setOrderField();
52
53
		if ( !is_array( $this->primary ) )
54
			$this->primary = [ $this->primary ];
55
	}
56
57
	/**
58
	 * @param $id
59
	 * @param array $fields
60
	 * @return array|bool
61
	 * @throws QueryException
62
	 */
63
	public function getRowById( $id, array $fields = [] )
64
	{
65
		$conditions = $this->getPrimaryKeyConditions( $id );
66
		$result = QueryBuild::select( $this->table )->fields( $fields );
67
		foreach ( $conditions as $field => $value )
68
			$result->whereEqual( $field, $value );
69
70
		return $result->first()->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result->first()->execute() also could return the type integer|string which is incompatible with the documented return type boolean|array.
Loading history...
71
	}
72
73
74
	/**
75
	 * @param $id
76
	 * @return array|int|null
77
	 * @throws QueryException
78
	 */
79
	public function deleteRowById( $id )
80
	{
81
		$conditions = $this->getPrimaryKeyConditions( $id );
82
		$result = QueryBuild::delete( $this->table );
83
		foreach ( $conditions as $field => $value )
84
			$result->whereEqual( $field, $value );
85
86
		return $result->execute();
87
	}
88
89
	/**
90
	 * @param $id
91
	 * @param array $arrayUpdater
92
	 * @return array|int|null
93
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
94
	 */
95
	public function updateRowById( $id, array $arrayUpdater )
96
	{
97
		$conditions = $this->getPrimaryKeyConditions( $id );
98
		$result = QueryBuild::update( $this->table );
99
		foreach ( $conditions as $field => $value )
100
			$result->whereEqual( $field, $value );
101
		$result->setFieldsByArray( $arrayUpdater );
102
103
		return $result->execute();
104
	}
105
106
107
	/**
108
	 * @param array $arrayValues
109
	 * @return array|int|null
110
	 */
111
	public function insertRow( array $arrayValues )
112
	{
113
		$result = QueryBuild::insert( $this->table )->setFieldsByArray( $arrayValues );
114
		$this->lastInsertId = DbService::getInstance()->getLastInsertId();
115
116
		return $result->execute();
117
	}
118
119
120
	/**
121
	 * @return mixed
122
	 */
123
	public function lastInsertId()
124
	{
125
		return $this->lastInsertId;
126
	}
127
128
129
	/**
130
	 * @param array $updates_ord
131
	 * @return int
132
	 * @throws QueryException
133
	 */
134
	public function saveOrder( $updates_ord = array() )
135
	{
136
		if ( empty( $this->orderField ) )
137
			throw new QueryException( 'Order field is not defined' );
138
139
		$query = /** @lang text */
140
			"UPDATE `{$this->table}` SET `{$this->orderField}` = CASE `{$this->primary[0]}` \r\n";
141
		foreach ( $updates_ord as $position => $id ) {
142
			$pos = $position + 1;
143
			$query .= " WHEN '$id' THEN '$pos' \r\n";
144
		}
145
		$query .= "ELSE `{$this->orderField}` END";
146
147
		return DbService::getInstance()->query( $query, [] );
0 ignored issues
show
Bug Best Practice introduced by
The expression return Qpdb\QueryBuilder...>query($query, array()) also could return the type array which is incompatible with the documented return type integer.
Loading history...
148
	}
149
150
151
	/**
152
	 * @param mixed
153
	 * @return array
154
	 * @throws QueryException
155
	 */
156
	protected function getPrimaryKeyConditions( $id )
157
	{
158
		if ( !is_array( $id ) )
159
			$id = [ $id ];
160
161
		if ( count( $this->primary ) !== count( $id ) )
0 ignored issues
show
Bug introduced by
It seems like $this->primary can also be of type string; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
		if ( count( /** @scrutinizer ignore-type */ $this->primary ) !== count( $id ) )
Loading history...
162
			throw new QueryException( 'Invalid primary key', QueryException::QUERY_CRUD_INVALID_PRIMARY );
163
164
		$conditions = [];
165
166
		foreach ( $this->primary as $index => $key )
167
			$conditions[ $key ] = $id[ $index ];
168
169
		return $conditions;
170
	}
171
172
173
}