Skip to content

Commit

Permalink
add projector and processor attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Mar 8, 2024
1 parent 29833b0 commit 1a24eb4
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 159 deletions.
20 changes: 20 additions & 0 deletions src/Attribute/Processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Attribute;

use Attribute;
use Patchlevel\EventSourcing\Subscription\RunMode;

#[Attribute(Attribute::TARGET_CLASS)]
final class Processor extends Subscriber
{
public function __construct(
string $id,
string $group = 'processor',
RunMode $runMode = RunMode::FromNow,
) {
parent::__construct($id, $group, $runMode);
}
}
20 changes: 20 additions & 0 deletions src/Attribute/Projector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Attribute;

use Attribute;
use Patchlevel\EventSourcing\Subscription\RunMode;

#[Attribute(Attribute::TARGET_CLASS)]
final class Projector extends Subscriber
{
public function __construct(
string $id,
string $group = 'projector',
RunMode $runMode = RunMode::FromBeginning,
) {
parent::__construct($id, $group, $runMode);
}
}
9 changes: 5 additions & 4 deletions src/Attribute/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
namespace Patchlevel\EventSourcing\Attribute;

use Attribute;
use Cspray\Phinal\AllowInheritance;
use Patchlevel\EventSourcing\Subscription\RunMode;
use Patchlevel\EventSourcing\Subscription\Subscription;

#[Attribute(Attribute::TARGET_CLASS)]
final class Subscriber
#[AllowInheritance('You can create specific attributes with default group and run mode')]
class Subscriber
{
public function __construct(
public readonly string $id,
public readonly string $group = Subscription::DEFAULT_GROUP,
public readonly RunMode $runMode = RunMode::FromBeginning,
public readonly string $group,
public readonly RunMode $runMode,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Attribute\Teardown;
use ReflectionAttribute;
use ReflectionClass;

use function array_key_exists;
Expand All @@ -26,7 +27,7 @@ public function metadata(string $subscriber): SubscriberMetadata

$reflector = new ReflectionClass($subscriber);

$attributes = $reflector->getAttributes(Subscriber::class);
$attributes = $reflector->getAttributes(Subscriber::class, ReflectionAttribute::IS_INSTANCEOF);

if ($attributes === []) {
throw new ClassIsNotASubscriber($subscriber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Processor;

use Patchlevel\EventSourcing\Attribute\Processor;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Events\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\SendEmailMock;

#[Subscriber('send_email')]
#[Processor('send_email')]
final class SendEmailProcessor
{
#[Subscribe(ProfileCreated::class)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\Aggregate\AggregateHeader;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Subscription\Subscriber\SubscriberUtil;
Expand All @@ -17,7 +17,7 @@

use function assert;

#[Subscriber('profile')]
#[Projector('profile')]
final class ProfileProjector
{
use SubscriberUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Table;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BalanceAdded;
use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BankAccountCreated;

use function assert;

#[Subscriber('dummy-1')]
#[Projector('dummy-1')]
final class BankAccountProjector
{
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Table;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Events\ProfileCreated;

use function assert;

#[Subscriber('profile-1')]
#[Projector('profile-1')]
final class ProfileProjector
{
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Subscription\RunMode;
use RuntimeException;

#[Subscriber('error_producer')]
#[Subscriber('error_producer', 'default', RunMode::FromBeginning)]
final class ErrorProducerSubscriber
{
public bool $setupError = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

namespace Patchlevel\EventSourcing\Tests\Integration\Subscription\Subscriber;

use Patchlevel\EventSourcing\Attribute\Processor;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Repository\RepositoryManager;
use Patchlevel\EventSourcing\Tests\Integration\Subscription\Aggregate\Profile;
use Patchlevel\EventSourcing\Tests\Integration\Subscription\Events\ProfileCreated;

use function assert;

#[Subscriber('profile')]
#[Processor('profile')]
final class ProfileProcessor
{
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Table;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Subscription\Subscriber\SubscriberUtil;
use Patchlevel\EventSourcing\Tests\Integration\Subscription\Events\ProfileCreated;

use function assert;

#[Subscriber('profile_1')]
#[Projector('profile_1')]
final class ProfileProjection
{
use SubscriberUtil;
Expand Down
29 changes: 21 additions & 8 deletions tests/Integration/Subscription/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ public function testHappyPath(): void
);

self::assertEquals(
[new Subscription('profile_1', lastSavedAt: new DateTimeImmutable('2021-01-01T00:00:00'))],
[
new Subscription(
'profile_1',
'projector',
lastSavedAt: new DateTimeImmutable('2021-01-01T00:00:00'),
),
],
$engine->subscriptions(),
);

Expand All @@ -109,7 +115,7 @@ public function testHappyPath(): void
[
new Subscription(
'profile_1',
Subscription::DEFAULT_GROUP,
'projector',
RunMode::FromBeginning,
Status::Active,
lastSavedAt: new DateTimeImmutable('2021-01-01T00:00:00'),
Expand All @@ -127,7 +133,7 @@ public function testHappyPath(): void
[
new Subscription(
'profile_1',
Subscription::DEFAULT_GROUP,
'projector',
RunMode::FromBeginning,
Status::Active,
1,
Expand All @@ -153,7 +159,7 @@ public function testHappyPath(): void
[
new Subscription(
'profile_1',
Subscription::DEFAULT_GROUP,
'projector',
RunMode::FromBeginning,
Status::New,
lastSavedAt: new DateTimeImmutable('2021-01-01T00:00:00'),
Expand Down Expand Up @@ -354,7 +360,14 @@ public function testProcessor(): void
);

self::assertEquals(
[new Subscription('profile', lastSavedAt: new DateTimeImmutable('2021-01-01T00:00:00'))],
[
new Subscription(
'profile',
'processor',
RunMode::FromNow,
lastSavedAt: new DateTimeImmutable('2021-01-01T00:00:00'),
),
],
$engine->subscriptions(),
);

Expand All @@ -364,8 +377,8 @@ public function testProcessor(): void
[
new Subscription(
'profile',
Subscription::DEFAULT_GROUP,
RunMode::FromBeginning,
'processor',
RunMode::FromNow,
Status::Active,
lastSavedAt: new DateTimeImmutable('2021-01-01T00:00:00'),
),
Expand Down Expand Up @@ -399,7 +412,7 @@ public function testProcessor(): void
new TraceHeader([
[
'name' => 'profile',
'category' => 'event_sourcing/subscriber/default',
'category' => 'event_sourcing/subscriber/processor',
],
]),
$messages[1]->header(TraceHeader::class),
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Attribute/SubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace Patchlevel\EventSourcing\Tests\Unit\Attribute;

use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Subscription\RunMode;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Attribute\Subscriber */
final class SubscriberTest extends TestCase
{
public function testInstantiate(): void
{
$attribute = new Subscriber('foo');
$attribute = new Subscriber('foo', 'default', RunMode::FromBeginning);

self::assertSame('foo', $attribute->id);
}
Expand Down
37 changes: 0 additions & 37 deletions tests/Unit/Fixture/Dummy2Subscriber.php

This file was deleted.

37 changes: 0 additions & 37 deletions tests/Unit/Fixture/DummySubscriber.php

This file was deleted.

Loading

0 comments on commit 1a24eb4

Please sign in to comment.