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