| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace DH\DoctrineAuditBundle\Transaction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use DH\DoctrineAuditBundle\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use DH\DoctrineAuditBundle\Model\Transaction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\ORM\EntityManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\ORM\UnitOfWork; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class TransactionHydrator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     use AuditTrait; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var Configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var EntityManagerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $em; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function __construct(Configuration $configuration) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->configuration = $configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->em = $this->configuration->getEntityManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function hydrate(Transaction $transaction): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $uow = $this->em->getUnitOfWork(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->hydrateWithScheduledInsertions($transaction, $uow); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->hydrateWithScheduledUpdates($transaction, $uow); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->hydrateWithScheduledDeletions($transaction, $uow, $this->em); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->hydrateWithScheduledCollectionUpdates($transaction, $uow, $this->em); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->hydrateWithScheduledCollectionDeletions($transaction, $uow, $this->em); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param UnitOfWork $uow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private function hydrateWithScheduledInsertions(Transaction $transaction, UnitOfWork $uow): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         foreach ($uow->getScheduledEntityInsertions() as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             if ($this->configuration->isAudited($entity)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $transaction->trackAuditEvent(Transaction::INSERT, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                     $entity, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     $uow->getEntityChangeSet($entity), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param UnitOfWork $uow | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     private function hydrateWithScheduledUpdates(Transaction $transaction, UnitOfWork $uow): void | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         foreach ($uow->getScheduledEntityUpdates() as $entity) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             if ($this->configuration->isAudited($entity)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 $transaction->trackAuditEvent(Transaction::UPDATE, [ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                     $entity, | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                     $uow->getEntityChangeSet($entity), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param UnitOfWork             $uow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param EntityManagerInterface $em | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @throws \Doctrine\ORM\Mapping\MappingException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     private function hydrateWithScheduledDeletions(Transaction $transaction, UnitOfWork $uow, EntityManagerInterface $em): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         foreach ($uow->getScheduledEntityDeletions() as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             if ($this->configuration->isAudited($entity)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 $uow->initializeObject($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $transaction->trackAuditEvent(Transaction::REMOVE, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     $entity, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     $this->id($em, $entity), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param UnitOfWork             $uow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param EntityManagerInterface $em | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @throws \Doctrine\ORM\Mapping\MappingException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     private function hydrateWithScheduledCollectionUpdates(Transaction $transaction, UnitOfWork $uow, EntityManagerInterface $em): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         foreach ($uow->getScheduledCollectionUpdates() as $collection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             if ($this->configuration->isAudited($collection->getOwner())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 $mapping = $collection->getMapping(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 foreach ($collection->getInsertDiff() as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     if ($this->configuration->isAudited($entity)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                         $transaction->trackAuditEvent(Transaction::ASSOCIATE, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                             $collection->getOwner(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                             $entity, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                             $mapping, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 foreach ($collection->getDeleteDiff() as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     if ($this->configuration->isAudited($entity)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                         $transaction->trackAuditEvent(Transaction::DISSOCIATE, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                             $collection->getOwner(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                             $entity, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                             $this->id($em, $entity), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                             $mapping, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param UnitOfWork             $uow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param EntityManagerInterface $em | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @throws \Doctrine\ORM\Mapping\MappingException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     private function hydrateWithScheduledCollectionDeletions(Transaction $transaction, UnitOfWork $uow, EntityManagerInterface $em): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         foreach ($uow->getScheduledCollectionDeletions() as $collection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             if ($this->configuration->isAudited($collection->getOwner())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 $mapping = $collection->getMapping(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 foreach ($collection->toArray() as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                     if ($this->configuration->isAudited($entity)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                         $transaction->trackAuditEvent(Transaction::DISSOCIATE, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                             $collection->getOwner(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                             $entity, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                             $this->id($em, $entity), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                             $mapping, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |  |