|
4 | 4 |
|
5 | 5 | namespace Gember\RdbmsEventStoreDoctrineDbal\Saga; |
6 | 6 |
|
| 7 | +use Doctrine\DBAL\ArrayParameterType; |
7 | 8 | use Doctrine\DBAL\Connection; |
8 | 9 | use Gember\DependencyContracts\EventStore\Saga\RdbmsSaga; |
9 | 10 | use Gember\DependencyContracts\EventStore\Saga\RdbmsSagaStoreRepository; |
10 | 11 | use Gember\DependencyContracts\EventStore\Saga\RdbmsSagaNotFoundException; |
| 12 | +use Gember\DependencyContracts\Util\Generator\Identity\IdentityGenerator; |
| 13 | +use Gember\RdbmsEventStoreDoctrineDbal\Saga\TableSchema\SagaStoreRelationTableSchema; |
11 | 14 | use Gember\RdbmsEventStoreDoctrineDbal\Saga\TableSchema\SagaStoreTableSchema; |
12 | 15 | use Override; |
13 | 16 | use Stringable; |
14 | 17 | use DateTimeImmutable; |
| 18 | +use Throwable; |
15 | 19 |
|
16 | 20 | /** |
17 | 21 | * @phpstan-type SagaRow array{ |
18 | | - * sagaId: string, |
| 22 | + * id: string, |
| 23 | + * sagaIds: list<string>, |
19 | 24 | * sagaName: string, |
20 | 25 | * payload: string, |
21 | 26 | * createdAt: string, |
|
27 | 32 | public function __construct( |
28 | 33 | private Connection $connection, |
29 | 34 | private SagaStoreTableSchema $sagaStoreTableSchema, |
| 35 | + private SagaStoreRelationTableSchema $sagaStoreRelationTableSchema, |
30 | 36 | private DoctrineDbalRdbmsSagaFactory $sagaFactory, |
| 37 | + private IdentityGenerator $identityGenerator, |
31 | 38 | ) {} |
32 | 39 |
|
33 | 40 | #[Override] |
34 | | - public function get(string $sagaName, Stringable|string $sagaId): RdbmsSaga |
| 41 | + public function get(string $sagaName, Stringable|string ...$sagaIds): RdbmsSaga |
35 | 42 | { |
36 | 43 | $sagaStoreSchema = $this->sagaStoreTableSchema; |
| 44 | + $sagaStoreRelationSchema = $this->sagaStoreRelationTableSchema; |
37 | 45 |
|
38 | | - /** @var false|SagaRow $row */ |
| 46 | + /** @var list<array{ |
| 47 | + * id: string, |
| 48 | + * sagaId: string, |
| 49 | + * sagaName: string, |
| 50 | + * payload: string, |
| 51 | + * createdAt: string, |
| 52 | + * updatedAt: string|null |
| 53 | + * }> $row */ |
39 | 54 | $row = $this->connection->createQueryBuilder() |
40 | 55 | ->select( |
41 | 56 | <<<DQL |
42 | | - ss.{$sagaStoreSchema->sagaIdFieldName} as sagaId, |
| 57 | + ss.{$sagaStoreSchema->idFieldName} as id, |
43 | 58 | ss.{$sagaStoreSchema->sagaNameFieldName} as sagaName, |
44 | 59 | ss.{$sagaStoreSchema->payloadFieldName} as payload, |
45 | 60 | ss.{$sagaStoreSchema->createdAtFieldName} as createdAt, |
46 | 61 | ss.{$sagaStoreSchema->updatedAtFieldName} as updatedAt |
47 | 62 | DQL |
48 | 63 | ) |
49 | 64 | ->from($sagaStoreSchema->tableName, 'ss') |
50 | | - ->where(sprintf('ss.%s = :sagaId', $sagaStoreSchema->sagaIdFieldName)) |
| 65 | + ->join('ss', $sagaStoreRelationSchema->tableName, 'ssr', sprintf( |
| 66 | + 'ss.%s = ssr.%s', |
| 67 | + $sagaStoreSchema->idFieldName, |
| 68 | + $sagaStoreRelationSchema->idFieldName, |
| 69 | + )) |
| 70 | + ->where(sprintf('ssr.%s IN (:sagaIds)', $sagaStoreRelationSchema->sagaIdFieldName)) |
51 | 71 | ->andWhere(sprintf('ss.%s = :sagaName', $sagaStoreSchema->sagaNameFieldName)) |
52 | | - ->setParameter('sagaId', (string) $sagaId) |
| 72 | + ->setParameter( |
| 73 | + 'sagaIds', |
| 74 | + array_map(fn($sagaId) => (string) $sagaId, $sagaIds), |
| 75 | + ArrayParameterType::STRING, |
| 76 | + ) |
53 | 77 | ->setParameter('sagaName', $sagaName) |
| 78 | + ->setMaxResults(1) |
54 | 79 | ->executeQuery() |
55 | 80 | ->fetchAssociative(); |
56 | 81 |
|
57 | 82 | if (!$row) { |
58 | | - throw RdbmsSagaNotFoundException::withSagaId($sagaName, $sagaId); |
| 83 | + throw RdbmsSagaNotFoundException::create($sagaName, ...$sagaIds); |
59 | 84 | } |
60 | 85 |
|
61 | | - return $this->sagaFactory->createFromRow($row); |
| 86 | + $sagaIdRows = $this->connection->createQueryBuilder() |
| 87 | + ->select( |
| 88 | + <<<DQL |
| 89 | + ssr.{$sagaStoreRelationSchema->sagaIdFieldName} as sagaId |
| 90 | + DQL |
| 91 | + ) |
| 92 | + ->from($sagaStoreRelationSchema->tableName, 'ssr') |
| 93 | + ->executeQuery() |
| 94 | + ->fetchAllAssociative(); |
| 95 | + |
| 96 | + /** @var list<SagaRow> $payload */ |
| 97 | + $payload = $row; |
| 98 | + $payload['sagaIds'] = array_map(fn($sagaIdRow) => $sagaIdRow['sagaId'], $sagaIdRows); |
| 99 | + |
| 100 | + return $this->sagaFactory->createFromRow($payload); |
62 | 101 | } |
63 | 102 |
|
64 | 103 | #[Override] |
65 | 104 | public function save( |
66 | 105 | string $sagaName, |
67 | | - Stringable|string $sagaId, |
68 | 106 | string $payload, |
69 | 107 | DateTimeImmutable $now, |
| 108 | + Stringable|string ...$sagaIds, |
| 109 | + ): RdbmsSaga { |
| 110 | + try { |
| 111 | + $previous = $this->get($sagaName, ...$sagaIds); |
| 112 | + } catch (RdbmsSagaNotFoundException) { |
| 113 | + return $this->create($sagaName, $payload, $now, ...$sagaIds); |
| 114 | + } |
| 115 | + |
| 116 | + return $this->update($previous, $sagaName, $payload, $now, ...$sagaIds); |
| 117 | + } |
| 118 | + |
| 119 | + private function create( |
| 120 | + string $sagaName, |
| 121 | + string $payload, |
| 122 | + DateTimeImmutable $now, |
| 123 | + Stringable|string ...$sagaIds, |
70 | 124 | ): RdbmsSaga { |
| 125 | + $id = $this->identityGenerator->generate(); |
| 126 | + |
71 | 127 | $sagaStoreSchema = $this->sagaStoreTableSchema; |
| 128 | + $sagaStoreRelationSchema = $this->sagaStoreRelationTableSchema; |
| 129 | + |
| 130 | + $this->connection->beginTransaction(); |
72 | 131 |
|
73 | 132 | try { |
74 | | - $previous = $this->get($sagaName, $sagaId); |
75 | | - } catch (RdbmsSagaNotFoundException) { |
76 | 133 | $this->connection->createQueryBuilder() |
77 | 134 | ->insert($sagaStoreSchema->tableName) |
78 | | - ->setValue($sagaStoreSchema->sagaIdFieldName, ':sagaId') |
| 135 | + ->setValue($sagaStoreSchema->idFieldName, ':id') |
79 | 136 | ->setValue($sagaStoreSchema->sagaNameFieldName, ':sagaName') |
80 | 137 | ->setValue($sagaStoreSchema->payloadFieldName, ':payload') |
81 | 138 | ->setValue($sagaStoreSchema->createdAtFieldName, ':createdAt') |
82 | 139 | ->setParameters([ |
83 | | - 'sagaId' => $sagaId, |
| 140 | + 'id' => $id, |
84 | 141 | 'sagaName' => $sagaName, |
85 | 142 | 'payload' => $payload, |
86 | 143 | 'createdAt' => $now->format($sagaStoreSchema->createdAtFieldFormat), |
87 | 144 | ]) |
88 | 145 | ->executeStatement(); |
89 | 146 |
|
90 | | - return new RdbmsSaga( |
91 | | - $sagaName, |
92 | | - $sagaId, |
93 | | - $payload, |
94 | | - $now, |
95 | | - null, |
96 | | - ); |
| 147 | + foreach ($sagaIds as $sagaId) { |
| 148 | + $this->connection->createQueryBuilder() |
| 149 | + ->insert($sagaStoreRelationSchema->tableName) |
| 150 | + ->setValue($sagaStoreRelationSchema->idFieldName, ':id') |
| 151 | + ->setValue($sagaStoreRelationSchema->sagaIdFieldName, ':sagaId') |
| 152 | + ->setParameters([ |
| 153 | + 'id' => $id, |
| 154 | + 'sagaId' => $sagaId, |
| 155 | + ]) |
| 156 | + ->executeStatement(); |
| 157 | + } |
| 158 | + |
| 159 | + $this->connection->commit(); |
| 160 | + } catch (Throwable $exception) { |
| 161 | + $this->connection->rollBack(); |
| 162 | + |
| 163 | + throw $exception; |
97 | 164 | } |
98 | 165 |
|
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(); |
| 166 | + return new RdbmsSaga( |
| 167 | + $id, |
| 168 | + $sagaName, |
| 169 | + array_values($sagaIds), |
| 170 | + $payload, |
| 171 | + $now, |
| 172 | + null, |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + private function update( |
| 177 | + RdbmsSaga $previous, |
| 178 | + string $sagaName, |
| 179 | + string $payload, |
| 180 | + DateTimeImmutable $now, |
| 181 | + Stringable|string ...$sagaIds, |
| 182 | + ): RdbmsSaga { |
| 183 | + $sagaStoreSchema = $this->sagaStoreTableSchema; |
| 184 | + $sagaStoreRelationSchema = $this->sagaStoreRelationTableSchema; |
| 185 | + |
| 186 | + $this->connection->beginTransaction(); |
| 187 | + |
| 188 | + try { |
| 189 | + $this->connection->createQueryBuilder() |
| 190 | + ->update($sagaStoreSchema->tableName) |
| 191 | + ->where(sprintf('%s = :id', $sagaStoreSchema->idFieldName)) |
| 192 | + ->set($sagaStoreSchema->payloadFieldName, ':payload') |
| 193 | + ->set($sagaStoreSchema->updatedAtFieldName, ':updatedAt') |
| 194 | + ->setParameter('id', $previous->id) |
| 195 | + ->setParameter('payload', $payload) |
| 196 | + ->setParameter('updatedAt', $now->format($sagaStoreSchema->updatedAtFieldFormat)) |
| 197 | + ->executeStatement(); |
| 198 | + |
| 199 | + $this->connection->createQueryBuilder() |
| 200 | + ->delete($sagaStoreRelationSchema->tableName) |
| 201 | + ->where(sprintf('%s = :id', $sagaStoreRelationSchema->idFieldName)) |
| 202 | + ->setParameter('id', $previous->id) |
| 203 | + ->executeStatement(); |
| 204 | + |
| 205 | + foreach ($sagaIds as $sagaId) { |
| 206 | + $this->connection->createQueryBuilder() |
| 207 | + ->insert($sagaStoreRelationSchema->tableName) |
| 208 | + ->setValue($sagaStoreRelationSchema->idFieldName, ':id') |
| 209 | + ->setValue($sagaStoreRelationSchema->sagaIdFieldName, ':sagaId') |
| 210 | + ->setParameters([ |
| 211 | + 'id' => $previous->id, |
| 212 | + 'sagaId' => $sagaId, |
| 213 | + ]) |
| 214 | + ->executeStatement(); |
| 215 | + } |
| 216 | + |
| 217 | + $this->connection->commit(); |
| 218 | + } catch (Throwable $exception) { |
| 219 | + $this->connection->rollBack(); |
| 220 | + |
| 221 | + throw $exception; |
| 222 | + } |
112 | 223 |
|
113 | 224 | return new RdbmsSaga( |
| 225 | + $previous->id, |
114 | 226 | $sagaName, |
115 | | - $sagaId, |
| 227 | + array_values($sagaIds), |
116 | 228 | $payload, |
117 | 229 | $previous->createdAt, |
118 | 230 | $now, |
|
0 commit comments