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