Passed
Pull Request — master (#90)
by Damien
02:52
created

collectScheduledCollectionUpdates()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
nc 11
nop 2
dl 0
loc 21
rs 8.8333
c 1
b 0
f 0
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
    public function getInserted(): array
201
    {
202
        return $this->inserted;
203
    }
204
205
    public function getUpdated(): array
206
    {
207
        return $this->updated;
208
    }
209
210
    public function getRemoved(): array
211
    {
212
        return $this->removed;
213
    }
214
215
    public function getAssociated(): array
216
    {
217
        return $this->associated;
218
    }
219
220
    public function getDissociated(): array
221
    {
222
        return $this->dissociated;
223
    }
224
225
    public function getEntityManager(): EntityManagerInterface
226
    {
227
        return $this->em;
228
    }
229
}
230