Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle scenario where payload does not provide aggregate mapping #174

Merged
merged 5 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Monorepo/ExampleApp/Common/Domain/Article/Publication.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;
use Monorepo\ExampleApp\Common\Domain\Article\Command\ChangeContent;
use Monorepo\ExampleApp\Common\Domain\Article\Command\ChangeTitle;
use Monorepo\ExampleApp\Common\Domain\Article\Command\CreatePublication;

#[Aggregate]
class Publication
{
public function __construct(#[AggregateIdentifier] private string $id, private string $title, private string $content)
public function __construct(#[Identifier] private string $id, private string $title, private string $content)
{
}

Expand Down
3 changes: 2 additions & 1 deletion Monorepo/ExampleApp/Common/Domain/Order/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Monorepo\ExampleApp\Common\Domain\Order;

use Ecotone\Modelling\Attribute\Identifier;
use Monorepo\ExampleApp\Common\Domain\Clock;
use Monorepo\ExampleApp\Common\Domain\Money;
use Monorepo\ExampleApp\Common\Domain\Order\Command\PlaceOrder;
Expand All @@ -22,7 +23,7 @@ final class Order
{
use WithAggregateEvents;

private function __construct(#[AggregateIdentifier] private UuidInterface $orderId, private UuidInterface $userId, private ShippingAddress $shippingAddress, private ProductDetails $productDetails, private \DateTimeImmutable $orderAt)
private function __construct(#[Identifier] private UuidInterface $orderId, private UuidInterface $userId, private ShippingAddress $shippingAddress, private ProductDetails $productDetails, private \DateTimeImmutable $orderAt)
{
$this->recordThat(new OrderWasPlaced($this->orderId));
}
Expand Down
3 changes: 2 additions & 1 deletion Monorepo/ExampleApp/Common/Domain/Product/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\Identifier;
use Ramsey\Uuid\UuidInterface;

#[Aggregate]
final class Product
{
public function __construct(#[AggregateIdentifier] private UuidInterface $productId, private ProductDetails $productDetails)
public function __construct(#[Identifier] private UuidInterface $productId, private ProductDetails $productDetails)
{
}

Expand Down
3 changes: 2 additions & 1 deletion Monorepo/ExampleApp/Common/Domain/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\Identifier;
use Ramsey\Uuid\UuidInterface;

#[Aggregate]
final class User
{
public function __construct(#[AggregateIdentifier] private UuidInterface $userId, private string $fullName)
public function __construct(#[Identifier] private UuidInterface $userId, private string $fullName)
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;
use Ecotone\Modelling\Attribute\QueryHandler;

#[Aggregate]
class Person
{
#[AggregateIdentifier]
#[Identifier]
private int $personId;
private string $name;

Expand Down
3 changes: 2 additions & 1 deletion packages/Dbal/tests/Fixture/ORM/Person/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;
use Ecotone\Modelling\Attribute\QueryHandler;
use Ecotone\Modelling\WithEvents;
use RuntimeException;
Expand All @@ -21,7 +22,7 @@ class Person

#[ORM\Id]
#[ORM\Column(name: 'person_id', type: 'integer')]
#[AggregateIdentifier]
#[Identifier]
private int $personId;

#[ORM\Column(name: 'name', type: 'string')]
Expand Down
1 change: 1 addition & 0 deletions packages/Ecotone/src/Lite/EcotoneLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static function bootstrap(
* @param string[] $classesToResolve
* @param array<string,string> $configurationVariables
* @param ContainerInterface|object[] $containerOrAvailableServices
* @deprecated Ecotone 2.0 will drop this method, use "bootstrapFlowTesting" instead
*/
public static function bootstrapForTesting(
array $classesToResolve = [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ private function initialize(InterfaceToCallRegistry $interfaceToCallRegistry, Cl
if (is_null($handledMessageClassNameDefinition) && is_null($mappingKey)) {
$aggregatePayloadIdentifiersMapping[$aggregateIdentifierName] = $aggregateIdentifierName;
} elseif (is_null($mappingKey) && ! $this->hasIdentifierMappingInMetadata($metadataIdentifierMapping, $aggregateIdentifierName)) {
throw new InvalidArgumentException("Can't find aggregate identifier mapping `{$aggregateIdentifierName}` in {$handledMessageClassNameDefinition} for {$aggregateClassDefinition}. Have you forgot to mark #[TargetAggregateIdentifier]?");
/** NO mapping available, identifier should come from message headers under "aggregate.id" */
$aggregatePayloadIdentifiersMapping = [];
} else {
$aggregatePayloadIdentifiersMapping[$aggregateIdentifierName] = $mappingKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Ecotone\Messaging\Attribute\Parameter\Header;
use Ecotone\Modelling\AggregateMessage;

/**
* @deprecated Ecotone 2.0 will drop this attribute. Use #[Identifier] instead
*/
#[Attribute(Attribute::TARGET_PROPERTY|Attribute::TARGET_PARAMETER)]
class AggregateIdentifier extends Header
{
Expand Down
13 changes: 13 additions & 0 deletions packages/Ecotone/src/Modelling/Attribute/Identifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Ecotone\Modelling\Attribute;

use Attribute;
use Ecotone\Messaging\Attribute\Parameter\Header;
use Ecotone\Modelling\AggregateMessage;

#[Attribute(Attribute::TARGET_PROPERTY|Attribute::TARGET_PARAMETER)]
class Identifier extends AggregateIdentifier
{

}
4 changes: 3 additions & 1 deletion packages/Ecotone/src/Modelling/Attribute/SagaIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Attribute;

/** @TODO Ecotone 2.0 make only Identifier attribute for Aggregate and Saga */
/**
* @deprecated Ecotone 2.0 will drop this attribute. Use #[Identifier] instead
*/
#[Attribute(Attribute::TARGET_PROPERTY|Attribute::TARGET_PARAMETER)]
class SagaIdentifier extends AggregateIdentifier
{
Expand Down
11 changes: 11 additions & 0 deletions packages/Ecotone/src/Modelling/Attribute/TargetIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Ecotone\Modelling\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
class TargetIdentifier extends TargetAggregateIdentifier
{

}
4 changes: 4 additions & 0 deletions packages/Ecotone/src/Modelling/LoadAggregateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function load(Message $message): ?Message
}

if (! $aggregateOrEventStream && $this->loadAggregateMode->isThrowingOnNotFound()) {
if ($aggregateIdentifiers === []) {
throw AggregateNotFoundException::create("Aggregate {$this->aggregateClassName} for calling {$this->aggregateMethod} was not as no identifiers were provided. Have you forgot to add use #[TargetIdentifier] in your Command or `aggregate.id` in metadata or provide #[Identifier] to MessageGateway?");
}

throw AggregateNotFoundException::create("Aggregate {$this->aggregateClassName} for calling {$this->aggregateMethod} was not found using identifiers " . json_encode($aggregateIdentifiers));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use Attribute;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\Identifier;

class ObjectWithConstructorProperties
{
public function __construct(
#[ExampleAttribute,
AggregateIdentifier] public string $id
Identifier] public string $id
) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Test\Ecotone\Messaging\Fixture\Handler\Property\Extra;

use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\Identifier;

trait PrivatePropertyTrait
{
#[AggregateIdentifier]
#[Identifier]
private ?ExtraObject $property;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\EventSourcingAggregate;
use Ecotone\Modelling\Attribute\EventSourcingSaga;
use Ecotone\Modelling\Attribute\Identifier;
use PHPUnit\Framework\TestCase;
use stdClass;
use Test\Ecotone\Messaging\Fixture\Conversion\Grouping\CollectionOfClassesFromDifferentNamespaceUsingGroupAlias;
Expand Down Expand Up @@ -181,7 +182,7 @@ public function test_retrieving_private_from_trait()
$classDefinition = ClassDefinition::createFor(TypeDescriptor::create(OrderWithTraits::class));

$this->assertEquals(
ClassPropertyDefinition::createPrivate('property', TypeDescriptor::create(ExtraObject::class), true, false, [new AggregateIdentifier()]),
ClassPropertyDefinition::createPrivate('property', TypeDescriptor::create(ExtraObject::class), true, false, [new Identifier()]),
$classDefinition->getProperty('property')
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Test\Ecotone\Modelling\Fixture\AggregateWithGateway;

use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;
use Ecotone\Modelling\Attribute\QueryHandler;
use Ramsey\Uuid\UuidInterface;

#[Aggregate]
final class Bucket
{
public const ADD = 'bucket.add';
public const GET = 'bucket.get';
public const CREATE = 'bucket.create';

/** @var array<string, string> */
private array $bucket = [];

private function __construct(#[Identifier] public UuidInterface $bucketId)
{
}

#[CommandHandler(self::CREATE)]
public static function create(UuidInterface $bucketId): self
{
return new self($bucketId);
}

#[CommandHandler(self::ADD)]
public function add(array $command): void
{
$this->bucket = array_merge($this->bucket, $command);
}

#[QueryHandler(self::GET)]
public function get(UuidInterface $key): string
{
return $this->bucket[$key->toString()];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Test\Ecotone\Modelling\Fixture\AggregateWithGateway;

use Ecotone\Messaging\Attribute\MessageGateway;
use Ecotone\Messaging\Attribute\Parameter\Payload;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\Identifier;
use Ramsey\Uuid\UuidInterface;

interface BucketGateway
{
#[MessageGateway(Bucket::ADD)]
public function add(#[Identifier] UuidInterface $bucketId, #[Payload] array $command): void;

#[MessageGateway(Bucket::GET)]
public function get(#[Identifier] UuidInterface $bucketId, #[Payload] UuidInterface $key): string;

#[MessageGateway(Bucket::GET)]
public function getWithoutAggregateIdentifier(#[Payload] UuidInterface $key): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;

#[Aggregate]
class AggregateCommandHandlerExample
{
#[AggregateIdentifier]
#[Identifier]
private string $id;

#[CommandHandler(endpointId: 'command-id')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;

#[Aggregate]
class AggregateCommandHandlerWithDoubledActionMethod
{
#[AggregateIdentifier]
#[Identifier]
private string $id;

#[CommandHandler('sameChannel')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;

#[Aggregate]
class AggregateCommandHandlerWithDoubledFactoryMethod
{
#[AggregateIdentifier]
#[Identifier]
private string $id;

#[CommandHandler('sameChannel')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;

#[Aggregate]
class AggregateCommandHandlerWithFactoryMethod
{
#[AggregateIdentifier]
#[Identifier]
private string $id;

#[CommandHandler(endpointId: 'factory-id')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;
use Ecotone\Modelling\Attribute\IgnorePayload;
use stdClass;

#[Aggregate]
class AggregateCommandHandlerWithNoCommandDataExample
{
#[AggregateIdentifier]
#[Identifier]
private string $id;

#[CommandHandler('doActionChannel', 'command-id')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;

#[Aggregate]
class AggregateCommandHandlerWithRedirectionByChannelName
{
#[AggregateIdentifier]
#[Identifier]
private string $id;

#[CommandHandler('sameChannel', 'factory')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\AggregateIdentifier;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\Identifier;

#[Aggregate]
class AggregateCommandHandlerWithRedirectionByClass
{
#[AggregateIdentifier]
#[Identifier]
private string $id;

#[CommandHandler(endpointId: 'factory')]
Expand Down
Loading