| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Dsantang\DomainEventsDoctrine\Outbox; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\ORM\EntityManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\ORM\Event\OnFlushEventArgs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\ORM\UnitOfWork; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Dsantang\DomainEvents\Counter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Dsantang\DomainEvents\DomainEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Dsantang\DomainEvents\EventAware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function array_filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function array_merge; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use function ksort; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | abstract class EventsHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var OutboxMappedSuperclass */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $outboxMappedSuperclass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return DomainEvent[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 2 |  |     protected function getDomainsEvents(OnFlushEventArgs $eventArgs) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 2 |  |         $unitOfWork = $eventArgs->getEntityManager() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 2 |  |             ->getUnitOfWork(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 2 |  |         $events = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  |         foreach (self::getEventAwareEntities($unitOfWork) as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  |             $events += $entity->expelRecordedEvents(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 2 |  |         ksort($events); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |         Counter::reset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |         return $events; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param DomainEvent[] $domainEvents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return OutboxEntry[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     abstract protected function convert(DomainEvent ...$domainEvents) : array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function onFlush(OnFlushEventArgs $eventArgs) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $domainEvents = $this->getDomainsEvents($eventArgs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $outboxEvents = $this->convert(...$domainEvents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if (empty($outboxEvents)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->persist($eventArgs->getEntityManager(), ...$outboxEvents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |     protected function persist(EntityManagerInterface $entityManager, OutboxEntry ...$outboxEntries) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |         foreach ($outboxEntries as $outboxEntry) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |             $entity = $this->outboxMappedSuperclass->withOutboxEntry($outboxEntry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |             $entityManager->persist($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |         $entityManager->getUnitOfWork()->computeChangeSets(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return EventAware[] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 2 |  |     private static function getEventAwareEntities(UnitOfWork $unitOfWork) : array | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 2 |  |         $entities = array_merge( | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 2 |  |             $unitOfWork->getScheduledEntityInsertions(), | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 2 |  |             $unitOfWork->getScheduledEntityUpdates(), | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 2 |  |             $unitOfWork->getScheduledEntityDeletions() | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         return array_filter($entities, static function ($entity) { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 2 |  |             return $entity instanceof EventAware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  |