Skip to content

Commit 2d29b10

Browse files
committed
Add Behaviour suffix to aggregate class and move aggregate state class to value objects
1 parent 566052d commit 2d29b10

File tree

4 files changed

+27
-29
lines changed

4 files changed

+27
-29
lines changed

src/Config/PreConfiguredNaming.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,13 @@ public function getAggregateStateFullyQualifiedClassName(
4848
AggregateType $type,
4949
EventSourcingAnalyzer $analyzer
5050
): string {
51-
$namespace = $this->getClassNamespaceFromPath(
52-
$this->config->determinePath($type, $analyzer)
53-
);
54-
5551
$aggregateState = $this->findAggregateState($type->id(), VertexConnectionMap::WALK_FORWARD, $analyzer);
5652

5753
if ($aggregateState === null) {
5854
throw new RuntimeException(\sprintf('Could not find aggregate state for aggregate "%s" - "%s"', $type->id(), $type->name()));
5955
}
6056

61-
$className = ($this->config->getFilterClassName())($aggregateState->identity()->name());
62-
63-
return $namespace . '\\' . $className . 'State';
57+
return $this->getFullyQualifiedClassName($aggregateState->identity(), $analyzer);
6458
}
6559

6660
public function getAggregateBehaviourFullyQualifiedClassName(
@@ -73,7 +67,7 @@ public function getAggregateBehaviourFullyQualifiedClassName(
7367

7468
$className = ($this->config->getFilterClassName())($type->name());
7569

76-
return $namespace . '\\' . $className;
70+
return $namespace . '\\' . $className . 'Behaviour';
7771
}
7872

7973
public function getAggregateIdFullyQualifiedClassName(AggregateType $type, EventSourcingAnalyzer $analyzer): string

src/Helper/ApiDescriptionClassMapTrait.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use EventEngine\CodeGenerator\EventEngineAst\Config\Naming;
1515
use EventEngine\CodeGenerator\EventEngineAst\Exception\WrongVertexConnection;
1616
use EventEngine\CodeGenerator\EventEngineAst\NodeVisitor\ClassMap;
17+
use EventEngine\InspectioGraph\AggregateType;
1718
use EventEngine\InspectioGraph\EventSourcingAnalyzer;
1819
use EventEngine\InspectioGraph\VertexConnection;
1920
use OpenCodeModeling\CodeAst\Builder\ClassBuilder;
@@ -61,7 +62,12 @@ private function generateApiDescriptionClassMapFor(
6162
$classBuilder = $this->getApiDescriptionClassBuilder($connection, $analyzer, $files, $type);
6263

6364
$identity = $connection->identity();
64-
$identityFqcn = $this->config->getFullyQualifiedClassName($identity, $analyzer);
65+
66+
if ($identity instanceof AggregateType) {
67+
$identityFqcn = $this->config->getAggregateBehaviourFullyQualifiedClassName($identity, $analyzer);
68+
} else {
69+
$identityFqcn = $this->config->getFullyQualifiedClassName($identity, $analyzer);
70+
}
6571

6672
$classBuilder->addNamespaceImport(
6773
$identityFqcn

tests/AggregateStateImmutableRecordOverrideTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function it_creates_aggregate_state_immutable_record_override(): void
5656

5757
foreach ($fileCollection as $file) {
5858
switch ($file->getName()) {
59-
case 'BuildingState':
59+
case 'Building':
6060
$this->assertAggregateStateFile($file);
6161
break;
6262
default:
@@ -78,11 +78,11 @@ private function assertAggregateStateFile(ClassBuilder $classBuilder): void
7878
<?php
7979
8080
declare (strict_types=1);
81-
namespace MyService\Domain\Model\Building;
81+
namespace MyService\Domain\Model\ValueObject;
8282
8383
use EventEngine\Data\ImmutableRecord;
8484
use EventEngine\Data\ImmutableRecordLogic;
85-
final class BuildingState implements ImmutableRecord
85+
final class Building implements ImmutableRecord
8686
{
8787
use ImmutableRecordLogic;
8888
private array $state = [];

tests/AggregateTest.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ private function assertApiDescription(ClassBuilder $classBuilder): void
6565
use EventEngine\EventEngineDescription;
6666
use EventEngine\JsonSchema\JsonSchema;
6767
use EventEngine\JsonSchema\JsonSchemaArray;
68-
use MyService\Domain\Model\Building\Building;
68+
use MyService\Domain\Model\Building\BuildingBehaviour;
6969
final class Aggregate implements EventEngineDescription
7070
{
7171
public const BUILDING = 'Building';
7272
public static function describe(EventEngine $eventEngine) : void
7373
{
74-
$eventEngine->process(Command::ADD_BUILDING)->withNew(self::BUILDING)->identifiedBy('buildingId')->handle([Building::class, 'addBuilding'])->recordThat(Event::BUILDING_ADDED)->apply([Building::class, 'whenBuildingAdded'])->storeStateIn('buildings')->storeEventsIn('building_stream');
74+
$eventEngine->process(Command::ADD_BUILDING)->withNew(self::BUILDING)->identifiedBy('buildingId')->handle([BuildingBehaviour::class, 'addBuilding'])->recordThat(Event::BUILDING_ADDED)->apply([BuildingBehaviour::class, 'whenBuildingAdded'])->storeStateIn('buildings')->storeEventsIn('building_stream');
7575
}
7676
}
7777
EOF;
@@ -123,10 +123,10 @@ private function assertApiDescriptionClassMap(ClassBuilder $classBuilder): void
123123
use EventEngine\EventEngineDescription;
124124
use EventEngine\JsonSchema\JsonSchema;
125125
use EventEngine\JsonSchema\JsonSchemaArray;
126-
use MyService\Domain\Model\Building\Building;
126+
use MyService\Domain\Model\Building\BuildingBehaviour;
127127
final class Aggregate implements EventEngineDescription
128128
{
129-
public const CLASS_MAP = [self::BUILDING => Building::class];
129+
public const CLASS_MAP = [self::BUILDING => BuildingBehaviour::class];
130130
}
131131
EOF;
132132
$this->assertSame($expected, $this->config->config()->getPrinter()->prettyPrintFile($nodeTraverser->traverse($ast)));
@@ -184,14 +184,14 @@ private function assertApiDescriptionWithClassMap(ClassBuilder $classBuilder): v
184184
use EventEngine\EventEngineDescription;
185185
use EventEngine\JsonSchema\JsonSchema;
186186
use EventEngine\JsonSchema\JsonSchemaArray;
187-
use MyService\Domain\Model\Building\Building;
187+
use MyService\Domain\Model\Building\BuildingBehaviour;
188188
final class Aggregate implements EventEngineDescription
189189
{
190190
public const BUILDING = 'Building';
191-
public const CLASS_MAP = [self::BUILDING => Building::class];
191+
public const CLASS_MAP = [self::BUILDING => BuildingBehaviour::class];
192192
public static function describe(EventEngine $eventEngine) : void
193193
{
194-
$eventEngine->process(Command::ADD_BUILDING)->withNew(self::BUILDING)->identifiedBy('buildingId')->handle([Building::class, 'addBuilding'])->recordThat(Event::BUILDING_ADDED)->apply([Building::class, 'whenBuildingAdded'])->storeStateIn('buildings')->storeEventsIn('building_stream');
194+
$eventEngine->process(Command::ADD_BUILDING)->withNew(self::BUILDING)->identifiedBy('buildingId')->handle([BuildingBehaviour::class, 'addBuilding'])->recordThat(Event::BUILDING_ADDED)->apply([BuildingBehaviour::class, 'whenBuildingAdded'])->storeStateIn('buildings')->storeEventsIn('building_stream');
195195
}
196196
}
197197
EOF;
@@ -221,7 +221,7 @@ public function it_creates_aggregate_file(): void
221221

222222
foreach ($fileCollection as $file) {
223223
switch ($file->getName()) {
224-
case 'Building':
224+
case 'BuildingBehaviour':
225225
$this->assertAggregateFile($file);
226226
break;
227227
case 'BuildingId':
@@ -252,16 +252,16 @@ private function assertAggregateFile(ClassBuilder $classBuilder): void
252252
use Generator;
253253
use MyService\Domain\Api\Command;
254254
use MyService\Domain\Api\Event;
255-
use MyService\Domain\Model\Building\BuildingState;
256-
final class Building
255+
use MyService\Domain\Model\ValueObject\Building;
256+
final class BuildingBehaviour
257257
{
258258
public static function addBuilding(Message $addBuilding) : Generator
259259
{
260260
(yield [Event::BUILDING_ADDED, $addBuilding->payload()]);
261261
}
262-
public static function whenBuildingAdded(Message $buildingAdded) : BuildingState
262+
public static function whenBuildingAdded(Message $buildingAdded) : Building
263263
{
264-
return BuildingState::fromArray($buildingAdded->payload());
264+
return Building::fromArray($buildingAdded->payload());
265265
}
266266
}
267267
EOF;
@@ -290,7 +290,7 @@ public function it_creates_aggregate_state_file_with_value_objects(): void
290290

291291
foreach ($fileCollection as $file) {
292292
switch ($file->getName()) {
293-
case 'BuildingState':
293+
case 'Building':
294294
$this->assertAggregateStateFile($file);
295295
break;
296296
case 'BuildingId':
@@ -315,13 +315,11 @@ private function assertAggregateStateFile(ClassBuilder $classBuilder): void
315315
<?php
316316
317317
declare (strict_types=1);
318-
namespace MyService\Domain\Model\Building;
318+
namespace MyService\Domain\Model\ValueObject;
319319
320320
use EventEngine\Data\ImmutableRecord;
321321
use EventEngine\Data\ImmutableRecordLogic;
322-
use MyService\Domain\Model\ValueObject\BuildingId;
323-
use MyService\Domain\Model\ValueObject\Name;
324-
final class BuildingState implements ImmutableRecord
322+
final class Building implements ImmutableRecord
325323
{
326324
use ImmutableRecordLogic;
327325
public const BUILDING_ID = 'buildingId';

0 commit comments

Comments
 (0)