1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\DoctrineAuditBundle\Manager; |
4
|
|
|
|
5
|
|
|
use DH\DoctrineAuditBundle\Helper\AuditHelper; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Doctrine\ORM\UnitOfWork; |
9
|
|
|
|
10
|
|
|
class AuditTransaction |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \DH\DoctrineAuditBundle\AuditConfiguration |
14
|
|
|
*/ |
15
|
|
|
private $configuration; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var AuditHelper |
19
|
|
|
*/ |
20
|
|
|
private $helper; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var null|string |
24
|
|
|
*/ |
25
|
|
|
private $transaction_hash; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $inserted = []; // [$source, $changeset] |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $updated = []; // [$source, $changeset] |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $removed = []; // [$source, $id] |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $associated = []; // [$source, $target, $mapping] |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $dissociated = []; // [$source, $target, $id, $mapping] |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var EntityManagerInterface |
54
|
|
|
*/ |
55
|
|
|
private $em; |
56
|
|
|
|
57
|
|
|
public function __construct(AuditHelper $helper) |
58
|
|
|
{ |
59
|
|
|
$this->helper = $helper; |
60
|
|
|
$this->configuration = $helper->getConfiguration(); |
61
|
|
|
$this->em = $this->configuration->getEntityManager(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns transaction hash. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getTransactionHash(): string |
70
|
|
|
{ |
71
|
|
|
if (null === $this->transaction_hash) { |
72
|
|
|
$this->transaction_hash = sha1(uniqid('tid', true)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->transaction_hash; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function collect(): void |
79
|
|
|
{ |
80
|
|
|
$uow = $this->em->getUnitOfWork(); |
81
|
|
|
|
82
|
|
|
$this->collectScheduledInsertions($uow); |
83
|
|
|
$this->collectScheduledUpdates($uow); |
84
|
|
|
$this->collectScheduledDeletions($uow, $this->em); |
85
|
|
|
$this->collectScheduledCollectionUpdates($uow, $this->em); |
86
|
|
|
$this->collectScheduledCollectionDeletions($uow, $this->em); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param UnitOfWork $uow |
91
|
|
|
*/ |
92
|
|
|
public function collectScheduledInsertions(UnitOfWork $uow): void |
93
|
|
|
{ |
94
|
|
|
foreach ($uow->getScheduledEntityInsertions() as $entity) { |
95
|
|
|
if ($this->configuration->isAudited($entity)) { |
96
|
|
|
$this->inserted[] = [ |
97
|
|
|
$entity, |
98
|
|
|
$uow->getEntityChangeSet($entity), |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param UnitOfWork $uow |
106
|
|
|
*/ |
107
|
|
|
public function collectScheduledUpdates(UnitOfWork $uow): void |
108
|
|
|
{ |
109
|
|
|
foreach ($uow->getScheduledEntityUpdates() as $entity) { |
110
|
|
|
if ($this->configuration->isAudited($entity)) { |
111
|
|
|
$this->updated[] = [ |
112
|
|
|
$entity, |
113
|
|
|
$uow->getEntityChangeSet($entity), |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param UnitOfWork $uow |
121
|
|
|
* @param EntityManager $em |
122
|
|
|
* |
123
|
|
|
* @throws \Doctrine\DBAL\DBALException |
124
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
125
|
|
|
*/ |
126
|
|
|
public function collectScheduledDeletions(UnitOfWork $uow, EntityManager $em): void |
127
|
|
|
{ |
128
|
|
|
foreach ($uow->getScheduledEntityDeletions() as $entity) { |
129
|
|
|
if ($this->configuration->isAudited($entity)) { |
130
|
|
|
$uow->initializeObject($entity); |
131
|
|
|
$this->removed[] = [ |
132
|
|
|
$entity, |
133
|
|
|
$this->helper->id($em, $entity), |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param UnitOfWork $uow |
141
|
|
|
* @param EntityManager $em |
142
|
|
|
* |
143
|
|
|
* @throws \Doctrine\DBAL\DBALException |
144
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
145
|
|
|
*/ |
146
|
|
|
public function collectScheduledCollectionUpdates(UnitOfWork $uow, EntityManager $em): void |
147
|
|
|
{ |
148
|
|
|
foreach ($uow->getScheduledCollectionUpdates() as $collection) { |
149
|
|
|
if ($this->configuration->isAudited($collection->getOwner())) { |
150
|
|
|
$mapping = $collection->getMapping(); |
151
|
|
|
foreach ($collection->getInsertDiff() as $entity) { |
152
|
|
|
if ($this->configuration->isAudited($entity)) { |
153
|
|
|
$this->associated[] = [ |
154
|
|
|
$collection->getOwner(), |
155
|
|
|
$entity, |
156
|
|
|
$mapping, |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
foreach ($collection->getDeleteDiff() as $entity) { |
161
|
|
|
if ($this->configuration->isAudited($entity)) { |
162
|
|
|
$this->dissociated[] = [ |
163
|
|
|
$collection->getOwner(), |
164
|
|
|
$entity, |
165
|
|
|
$this->helper->id($em, $entity), |
166
|
|
|
$mapping, |
167
|
|
|
]; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param UnitOfWork $uow |
176
|
|
|
* @param EntityManager $em |
177
|
|
|
* |
178
|
|
|
* @throws \Doctrine\DBAL\DBALException |
179
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
180
|
|
|
*/ |
181
|
|
|
public function collectScheduledCollectionDeletions(UnitOfWork $uow, EntityManager $em): void |
182
|
|
|
{ |
183
|
|
|
foreach ($uow->getScheduledCollectionDeletions() as $collection) { |
184
|
|
|
if ($this->configuration->isAudited($collection->getOwner())) { |
185
|
|
|
$mapping = $collection->getMapping(); |
186
|
|
|
foreach ($collection->toArray() as $entity) { |
187
|
|
|
if ($this->configuration->isAudited($entity)) { |
188
|
|
|
$this->dissociated[] = [ |
189
|
|
|
$collection->getOwner(), |
190
|
|
|
$entity, |
191
|
|
|
$this->helper->id($em, $entity), |
192
|
|
|
$mapping, |
193
|
|
|
]; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
public function getPayload(): array |
204
|
|
|
{ |
205
|
|
|
$payload = []; |
206
|
|
|
|
207
|
|
|
if (\count($this->getInserted()) > 0) { |
208
|
|
|
$payload['inserted'] = $this->getInserted(); |
209
|
|
|
} |
210
|
|
|
if (\count($this->getUpdated()) > 0) { |
211
|
|
|
$payload['updated'] = $this->getUpdated(); |
212
|
|
|
} |
213
|
|
|
if (\count($this->getRemoved()) > 0) { |
214
|
|
|
$payload['removed'] = $this->getRemoved(); |
215
|
|
|
} |
216
|
|
|
if (\count($this->getAssociated()) > 0) { |
217
|
|
|
$payload['associated'] = $this->getAssociated(); |
218
|
|
|
} |
219
|
|
|
if (\count($this->getDissociated()) > 0) { |
220
|
|
|
$payload['dissociated'] = $this->getDissociated(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $payload; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function getInserted(): array |
227
|
|
|
{ |
228
|
|
|
return $this->inserted; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function getUpdated(): array |
232
|
|
|
{ |
233
|
|
|
return $this->updated; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function getRemoved(): array |
237
|
|
|
{ |
238
|
|
|
return $this->removed; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function getAssociated(): array |
242
|
|
|
{ |
243
|
|
|
return $this->associated; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function getDissociated(): array |
247
|
|
|
{ |
248
|
|
|
return $this->dissociated; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function getEntityManager(): EntityManagerInterface |
252
|
|
|
{ |
253
|
|
|
return $this->em; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|