1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ps2alerts\Api\Repository; |
4
|
|
|
|
5
|
|
|
use Aura\SqlQuery\AbstractQuery; |
6
|
|
|
use Aura\SqlQuery\QueryFactory; |
7
|
|
|
use Ps2alerts\Api\Contract\DatabaseAwareInterface; |
8
|
|
|
use Ps2alerts\Api\Contract\DatabaseAwareTrait; |
9
|
|
|
use Ps2alerts\Api\Contract\RedisAwareInterface; |
10
|
|
|
use Ps2alerts\Api\Contract\RedisAwareTrait; |
11
|
|
|
use Ps2alerts\Api\QueryObjects\QueryObject; |
12
|
|
|
|
13
|
|
|
abstract class AbstractEndpointRepository implements |
14
|
|
|
DatabaseAwareInterface, |
15
|
|
|
RedisAwareInterface |
16
|
|
|
{ |
17
|
|
|
use DatabaseAwareTrait; |
18
|
|
|
use RedisAwareTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Determines the table that the DB is interfacing with |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
abstract public function getTable(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Determines the primary key of the table |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
abstract public function getPrimaryKey(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Determines the Result key of the table |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
abstract public function getResultKey(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Builds a new query factory ready for use with the QueryObjects |
43
|
|
|
* |
44
|
|
|
* @return \Aura\SqlQuery\AbstractQuery |
45
|
|
|
*/ |
46
|
|
|
public function newQuery() |
47
|
|
|
{ |
48
|
|
|
$factory = new QueryFactory('mysql'); |
49
|
|
|
|
50
|
|
|
$query = $factory->newSelect(); // Suspect I'll only ever need this one |
51
|
|
|
$query->from($this->getTable()); |
52
|
|
|
|
53
|
|
|
return $query; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sets up the resulting query based off properties present in the supplied object. |
58
|
|
|
* |
59
|
|
|
* @see \Ps2alerts\Api\QueryObjects\QueryObject |
60
|
|
|
* |
61
|
|
|
* @param \Ps2alerts\Api\QueryObjects\QueryObject $queryObject |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function read(QueryObject $queryObject) |
65
|
|
|
{ |
66
|
|
|
$query = $this->newQuery(); |
67
|
|
|
|
68
|
|
|
// Setup select statements |
69
|
|
|
if (! empty($queryObject->getSelects())) { |
70
|
|
|
$query->cols($queryObject->getSelects()); |
71
|
|
|
} else { |
72
|
|
|
$query->cols(['*']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Workarounds :-/ |
76
|
|
|
if (! empty($queryObject->getFlags())) { |
77
|
|
|
if ($queryObject->getFlags() === 'outfitIDs') { |
78
|
|
|
// Prevent the VS, NC and TR "no outfit" workaround |
79
|
|
|
$queryObject->addWhere([ |
80
|
|
|
'col' => 'outfitID', |
81
|
|
|
'op' => '>', |
82
|
|
|
'value' => 0 |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Setup where statements |
88
|
|
|
if (! empty($queryObject->getWheres())) { |
89
|
|
|
foreach ($queryObject->getWheres() as $where) { |
90
|
|
|
$col = $where['col']; |
91
|
|
|
|
92
|
|
|
if ($where['col'] === 'primary') { |
93
|
|
|
$col = $this->getPrimaryKey(); |
94
|
|
|
} |
95
|
|
|
if ($where['col'] === 'result') { |
96
|
|
|
$col = $this->getResultKey(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$op = (isset($where['op']) ? $where['op'] : '='); |
100
|
|
|
|
101
|
|
|
$query->where("{$col} {$op} :{$col}"); |
102
|
|
|
$query->bindValue($col, $where['value']); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Setup where statements |
107
|
|
|
if (! empty($queryObject->getWhereIns())) { |
108
|
|
|
foreach ($queryObject->getWhereIns() as $whereIn) { |
109
|
|
|
$col = $whereIn['col']; |
110
|
|
|
|
111
|
|
|
if ($whereIn['col'] === 'primary') { |
112
|
|
|
$col = $this->getPrimaryKey(); |
113
|
|
|
} |
114
|
|
|
if ($whereIn['col'] === 'result') { |
115
|
|
|
$col = $this->getResultKey(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$query->where("{$col} IN ({$whereIn['value']})"); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Set up order statement |
123
|
|
|
if (! empty($queryObject->getOrderBy())) { |
124
|
|
|
$orderBy = $queryObject->getOrderBy(); |
125
|
|
|
if ($orderBy === 'primary') { |
126
|
|
|
$orderBy = $this->getPrimaryKey(); |
127
|
|
|
} |
128
|
|
|
if ($orderBy === 'result') { |
129
|
|
|
$orderBy = $this->getResultKey(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$query->orderBy([ |
|
|
|
|
133
|
|
|
"`{$orderBy}` {$queryObject->getOrderByDirection()}" |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (! empty($queryObject->getLimit())) { |
138
|
|
|
$query->limit($queryObject->getLimit()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->prepareAndExecuteQuery($query, $queryObject); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Sets up the PDO Driver, then executes the query based on dimension. |
146
|
|
|
* |
147
|
|
|
* @param \Aura\SqlQuery\AbstractQuery $query |
148
|
|
|
* @param \Ps2alerts\Api\QueryObjects\QueryObject $queryObject Sent QueryObject to read dimension |
149
|
|
|
* @return array The final data |
150
|
|
|
*/ |
151
|
|
|
public function prepareAndExecuteQuery(AbstractQuery $query, QueryObject $queryObject) |
152
|
|
|
{ |
153
|
|
|
$pdo = $this->getDatabaseDriver(); |
154
|
|
|
|
155
|
|
|
if ($queryObject->getDimension() === 'multi') { |
156
|
|
|
return $pdo->fetchAll($query->getStatement(), $query->getBindValues()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $pdo->fetchOne($query->getStatement(), $query->getBindValues()); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.