@@ -163,12 +163,12 @@ use Patchlevel\EventSourcing\Attribute\Projector;
163163use Patchlevel\EventSourcing\Attribute\Setup;
164164use Patchlevel\EventSourcing\Attribute\Subscribe;
165165use Patchlevel\EventSourcing\Attribute\Teardown;
166- use Patchlevel\EventSourcing\Subscription\Subscriber\SubscriberUtil;
167166
168- #[Projector('hotel' )]
167+ #[Projector(self::TABLE )]
169168final 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```
0 commit comments