hydrateWithScheduledCollectionUpdates()   B
last analyzed

Complexity

Conditions 9
Paths 12

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 19
c 1
b 1
f 0
dl 0
loc 33
rs 8.0555
cc 9
nc 12
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Auditor\Provider\Doctrine\Auditing\Transaction;
6
7
use DH\Auditor\Model\TransactionInterface;
8
use DH\Auditor\Provider\Doctrine\DoctrineProvider;
9
use DH\Auditor\Provider\Doctrine\Model\Transaction;
10
use DH\Auditor\Transaction\TransactionHydratorInterface;
11
use Doctrine\ORM\EntityManagerInterface;
12
use Doctrine\ORM\PersistentCollection;
13
14
final class TransactionHydrator implements TransactionHydratorInterface
15
{
16
    use AuditTrait;
0 ignored issues
show
introduced by
The trait DH\Auditor\Provider\Doct...\Transaction\AuditTrait requires some properties which are not provided by DH\Auditor\Provider\Doct...ion\TransactionHydrator: $name, $fieldMappings, $embeddedClasses, $value
Loading history...
17
18
    private DoctrineProvider $provider;
19
20
    public function __construct(DoctrineProvider $provider)
21
    {
22
        $this->provider = $provider;
23
    }
24
25
    /**
26
     * @param Transaction $transaction
27
     */
28
    public function hydrate(TransactionInterface $transaction): void
29
    {
30
        $em = $transaction->getEntityManager();
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not exist on DH\Auditor\Model\TransactionInterface. It seems like you code against a sub-type of DH\Auditor\Model\TransactionInterface such as DH\Auditor\Provider\Doctrine\Model\Transaction. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        /** @scrutinizer ignore-call */ 
31
        $em = $transaction->getEntityManager();
Loading history...
31
        $this->hydrateWithScheduledInsertions($transaction, $em);
32
        $this->hydrateWithScheduledUpdates($transaction, $em);
33
        $this->hydrateWithScheduledDeletions($transaction, $em);
34
        $this->hydrateWithScheduledCollectionUpdates($transaction, $em);
35
        $this->hydrateWithScheduledCollectionDeletions($transaction, $em);
36
    }
37
38
    private function hydrateWithScheduledInsertions(Transaction $transaction, EntityManagerInterface $entityManager): void
39
    {
40
        $uow = $entityManager->getUnitOfWork();
41
        foreach (array_reverse($uow->getScheduledEntityInsertions()) as $entity) {
42
            if ($this->provider->isAudited($entity)) {
43
                $transaction->insert(
44
                    $entity,
45
                    $uow->getEntityChangeSet($entity),
46
                );
47
            }
48
        }
49
    }
50
51
    private function hydrateWithScheduledUpdates(Transaction $transaction, EntityManagerInterface $entityManager): void
52
    {
53
        $uow = $entityManager->getUnitOfWork();
54
        foreach (array_reverse($uow->getScheduledEntityUpdates()) as $entity) {
55
            if ($this->provider->isAudited($entity)) {
56
                $transaction->update(
57
                    $entity,
58
                    $uow->getEntityChangeSet($entity),
59
                );
60
            }
61
        }
62
    }
63
64
    private function hydrateWithScheduledDeletions(Transaction $transaction, EntityManagerInterface $entityManager): void
65
    {
66
        $uow = $entityManager->getUnitOfWork();
67
        foreach (array_reverse($uow->getScheduledEntityDeletions()) as $entity) {
68
            if ($this->provider->isAudited($entity)) {
69
                $uow->initializeObject($entity);
70
                $transaction->remove(
71
                    $entity,
72
                    $this->id($entityManager, $entity),
73
                );
74
            }
75
        }
76
    }
77
78
    private function hydrateWithScheduledCollectionUpdates(Transaction $transaction, EntityManagerInterface $entityManager): void
79
    {
80
        $uow = $entityManager->getUnitOfWork();
81
82
        /** @var PersistentCollection $collection */
83
        foreach (array_reverse($uow->getScheduledCollectionUpdates()) as $collection) {
84
            $owner = $collection->getOwner();
85
86
            if (null !== $owner && $this->provider->isAudited($owner)) {
87
                $mapping = $collection->getMapping();
88
89
                if (!\is_array($mapping)) {
90
                    continue;
91
                }
92
93
                /** @var object $entity */
94
                foreach ($collection->getInsertDiff() as $entity) {
95
                    if ($this->provider->isAudited($entity)) {
96
                        $transaction->associate(
97
                            $owner,
98
                            $entity,
99
                            $mapping,
100
                        );
101
                    }
102
                }
103
104
                /** @var object $entity */
105
                foreach ($collection->getDeleteDiff() as $entity) {
106
                    if ($this->provider->isAudited($entity)) {
107
                        $transaction->dissociate(
108
                            $owner,
109
                            $entity,
110
                            $mapping,
111
                        );
112
                    }
113
                }
114
            }
115
        }
116
    }
117
118
    private function hydrateWithScheduledCollectionDeletions(Transaction $transaction, EntityManagerInterface $entityManager): void
119
    {
120
        $uow = $entityManager->getUnitOfWork();
121
122
        /** @var PersistentCollection $collection */
123
        foreach (array_reverse($uow->getScheduledCollectionDeletions()) as $collection) {
124
            $owner = $collection->getOwner();
125
126
            if (null !== $owner && $this->provider->isAudited($owner)) {
127
                $mapping = $collection->getMapping();
128
129
                if (!\is_array($mapping)) {
130
                    continue;
131
                }
132
133
                /** @var object $entity */
134
                foreach ($collection->toArray() as $entity) {
135
                    if ($this->provider->isAudited($entity)) {
136
                        $transaction->dissociate(
137
                            $owner,
138
                            $entity,
139
                            $mapping,
140
                        );
141
                    }
142
                }
143
            }
144
        }
145
    }
146
}
147