Passed
Branch master (4b81e6)
by Adrian
03:33 queued 23s
created

AbstractTableDao::getMaxOrder()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
cc 4
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
	/**
36
	 * AbstractTableCrud constructor.
37
	 */
38
	public function __construct() {
39
		$this->table = $this->getTableName();
40
		$this->primary = (array)$this->getPrimaryKey();
41
		$this->orderField = $this->getOrderField();
42
	}
43
44
45
	/**
46
	 * @return string
47
	 */
48
	abstract protected function getTableName();
49
50
	/**
51
	 * @return string|array
52
	 */
53
	abstract protected function getPrimaryKey();
54
55
	/**
56
	 * @return string
57
	 */
58
	abstract protected function getOrderField();
59
60
61
	/**
62
	 * @param       $id
63
	 * @param array $fields
64
	 * @return array|bool
65
	 * @throws QueryException
66
	 */
67
	public function getRowById( $id, array $fields = [] ) {
68
		$conditions = $this->getPrimaryKeyConditions( $id );
69
		$result = QueryBuild::select( $this->table )->fields( $fields );
70
		foreach ( $conditions as $field => $value )
71
			$result->whereEqual( $field, $value );
72
73
		return $result->first()->execute();
74
	}
75
76
77
	/**
78
	 * @param $id
79
	 * @return array|int|null
80
	 * @throws QueryException
81
	 */
82
	public function deleteRowById( $id ) {
83
		$conditions = $this->getPrimaryKeyConditions( $id );
84
		$result = QueryBuild::delete( $this->table );
85
		foreach ( $conditions as $field => $value )
86
			$result->whereEqual( $field, $value );
87
88
		return $result->execute();
89
	}
90
91
	/**
92
	 * @param       $id
93
	 * @param array $arrayUpdater
94
	 * @return array|int|null
95
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
96
	 */
97
	public function updateRowById( $id, array $arrayUpdater ) {
98
		$conditions = $this->getPrimaryKeyConditions( $id );
99
		$result = QueryBuild::update( $this->table );
100
		foreach ( $conditions as $field => $value )
101
			$result->whereEqual( $field, $value );
102
		$result->setFieldsByArray( $arrayUpdater );
103
104
		return $result->execute();
105
	}
106
107
108
	/**
109
	 * @param array $arrayValues
110
	 * @return bool|mixed|\PDOStatement
111
	 * @throws QueryException
112
	 */
113
	public function insertRow( array $arrayValues ) {
114
		return QueryBuild::insert( $this->table )->setFieldsByArray( $arrayValues )->execute();
115
	}
116
117
	/**
118
	 * @param bool $nullToZero
119
	 * @return null|mixed
120
	 * @throws QueryException
121
	 */
122
	public function getMaxOrder( $nullToZero = false ) {
123
		if ( empty( $this->orderField ) )
124
			throw new QueryException( 'Order field is not defined' );
125
126
		$result = QueryBuild::select( $this->table )
127
			->fieldExpression( "MAX(`{$this->orderField}`)", "max_order_column_alias" )
128
			->first()
129
			->column( 'max_order_column_alias' )
130
			->execute();
131
132
		if ( null === $result && $nullToZero ) {
0 ignored issues
show
introduced by
The condition null === $result is always false.
Loading history...
133
			return 0;
134
		}
135
136
		return $result;
137
	}
138
139
	/**
140
	 * @param array $updates_ord
141
	 * @return bool|\PDOStatement
142
	 * @throws QueryException
143
	 */
144
	public function saveOrder( $updates_ord = array() ) {
145
		if ( empty( $this->orderField ) )
146
			throw new QueryException( 'Order field is not defined' );
147
148
		$query = /** @lang text */
149
			"UPDATE `{$this->table}` SET `{$this->orderField}` = CASE `{$this->primary[0]}` \r\n";
150
		foreach ( $updates_ord as $position => $id ) {
151
			$pos = $position + 1;
152
			$query .= " WHEN '$id' THEN '$pos' \r\n";
153
		}
154
		$query .= "ELSE `{$this->orderField}` END";
155
156
		return PdoWrapperService::getInstance()->query( $query, [] );
157
	}
158
159
160
	/**
161
	 * @param mixed
162
	 * @return array
163
	 * @throws QueryException
164
	 */
165
	protected function getPrimaryKeyConditions( $id ) {
166
167
		$id = (array)$id;
168
169
		if ( count( $this->primary ) !== count( $id ) )
170
			throw new QueryException( 'Invalid primary key', QueryException::QUERY_CRUD_INVALID_PRIMARY );
171
172
		$conditions = [];
173
174
		foreach ( $this->primary as $index => $key )
175
			$conditions[ $key ] = $id[ $index ];
176
177
		return $conditions;
178
	}
179
180
181
}