|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Gember\EventStoreDoctrineDbal\Repository; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Connection; |
| 8 | +use Doctrine\DBAL\Exception; |
| 9 | +use Gember\EventSourcing\AggregateRoot\AggregateRootId; |
| 10 | +use Gember\EventSourcing\EventNameResolver\EventNameResolver; |
| 11 | +use Gember\EventSourcing\EventNameResolver\UnresolvableEventNameException; |
| 12 | +use Gember\EventSourcing\EventStore\EventEnvelope; |
| 13 | +use Gember\EventSourcing\Util\Serialization\Serializer\SerializationFailedException; |
| 14 | +use Gember\EventSourcing\Util\Serialization\Serializer\Serializer; |
| 15 | +use Gember\EventStoreDoctrineDbal\TableSchema\TableSchema; |
| 16 | +use JsonException; |
| 17 | +use Throwable; |
| 18 | + |
| 19 | +/** |
| 20 | + * @phpstan-import-type RowPayload from EventStoreRepository |
| 21 | + */ |
| 22 | +final readonly class DoctrineDbalEventStoreRepository implements EventStoreRepository |
| 23 | +{ |
| 24 | + public function __construct( |
| 25 | + private Connection $connection, |
| 26 | + private TableSchema $tableSchema, |
| 27 | + private EventNameResolver $eventNameResolver, |
| 28 | + private Serializer $serializer, |
| 29 | + ) {} |
| 30 | + |
| 31 | + /** |
| 32 | + * @throws Exception |
| 33 | + */ |
| 34 | + public function getRows(AggregateRootId $aggregateRootId, int $playhead): array |
| 35 | + { |
| 36 | + $schema = $this->tableSchema; |
| 37 | + |
| 38 | + /** @var list<RowPayload> */ |
| 39 | + return $this->connection->createQueryBuilder() |
| 40 | + ->select( |
| 41 | + <<<DQL |
| 42 | + {$schema->eventIdFieldName} as eventId, |
| 43 | + {$schema->payloadFieldName} as payload, |
| 44 | + {$schema->eventNameFieldName} as eventName, |
| 45 | + {$schema->playheadFieldName} as playhead, |
| 46 | + {$schema->appliedAtFieldName} as appliedAt, |
| 47 | + {$schema->metadataFieldName} as metadata |
| 48 | + DQL |
| 49 | + ) |
| 50 | + ->from($schema->tableName) |
| 51 | + ->where(sprintf('%s >= :playhead', $schema->payloadFieldName)) |
| 52 | + ->andWhere(sprintf('%s = :aggregateRootId', $schema->aggregateRootIdFieldName)) |
| 53 | + ->setParameters([ |
| 54 | + 'playhead' => $playhead, |
| 55 | + 'aggregateRootId' => $aggregateRootId, |
| 56 | + ]) |
| 57 | + ->orderBy($schema->appliedAtFieldName, 'desc') |
| 58 | + ->executeQuery() |
| 59 | + ->fetchAllAssociative(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @throws Exception |
| 64 | + * @throws Throwable |
| 65 | + * @throws UnresolvableEventNameException |
| 66 | + * @throws SerializationFailedException |
| 67 | + * @throws JsonException |
| 68 | + */ |
| 69 | + public function saveRows(AggregateRootId $aggregateRootId, EventEnvelope ...$envelopes): void |
| 70 | + { |
| 71 | + $schema = $this->tableSchema; |
| 72 | + |
| 73 | + try { |
| 74 | + $this->connection->beginTransaction(); |
| 75 | + |
| 76 | + foreach ($envelopes as $envelope) { |
| 77 | + $this->connection->createQueryBuilder() |
| 78 | + ->insert($schema->tableName) |
| 79 | + ->setValue($schema->eventIdFieldName, ':id') |
| 80 | + ->setValue($schema->aggregateRootIdFieldName, ':aggregateRootId') |
| 81 | + ->setValue($schema->eventNameFieldName, ':eventName') |
| 82 | + ->setValue($schema->payloadFieldName, ':payload') |
| 83 | + ->setValue($schema->playheadFieldName, ':playhead') |
| 84 | + ->setValue($schema->metadataFieldName, ':metadata') |
| 85 | + ->setValue($schema->appliedAtFieldName, ':appliedAt') |
| 86 | + ->setParameters([ |
| 87 | + 'id' => $envelope->eventId, |
| 88 | + 'aggregateRootId' => (string) $aggregateRootId, |
| 89 | + 'eventName' => $this->eventNameResolver->resolve($envelope->event::class), |
| 90 | + 'payload' => $this->serializer->serialize($envelope->event), |
| 91 | + 'playhead' => $envelope->playhead, |
| 92 | + 'metadata' => json_encode($envelope->metadata->metadata, JSON_THROW_ON_ERROR), |
| 93 | + 'appliedAt' => $envelope->appliedAt->format($schema->appliedAtFieldFormat), |
| 94 | + ]) |
| 95 | + ->executeStatement(); |
| 96 | + } |
| 97 | + |
| 98 | + $this->connection->commit(); |
| 99 | + } catch (Throwable $exception) { |
| 100 | + $this->connection->rollBack(); |
| 101 | + |
| 102 | + throw $exception; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @throws Exception |
| 108 | + */ |
| 109 | + public function getLastPlayhead(AggregateRootId $aggregateRootId): ?int |
| 110 | + { |
| 111 | + $schema = $this->tableSchema; |
| 112 | + |
| 113 | + $row = $this->connection->createQueryBuilder() |
| 114 | + ->select($schema->playheadFieldName) |
| 115 | + ->andWhere(sprintf('%s = :aggregateRootId', $schema->aggregateRootIdFieldName)) |
| 116 | + ->setParameters([ |
| 117 | + 'aggregateRootId' => $aggregateRootId, |
| 118 | + ]) |
| 119 | + ->orderBy($schema->appliedAtFieldName, 'desc') |
| 120 | + ->setMaxResults(1) |
| 121 | + ->executeQuery() |
| 122 | + ->fetchAssociative(); |
| 123 | + |
| 124 | + if ($row === false) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + return (int) $row[$schema->playheadFieldName]; // @phpstan-ignore cast.int |
| 129 | + } |
| 130 | +} |
0 commit comments