|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Gember\RdbmsEventStoreDoctrineDbal\Saga; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Connection; |
| 8 | +use Gember\DependencyContracts\EventStore\Saga\RdbmsSaga; |
| 9 | +use Gember\DependencyContracts\EventStore\Saga\RdbmsSagaStoreRepository; |
| 10 | +use Gember\DependencyContracts\EventStore\Saga\RdbmsSagaNotFoundException; |
| 11 | +use Gember\RdbmsEventStoreDoctrineDbal\Saga\TableSchema\SagaStoreTableSchema; |
| 12 | +use Override; |
| 13 | +use Stringable; |
| 14 | +use DateTimeImmutable; |
| 15 | + |
| 16 | +/** |
| 17 | + * @phpstan-type SagaRow array{ |
| 18 | + * sagaId: string, |
| 19 | + * sagaName: string, |
| 20 | + * payload: string, |
| 21 | + * createdAt: string, |
| 22 | + * updatedAt: null|string |
| 23 | + * } |
| 24 | + */ |
| 25 | +final readonly class DoctrineRdbmsSagaStoreRepository implements RdbmsSagaStoreRepository |
| 26 | +{ |
| 27 | + public function __construct( |
| 28 | + private Connection $connection, |
| 29 | + private SagaStoreTableSchema $sagaStoreTableSchema, |
| 30 | + private DoctrineDbalRdbmsSagaFactory $sagaFactory, |
| 31 | + ) {} |
| 32 | + |
| 33 | + #[Override] |
| 34 | + public function get(string $sagaName, Stringable|string $sagaId): RdbmsSaga |
| 35 | + { |
| 36 | + $sagaStoreSchema = $this->sagaStoreTableSchema; |
| 37 | + |
| 38 | + /** @var false|SagaRow $row */ |
| 39 | + $row = $this->connection->createQueryBuilder() |
| 40 | + ->select( |
| 41 | + <<<DQL |
| 42 | + ss.{$sagaStoreSchema->sagaIdFieldName} as sagaId, |
| 43 | + ss.{$sagaStoreSchema->sagaNameFieldName} as sagaName, |
| 44 | + ss.{$sagaStoreSchema->payloadFieldName} as payload, |
| 45 | + ss.{$sagaStoreSchema->createdAtFieldName} as createdAt, |
| 46 | + ss.{$sagaStoreSchema->updatedAtFieldName} as updatedAt |
| 47 | + DQL |
| 48 | + ) |
| 49 | + ->from($sagaStoreSchema->tableName, 'ss') |
| 50 | + ->where(sprintf('ss.%s = :sagaId', $sagaStoreSchema->sagaIdFieldName)) |
| 51 | + ->andWhere(sprintf('ss.%s = :sagaName', $sagaStoreSchema->sagaNameFieldName)) |
| 52 | + ->setParameter('sagaId', (string) $sagaId) |
| 53 | + ->setParameter('sagaName', $sagaName) |
| 54 | + ->executeQuery() |
| 55 | + ->fetchAssociative(); |
| 56 | + |
| 57 | + if (!$row) { |
| 58 | + throw RdbmsSagaNotFoundException::withSagaId($sagaName, $sagaId); |
| 59 | + } |
| 60 | + |
| 61 | + return $this->sagaFactory->createFromRow($row); |
| 62 | + } |
| 63 | + |
| 64 | + #[Override] |
| 65 | + public function save( |
| 66 | + string $sagaName, |
| 67 | + Stringable|string $sagaId, |
| 68 | + string $payload, |
| 69 | + DateTimeImmutable $now, |
| 70 | + ): RdbmsSaga { |
| 71 | + $sagaStoreSchema = $this->sagaStoreTableSchema; |
| 72 | + |
| 73 | + try { |
| 74 | + $previous = $this->get($sagaName, $sagaId); |
| 75 | + } catch (RdbmsSagaNotFoundException) { |
| 76 | + $this->connection->createQueryBuilder() |
| 77 | + ->insert($sagaStoreSchema->tableName) |
| 78 | + ->setValue($sagaStoreSchema->sagaIdFieldName, ':sagaId') |
| 79 | + ->setValue($sagaStoreSchema->sagaNameFieldName, ':sagaName') |
| 80 | + ->setValue($sagaStoreSchema->payloadFieldName, ':payload') |
| 81 | + ->setValue($sagaStoreSchema->createdAtFieldName, ':createdAt') |
| 82 | + ->setParameters([ |
| 83 | + 'sagaId' => $sagaId, |
| 84 | + 'sagaName' => $sagaName, |
| 85 | + 'payload' => $payload, |
| 86 | + 'createdAt' => $now->format($sagaStoreSchema->createdAtFieldFormat), |
| 87 | + ]) |
| 88 | + ->executeStatement(); |
| 89 | + |
| 90 | + return new RdbmsSaga( |
| 91 | + $sagaName, |
| 92 | + $sagaId, |
| 93 | + $payload, |
| 94 | + $now, |
| 95 | + null, |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + $this->connection->createQueryBuilder() |
| 100 | + ->update($sagaStoreSchema->tableName) |
| 101 | + ->where(sprintf('%s = :sagaId', $sagaStoreSchema->sagaIdFieldName)) |
| 102 | + ->andWhere(sprintf('%s = :sagaName', $sagaStoreSchema->sagaNameFieldName)) |
| 103 | + ->set($sagaStoreSchema->payloadFieldName, ':payload') |
| 104 | + ->set($sagaStoreSchema->updatedAtFieldName, ':updatedAt') |
| 105 | + ->setParameters([ |
| 106 | + 'sagaId' => $sagaId, |
| 107 | + 'sagaName' => $sagaName, |
| 108 | + 'payload' => $payload, |
| 109 | + 'updatedAt' => $now->format($sagaStoreSchema->updatedAtFieldFormat), |
| 110 | + ]) |
| 111 | + ->executeStatement(); |
| 112 | + |
| 113 | + return new RdbmsSaga( |
| 114 | + $sagaName, |
| 115 | + $sagaId, |
| 116 | + $payload, |
| 117 | + $previous->createdAt, |
| 118 | + $now, |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments