|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Oc\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use Doctrine\DBAL\Connection; |
|
7
|
|
|
use Oc\Entity\GeoCacheAdoptionsEntity; |
|
8
|
|
|
use Oc\Repository\Exception\RecordAlreadyExistsException; |
|
9
|
|
|
use Oc\Repository\Exception\RecordNotFoundException; |
|
10
|
|
|
use Oc\Repository\Exception\RecordNotPersistedException; |
|
11
|
|
|
use Oc\Repository\Exception\RecordsNotFoundException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class CacheAdoptionsRepository |
|
15
|
|
|
* |
|
16
|
|
|
* @package Oc\Repository# |
|
17
|
|
|
*/ |
|
18
|
|
|
class CacheAdoptionsRepository |
|
19
|
|
|
{ |
|
20
|
|
|
const TABLE = 'cache_adoptions'; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Connection */ |
|
23
|
|
|
private $connection; |
|
24
|
|
|
|
|
25
|
|
|
/** @var UserRepository */ |
|
26
|
|
|
private $userRepository; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* CacheAdoptionsRepository constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param Connection $connection |
|
32
|
|
|
* @param UserRepository $userRepository |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(Connection $connection, UserRepository $userRepository) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->connection = $connection; |
|
37
|
|
|
$this->userRepository = $userRepository; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return array |
|
42
|
|
|
* @throws RecordsNotFoundException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function fetchAll() |
|
45
|
|
|
{ |
|
46
|
|
|
$statement = $this->connection->createQueryBuilder() |
|
47
|
|
|
->select('*') |
|
48
|
|
|
->from(self::TABLE) |
|
49
|
|
|
->execute(); |
|
50
|
|
|
|
|
51
|
|
|
$result = $statement->fetchAll(); |
|
52
|
|
|
|
|
53
|
|
|
if ($statement->rowCount() === 0) { |
|
54
|
|
|
throw new RecordsNotFoundException('No records found'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$records = []; |
|
58
|
|
|
|
|
59
|
|
|
foreach ($result as $item) { |
|
60
|
|
|
$records[] = $this->getEntityFromDatabaseArray($item); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $records; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array $where |
|
68
|
|
|
* |
|
69
|
|
|
* @return GeoCacheAdoptionsEntity |
|
70
|
|
|
* @throws RecordNotFoundException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function fetchOneBy(array $where = []) |
|
73
|
|
|
{ |
|
74
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
|
75
|
|
|
->select('*') |
|
76
|
|
|
->from(self::TABLE) |
|
77
|
|
|
->setMaxResults(1); |
|
78
|
|
|
|
|
79
|
|
|
if (count($where) > 0) { |
|
80
|
|
|
foreach ($where as $column => $value) { |
|
81
|
|
|
$queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value)); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$statement = $queryBuilder->execute(); |
|
86
|
|
|
|
|
87
|
|
|
$result = $statement->fetch(); |
|
88
|
|
|
|
|
89
|
|
|
if ($statement->rowCount() === 0) { |
|
90
|
|
|
throw new RecordNotFoundException('Record with given where clause not found'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->getEntityFromDatabaseArray($result); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array $where |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
*/ |
|
102
|
|
|
public function fetchBy(array $where = []) |
|
103
|
|
|
{ |
|
104
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
|
105
|
|
|
->select('*') |
|
106
|
|
|
->from(self::TABLE); |
|
107
|
|
|
|
|
108
|
|
|
if (count($where) > 0) { |
|
109
|
|
|
foreach ($where as $column => $value) { |
|
110
|
|
|
$queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value)); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$statement = $queryBuilder->execute(); |
|
115
|
|
|
|
|
116
|
|
|
$result = $statement->fetchAll(); |
|
117
|
|
|
|
|
118
|
|
|
if ($statement->rowCount() === 0) { |
|
119
|
|
|
// throw new RecordsNotFoundException('No records with given where clause found'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$entities = []; |
|
123
|
|
|
|
|
124
|
|
|
foreach ($result as $item) { |
|
125
|
|
|
$entities[] = $this->getEntityFromDatabaseArray($item); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $entities; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param GeoCacheAdoptionsEntity $entity |
|
133
|
|
|
* |
|
134
|
|
|
* @return GeoCacheAdoptionsEntity |
|
135
|
|
|
* @throws RecordAlreadyExistsException |
|
136
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
137
|
|
|
*/ |
|
138
|
|
|
public function create(GeoCacheAdoptionsEntity $entity) |
|
139
|
|
|
{ |
|
140
|
|
|
if (!$entity->isNew()) { |
|
141
|
|
|
throw new RecordAlreadyExistsException('The entity does already exist.'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$databaseArray = $this->getDatabaseArrayFromEntity($entity); |
|
145
|
|
|
|
|
146
|
|
|
$this->connection->insert( |
|
147
|
|
|
self::TABLE, |
|
148
|
|
|
$databaseArray |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
$entity->id = (int) $this->connection->lastInsertId(); |
|
152
|
|
|
|
|
153
|
|
|
return $entity; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param GeoCacheAdoptionsEntity $entity |
|
158
|
|
|
* |
|
159
|
|
|
* @return GeoCacheAdoptionsEntity |
|
160
|
|
|
* @throws RecordNotPersistedException |
|
161
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
162
|
|
|
*/ |
|
163
|
|
|
public function update(GeoCacheAdoptionsEntity $entity) |
|
164
|
|
|
{ |
|
165
|
|
|
if ($entity->isNew()) { |
|
166
|
|
|
throw new RecordNotPersistedException('The entity does not exist.'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$databaseArray = $this->getDatabaseArrayFromEntity($entity); |
|
170
|
|
|
|
|
171
|
|
|
$this->connection->update( |
|
172
|
|
|
self::TABLE, |
|
173
|
|
|
$databaseArray, |
|
174
|
|
|
['id' => $entity->id] |
|
175
|
|
|
); |
|
176
|
|
|
|
|
177
|
|
|
return $entity; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param GeoCacheAdoptionsEntity $entity |
|
182
|
|
|
* |
|
183
|
|
|
* @return GeoCacheAdoptionsEntity |
|
184
|
|
|
* @throws RecordNotPersistedException |
|
185
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
186
|
|
|
* @throws \Doctrine\DBAL\Exception\InvalidArgumentException |
|
187
|
|
|
*/ |
|
188
|
|
|
public function remove(GeoCacheAdoptionsEntity $entity) |
|
189
|
|
|
{ |
|
190
|
|
|
if ($entity->isNew()) { |
|
191
|
|
|
throw new RecordNotPersistedException('The entity does not exist.'); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$this->connection->delete( |
|
195
|
|
|
self::TABLE, |
|
196
|
|
|
['id' => $entity->id] |
|
197
|
|
|
); |
|
198
|
|
|
|
|
199
|
|
|
$entity->cacheId = null; |
|
200
|
|
|
|
|
201
|
|
|
return $entity; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param GeoCacheAdoptionsEntity $entity |
|
206
|
|
|
* |
|
207
|
|
|
* @return array |
|
208
|
|
|
*/ |
|
209
|
|
|
public function getDatabaseArrayFromEntity(GeoCacheAdoptionsEntity $entity) |
|
210
|
|
|
{ |
|
211
|
|
|
return [ |
|
212
|
|
|
'id' => $entity->id, |
|
213
|
|
|
'cache_id' => $entity->cacheId, |
|
214
|
|
|
'date' => $entity->date, |
|
215
|
|
|
'from_user_id' => $entity->fromUserId, |
|
216
|
|
|
'to_user_id' => $entity->toUserId, |
|
217
|
|
|
'from_user' => $entity->fromUser, |
|
218
|
|
|
'to_user' => $entity->toUser, |
|
219
|
|
|
]; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @param array $data |
|
224
|
|
|
* |
|
225
|
|
|
* @return GeoCacheAdoptionsEntity |
|
226
|
|
|
* @throws \Exception |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getEntityFromDatabaseArray(array $data) |
|
229
|
|
|
{ |
|
230
|
|
|
$entity = new GeoCacheAdoptionsEntity(); |
|
231
|
|
|
$entity->id = (int) $data['id']; |
|
232
|
|
|
$entity->cacheId = (int) $data['cache_id']; |
|
233
|
|
|
$entity->date = new DateTime($data['date']); |
|
234
|
|
|
$entity->fromUserId = (int) $data['from_user_id']; |
|
235
|
|
|
$entity->toUserId = (int) $data['to_user_id']; |
|
236
|
|
|
$entity->fromUser = $this->userRepository->fetchOneById($entity->fromUserId); |
|
|
|
|
|
|
237
|
|
|
$entity->toUser = $this->userRepository->fetchOneById($entity->toUserId); |
|
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
return $entity; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..