dsantang /
domain-events-doctrine
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Dsantang\DomainEventsDoctrine\EventsRecorder; |
||
| 6 | |||
| 7 | use Doctrine\ORM\Event\OnFlushEventArgs; |
||
| 8 | use Doctrine\ORM\UnitOfWork; |
||
| 9 | use Dsantang\DomainEvents\Counter; |
||
| 10 | use Dsantang\DomainEvents\EventAware; |
||
| 11 | use Dsantang\DomainEventsDoctrine\Aggregator; |
||
| 12 | |||
| 13 | use function array_filter; |
||
| 14 | use function array_merge; |
||
| 15 | use function ksort; |
||
| 16 | |||
| 17 | final class OrderedDoctrineEventsRecorder |
||
| 18 | { |
||
| 19 | private Aggregator $eventsAggregator; |
||
| 20 | |||
| 21 | 2 | public function __construct(Aggregator $eventAggregator) |
|
| 22 | { |
||
| 23 | 2 | $this->eventsAggregator = $eventAggregator; |
|
| 24 | 2 | } |
|
| 25 | |||
| 26 | 2 | public function onFlush(OnFlushEventArgs $eventArgs): void |
|
| 27 | { |
||
| 28 | 2 | $unitOfWork = $eventArgs->getEntityManager() |
|
|
0 ignored issues
–
show
|
|||
| 29 | 2 | ->getUnitOfWork(); |
|
| 30 | |||
| 31 | 2 | $events = []; |
|
| 32 | |||
| 33 | 2 | foreach (self::getEventAwareEntities($unitOfWork) as $entity) { |
|
| 34 | 1 | $events += $entity->expelRecordedEvents(); |
|
| 35 | } |
||
| 36 | |||
| 37 | 2 | ksort($events); |
|
| 38 | |||
| 39 | 2 | Counter::reset(); |
|
| 40 | 2 | $this->eventsAggregator->aggregate(...$events); |
|
| 41 | 2 | } |
|
| 42 | |||
| 43 | /** |
||
| 44 | * @return EventAware[] |
||
| 45 | */ |
||
| 46 | 2 | private static function getEventAwareEntities(UnitOfWork $unitOfWork): array |
|
| 47 | { |
||
| 48 | 2 | $entities = array_merge( |
|
| 49 | 2 | $unitOfWork->getScheduledEntityInsertions(), |
|
| 50 | 2 | $unitOfWork->getScheduledEntityUpdates(), |
|
| 51 | 2 | $unitOfWork->getScheduledEntityDeletions() |
|
| 52 | ); |
||
| 53 | |||
| 54 | return array_filter($entities, static fn ($entity): bool => $entity instanceof EventAware); |
||
| 55 | 2 | } |
|
| 56 | } |
||
| 57 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.