-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #552 from patchlevel/argument-resolver
Argument resolver for subscriber
- Loading branch information
Showing
26 changed files
with
787 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,7 @@ deptrac: | |
- Metadata | ||
- Subscription | ||
Subscription: | ||
- Aggregate | ||
- Attribute | ||
- Clock | ||
- Message | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Metadata\Subscriber; | ||
|
||
final class ArgumentMetadata | ||
{ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly string $type, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Metadata\Subscriber; | ||
|
||
use Patchlevel\EventSourcing\Metadata\MetadataException; | ||
|
||
use function sprintf; | ||
|
||
final class ArgumentTypeNotSupported extends MetadataException | ||
{ | ||
public static function missingType(string $class, string $method, string $argumentName): self | ||
{ | ||
return new self( | ||
sprintf( | ||
'Argument type for method "%s" in class "%s" is not supported. Argument "%s" must have a type.', | ||
$method, | ||
$class, | ||
$argumentName, | ||
), | ||
); | ||
} | ||
|
||
public static function onlyNamedTypeSupported(string $class, string $method, string $argumentName): self | ||
{ | ||
return new self( | ||
sprintf( | ||
'Argument type for method "%s" in class "%s" is not supported. Argument "%s" must not have a union or intersection type.', | ||
$method, | ||
$class, | ||
$argumentName, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Metadata\Subscriber; | ||
|
||
final class SubscribeMethodMetadata | ||
{ | ||
/** @param list<ArgumentMetadata> $arguments */ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly array $arguments = [], | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Subscription/Subscriber/ArgumentResolver/AggregateIdArgumentResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver; | ||
|
||
use Patchlevel\EventSourcing\Aggregate\AggregateHeader; | ||
use Patchlevel\EventSourcing\Message\Message; | ||
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata; | ||
|
||
use function in_array; | ||
|
||
final class AggregateIdArgumentResolver implements ArgumentResolver | ||
{ | ||
public function resolve(ArgumentMetadata $argument, Message $message): string | ||
{ | ||
return $message->header(AggregateHeader::class)->aggregateId; | ||
} | ||
|
||
public function support(ArgumentMetadata $argument, string $eventClass): bool | ||
{ | ||
return $argument->type === 'string' && in_array($argument->name, ['aggregateId', 'aggregateRootId']); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Subscription/Subscriber/ArgumentResolver/ArgumentResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver; | ||
|
||
use Patchlevel\EventSourcing\Message\Message; | ||
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata; | ||
|
||
interface ArgumentResolver | ||
{ | ||
public function resolve(ArgumentMetadata $argument, Message $message): mixed; | ||
|
||
public function support(ArgumentMetadata $argument, string $eventClass): bool; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Subscription/Subscriber/ArgumentResolver/EventArgumentResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver; | ||
|
||
use Patchlevel\EventSourcing\Message\Message; | ||
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata; | ||
|
||
use function class_exists; | ||
use function is_a; | ||
|
||
final class EventArgumentResolver implements ArgumentResolver | ||
{ | ||
public function resolve(ArgumentMetadata $argument, Message $message): object | ||
{ | ||
return $message->event(); | ||
} | ||
|
||
public function support(ArgumentMetadata $argument, string $eventClass): bool | ||
{ | ||
return class_exists($argument->type) && is_a($eventClass, $argument->type, true); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Subscription/Subscriber/ArgumentResolver/MessageArgumentResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver; | ||
|
||
use Patchlevel\EventSourcing\Message\Message; | ||
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata; | ||
|
||
final class MessageArgumentResolver implements ArgumentResolver | ||
{ | ||
public function resolve(ArgumentMetadata $argument, Message $message): Message | ||
{ | ||
return $message; | ||
} | ||
|
||
public function support(ArgumentMetadata $argument, string $eventClass): bool | ||
{ | ||
return $argument->type === Message::class; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Subscription/Subscriber/ArgumentResolver/RecordedOnArgumentResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver; | ||
|
||
use DateTimeImmutable; | ||
use Patchlevel\EventSourcing\Aggregate\AggregateHeader; | ||
use Patchlevel\EventSourcing\Message\Message; | ||
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata; | ||
|
||
final class RecordedOnArgumentResolver implements ArgumentResolver | ||
{ | ||
public function resolve(ArgumentMetadata $argument, Message $message): DateTimeImmutable | ||
{ | ||
return $message->header(AggregateHeader::class)->recordedOn; | ||
} | ||
|
||
public function support(ArgumentMetadata $argument, string $eventClass): bool | ||
{ | ||
return $argument->type === DateTimeImmutable::class; | ||
} | ||
} |
Oops, something went wrong.