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 ded2748
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 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 @@ -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::Once)]
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

0 comments on commit ded2748

Please sign in to comment.