-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
use EventSauce\EventSourcing\AggregateRootId; | ||
use EventSauce\EventSourcing\ClassNameInflector; | ||
use EventSauce\EventSourcing\Message; | ||
use Whrc\Model\Common\Identity\Identity; | ||
|
||
final readonly class MessageInflection | ||
{ | ||
|
@@ -25,12 +26,12 @@ public function __construct(private ClassNameInflector $classNameInflector) | |
*/ | ||
public function extractAggregateRootId(Message $message, ?string $identityType = null): AggregateRootId | ||
{ | ||
/** @var class-string<AggregateRootIdAware>|null $aggregateRootTypeString */ | ||
/** @var class-string<AggregateRootIdAware<T>>|null $aggregateRootTypeString */ | ||
$aggregateRootTypeString = $message->aggregateRootType(); | ||
|
||
!is_null($aggregateRootTypeString) || throw new \LogicException('Expected $message to have an aggregate root type.'); | ||
|
||
/** @phpstan-var class-string<AggregateRootIdAware> $aggregateRootType */ | ||
/** @phpstan-var class-string<AggregateRootIdAware<T>> $aggregateRootType */ | ||
$aggregateRootType = $this->classNameInflector->typeToClassName($aggregateRootTypeString); | ||
|
||
if (!in_array(AggregateRootIdAware::class, class_implements($aggregateRootType) ?: [])) { | ||
|
@@ -50,4 +51,37 @@ public function extractAggregateRootId(Message $message, ?string $identityType = | |
|
||
return $value; | ||
} | ||
|
||
/** | ||
* @template T of Identity | ||
* | ||
* @param class-string<T> $identityType | ||
* | ||
* @phpstan-return T|AggregateRootId|null $value | ||
*/ | ||
public function extractOptionalIdentityFromHeader(Message $message, string $identityType, string $headerName): ?AggregateRootId | ||
Check failure on line 62 in src/Messages/MessageInflection.php GitHub Actions / PHPStan PHP 8.2
Check failure on line 62 in src/Messages/MessageInflection.php GitHub Actions / PHPStan PHP 8.2
Check failure on line 62 in src/Messages/MessageInflection.php GitHub Actions / PHPStan PHP 8.2
Check failure on line 62 in src/Messages/MessageInflection.php GitHub Actions / PHPStan PHP 8.2
|
||
{ | ||
return IdentityInflection::toObject( | ||
$identityType, | ||
Check failure on line 65 in src/Messages/MessageInflection.php GitHub Actions / PHPStan PHP 8.2
|
||
$message->header($headerName) | ||
); | ||
} | ||
|
||
/** | ||
* @template T of Identity | ||
* | ||
* @param class-string<T> $identityType | ||
* | ||
* @phpstan-return T|AggregateRootId $value | ||
*/ | ||
public function extractIdentityFromHeader(Message $message, string $identityType, string $headerName): AggregateRootId | ||
Check failure on line 77 in src/Messages/MessageInflection.php GitHub Actions / PHPStan PHP 8.2
Check failure on line 77 in src/Messages/MessageInflection.php GitHub Actions / PHPStan PHP 8.2
|
||
{ | ||
$value = $this->extractOptionalIdentityFromHeader($message, $identityType, $headerName); | ||
|
||
if (is_null($value)) { | ||
throw new \RuntimeException('Expected $message to have an identity'); | ||
} | ||
|
||
return $value; | ||
} | ||
} |