forked from neos/neos-development-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoctrineSubscriptionStore.php
182 lines (169 loc) · 6.53 KB
/
DoctrineSubscriptionStore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
declare(strict_types=1);
namespace Neos\ContentRepositoryRegistry\Factory\SubscriptionStore;
use DateTimeImmutable;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Neos\ContentRepository\Core\Infrastructure\DbalSchemaDiff;
use Neos\ContentRepository\Core\Subscription\Store\SubscriptionCriteria;
use Neos\ContentRepository\Core\Subscription\Store\SubscriptionStoreInterface;
use Neos\ContentRepository\Core\Subscription\Subscription;
use Neos\ContentRepository\Core\Subscription\SubscriptionError;
use Neos\ContentRepository\Core\Subscription\SubscriptionId;
use Neos\ContentRepository\Core\Subscription\Subscriptions;
use Neos\ContentRepository\Core\Subscription\SubscriptionStatus;
use Neos\EventStore\Model\Event\SequenceNumber;
use Psr\Clock\ClockInterface;
use Neos\Flow\Annotations as Flow;
/**
* @Flow\Proxy(false)
*/
final class DoctrineSubscriptionStore implements SubscriptionStoreInterface
{
public function __construct(
private string $tableName,
private readonly Connection $dbal,
private readonly ClockInterface $clock,
) {
}
public function setup(): void
{
$schemaConfig = $this->dbal->createSchemaManager()->createSchemaConfig();
$schemaConfig->setDefaultTableOptions([
'charset' => 'utf8mb4'
]);
$tableSchema = new Table($this->tableName, [
(new Column('id', Type::getType(Types::STRING)))->setNotnull(true)->setLength(SubscriptionId::MAX_LENGTH),
(new Column('position', Type::getType(Types::INTEGER)))->setNotnull(true),
(new Column('status', Type::getType(Types::STRING)))->setNotnull(true)->setLength(32),
(new Column('error_message', Type::getType(Types::TEXT)))->setNotnull(false),
(new Column('error_previous_status', Type::getType(Types::STRING)))->setNotnull(false)->setLength(32),
(new Column('error_trace', Type::getType(Types::TEXT)))->setNotnull(false),
(new Column('last_saved_at', Type::getType(Types::DATETIME_IMMUTABLE)))->setNotnull(true),
]);
$tableSchema->setPrimaryKey(['id']);
$tableSchema->addIndex(['status']);
$schema = new Schema(
[$tableSchema],
[],
$schemaConfig,
);
foreach (DbalSchemaDiff::determineRequiredSqlStatements($this->dbal, $schema) as $statement) {
$this->dbal->executeStatement($statement);
}
}
public function findByCriteriaForUpdate(SubscriptionCriteria $criteria): Subscriptions
{
$queryBuilder = $this->dbal->createQueryBuilder()
->select('*')
->from($this->tableName)
->orderBy('id');
if (!$this->dbal->getDatabasePlatform() instanceof SqlitePlatform) {
$queryBuilder->forUpdate();
}
if ($criteria->ids !== null) {
$queryBuilder->andWhere('id IN (:ids)')
->setParameter(
'ids',
$criteria->ids->toStringArray(),
ArrayParameterType::STRING,
);
}
if (!$criteria->status->isEmpty()) {
$queryBuilder->andWhere('status IN (:status)')
->setParameter(
'status',
$criteria->status->toStringArray(),
ArrayParameterType::STRING,
);
}
$result = $queryBuilder->executeQuery();
assert($result instanceof Result);
$rows = $result->fetchAllAssociative();
if ($rows === []) {
return Subscriptions::createEmpty();
}
return Subscriptions::fromArray(array_map(self::fromDatabase(...), $rows));
}
public function add(Subscription $subscription): void
{
$row = self::toDatabase($subscription);
$row['id'] = $subscription->id->value;
$row['last_saved_at'] = $this->clock->now()->format('Y-m-d H:i:s');
$this->dbal->insert(
$this->tableName,
$row,
);
}
public function update(
SubscriptionId $subscriptionId,
SubscriptionStatus $status,
SequenceNumber $position,
SubscriptionError|null $subscriptionError,
): void {
$row = [];
$row['last_saved_at'] = $this->clock->now()->format('Y-m-d H:i:s');
$row['status'] = $status->value;
$row['position'] = $position->value;
$row['error_message'] = $subscriptionError?->errorMessage;
$row['error_previous_status'] = $subscriptionError?->previousStatus?->value;
$row['error_trace'] = $subscriptionError?->errorTrace;
$this->dbal->update(
$this->tableName,
$row,
[
'id' => $subscriptionId->value,
]
);
}
/**
* @return array<string, mixed>
*/
private static function toDatabase(Subscription $subscription): array
{
return [
'status' => $subscription->status->value,
'position' => $subscription->position->value,
'error_message' => $subscription->error?->errorMessage,
'error_previous_status' => $subscription->error?->previousStatus?->value,
'error_trace' => $subscription->error?->errorTrace,
];
}
/**
* @param array<string, mixed> $row
*/
private static function fromDatabase(array $row): Subscription
{
if (isset($row['error_message'])) {
$subscriptionError = new SubscriptionError($row['error_message'], SubscriptionStatus::from($row['error_previous_status']), $row['error_trace']);
} else {
$subscriptionError = null;
}
$lastSavedAt = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $row['last_saved_at']);
if ($lastSavedAt === false) {
throw new \RuntimeException(sprintf('last_saved_at %s is not a valid date', $row['last_saved_at']), 1733602968);
}
return new Subscription(
SubscriptionId::fromString($row['id']),
SubscriptionStatus::from($row['status']),
SequenceNumber::fromInteger($row['position']),
$subscriptionError,
$lastSavedAt,
);
}
public function beginTransaction(): void
{
$this->dbal->beginTransaction();
}
public function commit(): void
{
$this->dbal->commit();
}
}