Skip to content

Commit 4a2596c

Browse files
committed
stateful event sourced workflow with multiple aggregates
1 parent 77dbbe7 commit 4a2596c

File tree

11 files changed

+325
-0
lines changed

11 files changed

+325
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Test\Ecotone\EventSourcing\Fixture\StatefulEventSourcedWorkflowWithMultipleAggregates;
4+
5+
class AddItemToBasket
6+
{
7+
public function __construct(public string $basketId, public string $itemId, public int $quantity)
8+
{
9+
}
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Test\Ecotone\EventSourcing\Fixture\StatefulEventSourcedWorkflowWithMultipleAggregates;
4+
5+
use Ecotone\Messaging\Attribute\Parameter\Payload;
6+
use Ecotone\Modelling\Attribute\CommandHandler;
7+
use Ecotone\Modelling\Attribute\EventSourcingAggregate;
8+
use Ecotone\Modelling\Attribute\EventSourcingHandler;
9+
use Ecotone\Modelling\Attribute\Identifier;
10+
use Ecotone\Modelling\WithAggregateVersioning;
11+
use Ecotone\Modelling\WithEvents;
12+
13+
#[EventSourcingAggregate(withInternalEventRecorder: true)]
14+
class Basket
15+
{
16+
use WithEvents;
17+
use WithAggregateVersioning;
18+
19+
#[Identifier]
20+
private string $basketId;
21+
22+
#[CommandHandler(outputChannelName: 'itemInventory.makeReservation')]
23+
public function addItemToBasket(#[Payload] AddItemToBasket $command): ItemReservation
24+
{
25+
$this->recordThat(new ItemWasAddedToBasket($this->basketId, $command->itemId, $command->quantity));
26+
27+
return new ItemReservation($command->itemId, $command->quantity);
28+
}
29+
30+
#[EventSourcingHandler]
31+
public function applyBasketCreated(BasketCreated $event): void
32+
{
33+
$this->basketId = $event->basketId;
34+
}
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Test\Ecotone\EventSourcing\Fixture\StatefulEventSourcedWorkflowWithMultipleAggregates;
4+
5+
use Ecotone\Modelling\Attribute\NamedEvent;
6+
7+
#[NamedEvent('BasketCreated')]
8+
class BasketCreated
9+
{
10+
public function __construct(public string $basketId)
11+
{
12+
}
13+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Test\Ecotone\EventSourcing\Fixture\StatefulEventSourcedWorkflowWithMultipleAggregates;
4+
5+
use Ecotone\Messaging\Attribute\Converter;
6+
7+
class Converters
8+
{
9+
#[Converter]
10+
public function fromBasketCreated(BasketCreated $message): array
11+
{
12+
return ['basketId' => $message->basketId];
13+
}
14+
15+
#[Converter]
16+
public function toBasketCreated(array $payload): BasketCreated
17+
{
18+
return new BasketCreated(basketId: $payload['basketId']);
19+
}
20+
21+
#[Converter]
22+
public function fromItemInventoryCreated(ItemInventoryCreated $message): array
23+
{
24+
return ['itemId' => $message->itemId];
25+
}
26+
27+
#[Converter]
28+
public function toItemInventoryCreated(array $payload): ItemInventoryCreated
29+
{
30+
return new ItemInventoryCreated(itemId: $payload['itemId']);
31+
}
32+
33+
#[Converter]
34+
public function fromInventoryStockIncreased(InventoryStockIncreased $message): array
35+
{
36+
return ['itemId' => $message->itemId, 'quantity' => $message->quantity];
37+
}
38+
39+
#[Converter]
40+
public function toInventoryStockIncreased(array $payload): InventoryStockIncreased
41+
{
42+
return new InventoryStockIncreased(itemId: $payload['itemId'], quantity: $payload['quantity']);
43+
}
44+
45+
#[Converter]
46+
public function fromItemReserved(ItemReserved $message): array
47+
{
48+
return ['itemId' => $message->itemId, 'quantity' => $message->quantity];
49+
}
50+
51+
#[Converter]
52+
public function toItemReserved(array $payload): ItemReserved
53+
{
54+
return new ItemReserved(itemId: $payload['itemId'], quantity: $payload['quantity']);
55+
}
56+
57+
#[Converter]
58+
public function fromItemWasAddedToBasket(ItemWasAddedToBasket $message): array
59+
{
60+
return ['basketId' => $message->basketId, 'itemId' => $message->itemId, 'quantity' => $message->quantity];
61+
}
62+
63+
#[Converter]
64+
public function toItemWasAddedToBasket(array $payload): ItemWasAddedToBasket
65+
{
66+
return new ItemWasAddedToBasket(
67+
basketId: $payload['basketId'],
68+
itemId: $payload['itemId'],
69+
quantity: $payload['quantity']
70+
);
71+
}
72+
73+
#[Converter]
74+
public function fromItemReservation(ItemReservation $message): array
75+
{
76+
return ['itemId' => $message->itemId, 'quantity' => $message->quantity];
77+
}
78+
79+
#[Converter]
80+
public function toItemReservation(array $payload): ItemReservation
81+
{
82+
return new ItemReservation(
83+
itemId: $payload['itemId'],
84+
quantity: $payload['quantity']
85+
);
86+
}
87+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Test\Ecotone\EventSourcing\Fixture\StatefulEventSourcedWorkflowWithMultipleAggregates;
4+
5+
use Ecotone\Modelling\Attribute\NamedEvent;
6+
7+
#[NamedEvent('InventoryStockIncreased')]
8+
class InventoryStockIncreased
9+
{
10+
public function __construct(public string $itemId, public int $quantity)
11+
{
12+
}
13+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Test\Ecotone\EventSourcing\Fixture\StatefulEventSourcedWorkflowWithMultipleAggregates;
4+
5+
use Ecotone\Messaging\Attribute\Parameter\Payload;
6+
use Ecotone\Modelling\Attribute\CommandHandler;
7+
use Ecotone\Modelling\Attribute\EventSourcingAggregate;
8+
use Ecotone\Modelling\Attribute\EventSourcingHandler;
9+
use Ecotone\Modelling\Attribute\Identifier;
10+
use Ecotone\Modelling\WithAggregateVersioning;
11+
use Ecotone\Modelling\WithEvents;
12+
13+
#[EventSourcingAggregate(withInternalEventRecorder: true)]
14+
class ItemInventory
15+
{
16+
use WithEvents;
17+
use WithAggregateVersioning;
18+
19+
#[Identifier]
20+
private string $itemId;
21+
22+
private int $quantity = 0;
23+
24+
#[CommandHandler(routingKey: 'itemInventory.makeReservation')]
25+
public function makeReservation(#[Payload] ItemReservation $itemReservation): void
26+
{
27+
$this->recordThat(new ItemReserved($this->itemId, $itemReservation->quantity));
28+
}
29+
30+
#[EventSourcingHandler]
31+
public function applyItemInventoryCreated(ItemInventoryCreated $event): void
32+
{
33+
$this->itemId = $event->itemId;
34+
}
35+
36+
#[EventSourcingHandler]
37+
public function applyInventoryStockIncreased(InventoryStockIncreased $event): void
38+
{
39+
$this->quantity += $event->quantity;
40+
}
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Test\Ecotone\EventSourcing\Fixture\StatefulEventSourcedWorkflowWithMultipleAggregates;
4+
5+
use Ecotone\Modelling\Attribute\NamedEvent;
6+
7+
#[NamedEvent('ItemInventoryCreated')]
8+
class ItemInventoryCreated
9+
{
10+
public function __construct(public string $itemId)
11+
{
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Test\Ecotone\EventSourcing\Fixture\StatefulEventSourcedWorkflowWithMultipleAggregates;
4+
5+
use Ecotone\Modelling\Attribute\TargetIdentifier;
6+
7+
class ItemReservation
8+
{
9+
public function __construct(#[TargetIdentifier] public string $itemId, public int $quantity)
10+
{
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Test\Ecotone\EventSourcing\Fixture\StatefulEventSourcedWorkflowWithMultipleAggregates;
4+
5+
use Ecotone\Modelling\Attribute\NamedEvent;
6+
7+
#[NamedEvent('ItemReserved')]
8+
class ItemReserved
9+
{
10+
public function __construct(public string $itemId, public int $quantity)
11+
{
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Test\Ecotone\EventSourcing\Fixture\StatefulEventSourcedWorkflowWithMultipleAggregates;
4+
5+
use Ecotone\Modelling\Attribute\NamedEvent;
6+
7+
#[NamedEvent('ItemWasAddedToBasket')]
8+
class ItemWasAddedToBasket
9+
{
10+
public function __construct(public string $basketId, public string $itemId, public int $quantity)
11+
{
12+
}
13+
}

0 commit comments

Comments
 (0)