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\Contract\UuidAwareInterface; |
12
|
|
|
use Ps2alerts\Api\Contract\UuidAwareTrait; |
13
|
|
|
|
14
|
|
|
abstract class AbstractEndpointRepository implements |
15
|
|
|
DatabaseAwareInterface, |
16
|
|
|
RedisAwareInterface, |
17
|
|
|
UuidAwareInterface |
18
|
|
|
{ |
19
|
|
|
use DatabaseAwareTrait; |
20
|
|
|
use RedisAwareTrait; |
21
|
|
|
use UuidAwareTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Determines the table that the DB is interfacing with |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
abstract public function getTable(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Determines the primary key of the table |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
abstract public function getPrimaryKey(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Determines the Result key of the table |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
abstract public function getResultKey(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Builds a new query factory ready for use with the QueryObjects |
46
|
|
|
* |
47
|
|
|
* @return \Aura\SqlQuery\AbstractQuery |
48
|
|
|
*/ |
49
|
|
|
public function newQuery() |
50
|
|
|
{ |
51
|
|
|
$factory = new QueryFactory('mysql'); |
52
|
|
|
|
53
|
|
|
$query = $factory->newSelect(); // Suspect I'll only ever need this one |
54
|
|
|
$query->from($this->getTable()); |
55
|
|
|
|
56
|
|
|
return $query; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Executes the statement to the DB and returns the results |
61
|
|
|
* |
62
|
|
|
* @param \Aura\SqlQuery\AbstractQuery $query |
63
|
|
|
* @param boolean $single |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function fireStatementAndReturn($query, $single = false) |
68
|
|
|
{ |
69
|
|
|
$pdo = $this->getDatabaseDriver(); |
70
|
|
|
|
71
|
|
|
if ($single === false) { |
72
|
|
|
return $pdo->fetchAll($query->getStatement(), $query->getBindValues()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $pdo->fetchOne($query->getStatement(), $query->getBindValues()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Allows for Raw SQL firing without the query builder |
80
|
|
|
* |
81
|
|
|
* @param \Aura\SqlQuery\AbstractQuery $query |
|
|
|
|
82
|
|
|
* @param boolean $single |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function readRaw($sql, $single = false) |
87
|
|
|
{ |
88
|
|
|
$pdo = $this->getDatabaseDriver(); |
89
|
|
|
|
90
|
|
|
if ($single === false) { |
91
|
|
|
return $pdo->fetchAll($sql); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $pdo->fetchOne($sql); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Reads a single record from the database |
99
|
|
|
* |
100
|
|
|
* @param string $id |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function readSinglebyId($id, $keyType = 'primary') |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$query = $this->newQuery(); |
107
|
|
|
$key = $this->returnKeyType($keyType); |
108
|
|
|
|
109
|
|
|
$query->cols(['*']) |
110
|
|
|
->where("`{$key}` = '{$id}'"); |
111
|
|
|
|
112
|
|
|
return $this->fireStatementAndReturn($query, true); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Reads all related records from the database |
117
|
|
|
* |
118
|
|
|
* @param string $id |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function readAllById($id, $keyType = 'primary') |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$query = $this->newQuery(); |
125
|
|
|
$key = $this->returnKeyType($keyType); |
126
|
|
|
|
127
|
|
|
$query->cols(['*']) |
128
|
|
|
->where("`{$key}` = '{$id}'"); |
129
|
|
|
|
130
|
|
|
return $this->fireStatementAndReturn($query); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Reads all records based off a simple where statement |
135
|
|
|
* |
136
|
|
|
* @param array $fields |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function readAllByFields($fields) |
141
|
|
|
{ |
142
|
|
|
$query = $this->newQuery(); |
143
|
|
|
|
144
|
|
|
$query->cols(['*']); |
145
|
|
|
|
146
|
|
|
foreach ($fields as $field => $value) { |
147
|
|
|
$query->where("`{$field}` = '{$value}'"); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this->fireStatementAndReturn($query); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Reads the count of records based off a where statement |
155
|
|
|
* |
156
|
|
|
* @param array $fields |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
public function readCountByFields($fields) |
161
|
|
|
{ |
162
|
|
|
$query = $this->newQuery(); |
163
|
|
|
$key = $this->returnKeyType('primary'); |
164
|
|
|
|
165
|
|
|
$query->cols(["COUNT({$key}) as COUNT"]); |
166
|
|
|
|
167
|
|
|
foreach ($fields as $field => $value) { |
168
|
|
|
$query->where("`{$field}` = '{$value}'"); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$result = $this->fireStatementAndReturn($query); |
172
|
|
|
|
173
|
|
|
// Done this to prevent the need for clients to also do this. Returns a single number this way. |
174
|
|
|
return $result[0]["COUNT"]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Sets the proper key to search on based off a string |
179
|
|
|
* |
180
|
|
|
* @param string $key |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function returnKeyType($key) |
185
|
|
|
{ |
186
|
|
|
switch ($key) { |
187
|
|
|
case 'result': |
188
|
|
|
return $this->getResultKey(); |
189
|
|
|
case 'primary': |
190
|
|
|
default: |
191
|
|
|
return $this->getPrimaryKey(); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.