Skip to content

Commit a04cfbe

Browse files
committed
Add new #[SubscriptionName] attribute to replace SubscritionUtil trait
1 parent ce3769a commit a04cfbe

16 files changed

Lines changed: 172 additions & 172 deletions

File tree

src/Attribute/Processor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
final class Processor extends Subscriber
1212
{
1313
public function __construct(
14-
string $id,
14+
string|null $id = null,
1515
string $group = 'processor',
1616
RunMode $runMode = RunMode::FromNow,
1717
) {

src/Attribute/Projector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
final class Projector extends Subscriber
1212
{
1313
public function __construct(
14-
string $id,
14+
string|null $id = null,
1515
string $group = 'projector',
1616
RunMode $runMode = RunMode::FromBeginning,
1717
) {

src/Attribute/Subscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class Subscriber
1212
{
1313
public function __construct(
14-
public readonly string $id,
14+
public readonly string|null $id = null,
1515
public readonly RunMode $runMode,
1616
public readonly string $group = 'default',
1717
) {

src/Attribute/SubscriptionName.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\Attribute;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
10+
final class SubscriptionName
11+
{
12+
}

src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Patchlevel\EventSourcing\Attribute\Setup;
1010
use Patchlevel\EventSourcing\Attribute\Subscribe;
1111
use Patchlevel\EventSourcing\Attribute\Subscriber;
12+
use Patchlevel\EventSourcing\Attribute\SubscriptionName;
1213
use Patchlevel\EventSourcing\Attribute\Teardown;
1314
use ReflectionAttribute;
1415
use ReflectionClass;
@@ -109,8 +110,23 @@ public function metadata(string $subscriber): SubscriberMetadata
109110
throw DuplicateSubscribeMethod::mixedWithAll($subscriber);
110111
}
111112

113+
$subscriptionId = $subscriberInfo->id;
114+
115+
if ($subscriptionId === null) {
116+
foreach ($reflector->getReflectionConstants() as $constant) {
117+
if ($constant->getAttributes(SubscriptionName::class)) {
118+
$subscriptionId = $constant->getValue();
119+
break;
120+
}
121+
}
122+
}
123+
124+
if ($subscriptionId === null) {
125+
throw new SubscriptionIdMissing($subscriber);
126+
}
127+
112128
$metadata = new SubscriberMetadata(
113-
$subscriberInfo->id,
129+
$subscriptionId,
114130
$subscriberInfo->group,
115131
$subscriberInfo->runMode,
116132
$subscribeMethods,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\Metadata\Subscriber;
6+
7+
use Patchlevel\EventSourcing\Metadata\MetadataException;
8+
9+
use function sprintf;
10+
11+
final class SubscriptionIdMissing extends MetadataException
12+
{
13+
/** @param class-string $subscriber */
14+
public function __construct(string $subscriberClass)
15+
{
16+
parent::__construct(
17+
sprintf(
18+
'There is no subscription id defined for the subscriber "%s". Define it via #[Subscriber] or #[SubscriptionName] attribute.',
19+
$subscriberClass,
20+
),
21+
);
22+
}
23+
}

src/Subscription/Subscriber/SubscriberHelper.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Subscription/Subscriber/SubscriberUtil.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/Benchmark/BasicImplementation/Projection/BatchProfileProjector.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
use Patchlevel\EventSourcing\Attribute\Projector;
99
use Patchlevel\EventSourcing\Attribute\Setup;
1010
use Patchlevel\EventSourcing\Attribute\Subscribe;
11+
use Patchlevel\EventSourcing\Attribute\SubscriptionName;
1112
use Patchlevel\EventSourcing\Attribute\Teardown;
1213
use Patchlevel\EventSourcing\Subscription\Subscriber\BatchableSubscriber;
13-
use Patchlevel\EventSourcing\Subscription\Subscriber\SubscriberUtil;
1414
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\NameChanged;
1515
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\ProfileCreated;
1616

17-
#[Projector('profile')]
17+
#[Projector]
1818
final class BatchProfileProjector implements BatchableSubscriber
1919
{
20-
use SubscriberUtil;
20+
#[SubscriptionName]
21+
private const TABLE_NAME = 'projection_profile';
2122

2223
/** @var array<string, string> */
2324
private array $nameChanged = [];
@@ -30,20 +31,23 @@ public function __construct(
3031
#[Setup]
3132
public function create(): void
3233
{
33-
$this->connection->executeStatement("CREATE TABLE IF NOT EXISTS {$this->table()} (id VARCHAR PRIMARY KEY, name VARCHAR);");
34+
$this->connection->executeStatement(sprintf(
35+
"CREATE TABLE IF NOT EXISTS %s (id VARCHAR PRIMARY KEY, name VARCHAR);",
36+
self::TABLE_NAME,
37+
));
3438
}
3539

3640
#[Teardown]
3741
public function drop(): void
3842
{
39-
$this->connection->executeStatement("DROP TABLE IF EXISTS {$this->table()};");
43+
$this->connection->executeStatement(sprintf("DROP TABLE IF EXISTS %s;", self::TABLE_NAME));
4044
}
4145

4246
#[Subscribe(ProfileCreated::class)]
4347
public function onProfileCreated(ProfileCreated $profileCreated): void
4448
{
4549
$this->connection->insert(
46-
$this->table(),
50+
self::TABLE_NAME,
4751
[
4852
'id' => $profileCreated->profileId->toString(),
4953
'name' => $profileCreated->name,
@@ -73,7 +77,7 @@ public function commitBatch(): void
7377
$this->connection->transactional(function (): void {
7478
foreach ($this->nameChanged as $profileId => $name) {
7579
$this->connection->update(
76-
$this->table(),
80+
self::TABLE_NAME,
7781
['name' => $name],
7882
['id' => $profileId],
7983
);

tests/Benchmark/BasicImplementation/Projection/ProfileProjector.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
use Patchlevel\EventSourcing\Attribute\Projector;
99
use Patchlevel\EventSourcing\Attribute\Setup;
1010
use Patchlevel\EventSourcing\Attribute\Subscribe;
11+
use Patchlevel\EventSourcing\Attribute\SubscriptionName;
1112
use Patchlevel\EventSourcing\Attribute\Teardown;
12-
use Patchlevel\EventSourcing\Subscription\Subscriber\SubscriberUtil;
1313
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\NameChanged;
1414
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\ProfileCreated;
1515

1616
#[Projector('profile')]
1717
final class ProfileProjector
1818
{
19-
use SubscriberUtil;
19+
#[SubscriptionName]
20+
private const TABLE_NAME = 'projection_profile';
2021

2122
public function __construct(
2223
private Connection $connection,
@@ -26,20 +27,23 @@ public function __construct(
2627
#[Setup]
2728
public function create(): void
2829
{
29-
$this->connection->executeStatement("CREATE TABLE IF NOT EXISTS {$this->table()} (id VARCHAR PRIMARY KEY, name VARCHAR);");
30+
$this->connection->executeStatement(sprintf(
31+
"CREATE TABLE IF NOT EXISTS %s (id VARCHAR PRIMARY KEY, name VARCHAR);",
32+
self::TABLE_NAME,
33+
));
3034
}
3135

3236
#[Teardown]
3337
public function drop(): void
3438
{
35-
$this->connection->executeStatement("DROP TABLE IF EXISTS {$this->table()};");
39+
$this->connection->executeStatement(sprintf("DROP TABLE IF EXISTS %s;", self::TABLE_NAME));
3640
}
3741

3842
#[Subscribe(ProfileCreated::class)]
3943
public function onProfileCreated(ProfileCreated $profileCreated): void
4044
{
4145
$this->connection->insert(
42-
$this->table(),
46+
self::TABLE_NAME,
4347
[
4448
'id' => $profileCreated->profileId->toString(),
4549
'name' => $profileCreated->name,
@@ -51,14 +55,9 @@ public function onProfileCreated(ProfileCreated $profileCreated): void
5155
public function onNameChanged(NameChanged $nameChanged): void
5256
{
5357
$this->connection->update(
54-
$this->table(),
58+
self::TABLE_NAME,
5559
['name' => $nameChanged->name],
5660
['id' => $nameChanged->profileId->toString()],
5761
);
5862
}
59-
60-
public function table(): string
61-
{
62-
return 'projection_' . $this->subscriberId();
63-
}
6463
}

0 commit comments

Comments
 (0)