Skip to content

Commit

Permalink
Add support for extracting other identities from Message headers
Browse files Browse the repository at this point in the history
  • Loading branch information
simensen committed Jul 14, 2023
1 parent 53ff3f8 commit 4ead5a7
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/Messages/MessageInflection.php
Expand Up @@ -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
{
Expand All @@ -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) ?: [])) {
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / PHPStan PHP 8.2

Method Dflydev\EventSauce\Support\Messages\MessageInflection::extractOptionalIdentityFromHeader() has invalid return type Whrc\Model\Common\Identity\Identity.

Check failure on line 62 in src/Messages/MessageInflection.php

View workflow job for this annotation

GitHub Actions / PHPStan PHP 8.2

PHPDoc tag @return with type EventSauce\EventSourcing\AggregateRootId|(T of Whrc\Model\Common\Identity\Identity)|null is not subtype of native type EventSauce\EventSourcing\AggregateRootId|null.

Check failure on line 62 in src/Messages/MessageInflection.php

View workflow job for this annotation

GitHub Actions / PHPStan PHP 8.2

PHPDoc tag @template T for method Dflydev\EventSauce\Support\Messages\MessageInflection::extractOptionalIdentityFromHeader() has invalid bound type Whrc\Model\Common\Identity\Identity.

Check failure on line 62 in src/Messages/MessageInflection.php

View workflow job for this annotation

GitHub Actions / PHPStan PHP 8.2

Parameter $identityType of method Dflydev\EventSauce\Support\Messages\MessageInflection::extractOptionalIdentityFromHeader() has invalid type Whrc\Model\Common\Identity\Identity.
{
return IdentityInflection::toObject(

Check failure on line 64 in src/Messages/MessageInflection.php

View workflow job for this annotation

GitHub Actions / PHPStan PHP 8.2

Unable to resolve the template type T in call to method static method Dflydev\EventSauce\Support\Identity\IdentityInflection::toObject()
$identityType,

Check failure on line 65 in src/Messages/MessageInflection.php

View workflow job for this annotation

GitHub Actions / PHPStan PHP 8.2

Parameter #1 $identityClass of static method Dflydev\EventSauce\Support\Identity\IdentityInflection::toObject() expects class-string<EventSauce\EventSourcing\AggregateRootId>, class-string<T of Whrc\Model\Common\Identity\Identity> given.
$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

View workflow job for this annotation

GitHub Actions / PHPStan PHP 8.2

Method Dflydev\EventSauce\Support\Messages\MessageInflection::extractIdentityFromHeader() has invalid return type Whrc\Model\Common\Identity\Identity.

Check failure on line 77 in src/Messages/MessageInflection.php

View workflow job for this annotation

GitHub Actions / PHPStan PHP 8.2

PHPDoc tag @return with type EventSauce\EventSourcing\AggregateRootId|T of Whrc\Model\Common\Identity\Identity is not subtype of native type EventSauce\EventSourcing\AggregateRootId.
{
$value = $this->extractOptionalIdentityFromHeader($message, $identityType, $headerName);

if (is_null($value)) {
throw new \RuntimeException('Expected $message to have an identity');
}

return $value;
}
}

0 comments on commit 4ead5a7

Please sign in to comment.