Skip to content

Commit f30cecf

Browse files
committed
Depreacte SubscriberHelper and SubscriberUtil and update docs to use a CONST instead
1 parent 9717148 commit f30cecf

4 files changed

Lines changed: 15 additions & 27 deletions

File tree

docs/pages/getting_started.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ use Patchlevel\EventSourcing\Attribute\Projector;
163163
use Patchlevel\EventSourcing\Attribute\Setup;
164164
use Patchlevel\EventSourcing\Attribute\Subscribe;
165165
use Patchlevel\EventSourcing\Attribute\Teardown;
166-
use Patchlevel\EventSourcing\Subscription\Subscriber\SubscriberUtil;
167166

168-
#[Projector('hotel')]
167+
#[Projector(self::TABLE)]
169168
final class HotelProjector
170169
{
171-
use SubscriberUtil;
170+
// use a const for easier access in the projector & to keep projector id and table name in sync
171+
private const TABLE = 'hotel';
172172

173173
public function __construct(
174174
private readonly Connection $db,
@@ -178,14 +178,14 @@ final class HotelProjector
178178
/** @return list<array{id: string, name: string, guests: int}> */
179179
public function getHotels(): array
180180
{
181-
return $this->db->fetchAllAssociative("SELECT id, name, guests FROM {$this->table()};");
181+
return $this->db->fetchAllAssociative(sprintf("SELECT id, name, guests FROM %s;"), self::TABLE);
182182
}
183183

184184
#[Subscribe(HotelCreated::class)]
185185
public function handleHotelCreated(HotelCreated $event): void
186186
{
187187
$this->db->insert(
188-
$this->table(),
188+
self::TABLE,
189189
[
190190
'id' => $event->hotelId->toString(),
191191
'name' => $event->hotelName,
@@ -198,7 +198,7 @@ final class HotelProjector
198198
public function handleGuestIsCheckedIn(GuestIsCheckedIn $event): void
199199
{
200200
$this->db->executeStatement(
201-
"UPDATE {$this->table()} SET guests = guests + 1 WHERE id = ?;",
201+
sprintf("UPDATE %s SET guests = guests + 1 WHERE id = ?;", self::TABLE),
202202
[$event->hotelId->toString()],
203203
);
204204
}
@@ -207,26 +207,21 @@ final class HotelProjector
207207
public function handleGuestIsCheckedOut(GuestIsCheckedOut $event): void
208208
{
209209
$this->db->executeStatement(
210-
"UPDATE {$this->table()} SET guests = guests - 1 WHERE id = ?;",
210+
sprintf("UPDATE %s SET guests = guests - 1 WHERE id = ?;", self::TABLE),
211211
[$event->hotelId->toString()],
212212
);
213213
}
214214

215215
#[Setup]
216216
public function create(): void
217217
{
218-
$this->db->executeStatement("CREATE TABLE IF NOT EXISTS {$this->table()} (id VARCHAR PRIMARY KEY, name VARCHAR, guests INTEGER);");
218+
$this->db->executeStatement(sprintf("CREATE TABLE IF NOT EXISTS %s (id VARCHAR PRIMARY KEY, name VARCHAR, guests INTEGER);", self::TABLE));
219219
}
220220

221221
#[Teardown]
222222
public function drop(): void
223223
{
224-
$this->db->executeStatement("DROP TABLE IF EXISTS {$this->table()};");
225-
}
226-
227-
private function table(): string
228-
{
229-
return 'projection_' . $this->subscriberId();
224+
$this->db->executeStatement(sprintf("DROP TABLE IF EXISTS %s;", self::TABLE));
230225
}
231226
}
232227
```

docs/pages/subscription.md

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,6 @@ use Patchlevel\EventSourcing\Subscription\Lookup;
231231
#[Projector('public_profile')]
232232
final class PublicProfileProjection
233233
{
234-
use SubscriberUtil;
235-
236234
// ... constructor
237235

238236
#[Subscribe(Published::class)]
@@ -306,32 +304,26 @@ use Doctrine\DBAL\Connection;
306304
use Patchlevel\EventSourcing\Attribute\Projector;
307305
use Patchlevel\EventSourcing\Attribute\Setup;
308306
use Patchlevel\EventSourcing\Attribute\Teardown;
309-
use Patchlevel\EventSourcing\Subscription\Subscriber\SubscriberUtil;
310307

311-
#[Projector('profile_1')]
308+
#[Projector(self::TABLE)]
312309
final class ProfileProjector
313310
{
314-
use SubscriberUtil;
311+
private const TABLE = 'profile_v1';
315312

316313
private Connection $connection;
317314

318315
#[Setup]
319316
public function create(): void
320317
{
321318
$this->connection->executeStatement(
322-
"CREATE TABLE IF NOT EXISTS {$this->table()} (id VARCHAR PRIMARY KEY, name VARCHAR NOT NULL);",
319+
sprintf("CREATE TABLE IF NOT EXISTS %s (id VARCHAR PRIMARY KEY, name VARCHAR NOT NULL);", self::TABLE),
323320
);
324321
}
325322

326323
#[Teardown]
327324
public function drop(): void
328325
{
329-
$this->connection->executeStatement("DROP TABLE IF EXISTS {$this->table()};");
330-
}
331-
332-
private function table(): string
333-
{
334-
return 'projection_' . $this->subscriberId();
326+
$this->connection->executeStatement(sprintf("DROP TABLE IF EXISTS %s;", self::TABLE));
335327
}
336328
}
337329
```
@@ -346,7 +338,6 @@ final class ProfileProjector
346338
If you change the subscriber id, you must also change the table/collection name.
347339
The subscription engine will create a new subscription with the new subscriber id.
348340
That means the setup method will be called again and the table/collection will conflict with the old existing projection.
349-
You can use the `SubscriberUtil` to build the table/collection name.
350341

351342
!!! note
352343

src/Subscription/Subscriber/SubscriberHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Patchlevel\EventSourcing\Metadata\Subscriber\SubscriberMetadata;
99
use Patchlevel\EventSourcing\Metadata\Subscriber\SubscriberMetadataFactory;
1010

11+
/** @deprecated since 3.15.0 will be removed with 4.0.0 */
1112
final class SubscriberHelper
1213
{
1314
public function __construct(

src/Subscription/Subscriber/SubscriberUtil.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Patchlevel\EventSourcing\Metadata\Subscriber\AttributeSubscriberMetadataFactory;
88
use Patchlevel\EventSourcing\Metadata\Subscriber\SubscriberMetadataFactory;
99

10+
/** @deprecated since 3.15.0 will be removed with 4.0.0 */
1011
trait SubscriberUtil
1112
{
1213
private static SubscriberMetadataFactory|null $metadataFactory = null;

0 commit comments

Comments
 (0)