Skip to content

Commit 019aef0

Browse files
authored
Merge pull request #3876 from neos/feature/simplify-command-handling
9.0: TASK: Simplify command handler resolution
2 parents 82531cc + 65ae8a2 commit 019aef0

File tree

8 files changed

+82
-130
lines changed

8 files changed

+82
-130
lines changed

Neos.ContentRepository.Core/Classes/Feature/ContentStreamCommandHandler.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,16 @@ final class ContentStreamCommandHandler implements CommandHandlerInterface
3939
{
4040
public function canHandle(CommandInterface $command): bool
4141
{
42-
return $command instanceof CreateContentStream
43-
|| $command instanceof ForkContentStream
44-
|| $command instanceof RemoveContentStream;
42+
return method_exists($this, 'handle' . (new \ReflectionClass($command))->getShortName());
4543
}
4644

4745
public function handle(CommandInterface $command, ContentRepository $contentRepository): EventsToPublish
4846
{
49-
if ($command instanceof CreateContentStream) {
50-
return $this->handleCreateContentStream($command, $contentRepository);
51-
} elseif ($command instanceof ForkContentStream) {
52-
return $this->handleForkContentStream($command, $contentRepository);
53-
} elseif ($command instanceof RemoveContentStream) {
54-
return $this->handleRemoveContentStream($command, $contentRepository);
55-
}
56-
57-
throw new \RuntimeException('Unsupported command type');
47+
return match ($command::class) {
48+
CreateContentStream::class => $this->handleCreateContentStream($command, $contentRepository),
49+
ForkContentStream::class => $this->handleForkContentStream($command, $contentRepository),
50+
RemoveContentStream::class => $this->handleRemoveContentStream($command, $contentRepository),
51+
};
5852
}
5953

6054
/**
@@ -164,4 +158,5 @@ protected function requireContentStreamToExist(
164158
);
165159
}
166160
}
161+
167162
}

Neos.ContentRepository.Core/Classes/Feature/DimensionSpaceAdjustment/DimensionSpaceCommandHandler.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,15 @@ public function __construct(
5151

5252
public function canHandle(CommandInterface $command): bool
5353
{
54-
return $command instanceof MoveDimensionSpacePoint
55-
|| $command instanceof AddDimensionShineThrough;
54+
return method_exists($this, 'handle' . (new \ReflectionClass($command))->getShortName());
5655
}
5756

5857
public function handle(CommandInterface $command, ContentRepository $contentRepository): EventsToPublish
5958
{
60-
if ($command instanceof MoveDimensionSpacePoint) {
61-
return $this->handleMoveDimensionSpacePoint($command, $contentRepository);
62-
} elseif ($command instanceof AddDimensionShineThrough) {
63-
return $this->handleAddDimensionShineThrough($command, $contentRepository);
64-
}
65-
66-
throw new \RuntimeException('Command ' . get_class($command) . ' not supported by this command handler.');
59+
return match ($command::class) {
60+
MoveDimensionSpacePoint::class => $this->handleMoveDimensionSpacePoint($command, $contentRepository),
61+
AddDimensionShineThrough::class => $this->handleAddDimensionShineThrough($command, $contentRepository),
62+
};
6763
}
6864

6965
private function handleMoveDimensionSpacePoint(

Neos.ContentRepository.Core/Classes/Feature/NodeAggregateCommandHandler.php

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,39 @@
1717
use Neos\ContentRepository\Core\CommandHandler\CommandHandlerInterface;
1818
use Neos\ContentRepository\Core\CommandHandler\CommandInterface;
1919
use Neos\ContentRepository\Core\ContentRepository;
20+
use Neos\ContentRepository\Core\DimensionSpace;
2021
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePointSet;
2122
use Neos\ContentRepository\Core\EventStore\EventsToPublish;
22-
use Neos\ContentRepository\Core\SharedModel\Exception\NodeConstraintException;
23+
use Neos\ContentRepository\Core\Feature\Common\ConstraintChecks;
24+
use Neos\ContentRepository\Core\Feature\Common\TetheredNodeInternals;
2325
use Neos\ContentRepository\Core\Feature\NodeCreation\Command\CreateNodeAggregateWithNode;
2426
use Neos\ContentRepository\Core\Feature\NodeCreation\Command\CreateNodeAggregateWithNodeAndSerializedProperties;
27+
use Neos\ContentRepository\Core\Feature\NodeCreation\NodeCreation;
2528
use Neos\ContentRepository\Core\Feature\NodeDisabling\Command\DisableNodeAggregate;
2629
use Neos\ContentRepository\Core\Feature\NodeDisabling\Command\EnableNodeAggregate;
30+
use Neos\ContentRepository\Core\Feature\NodeDisabling\NodeDisabling;
2731
use Neos\ContentRepository\Core\Feature\NodeModification\Command\SetNodeProperties;
2832
use Neos\ContentRepository\Core\Feature\NodeModification\Command\SetSerializedNodeProperties;
33+
use Neos\ContentRepository\Core\Feature\NodeModification\NodeModification;
2934
use Neos\ContentRepository\Core\Feature\NodeMove\Command\MoveNodeAggregate;
35+
use Neos\ContentRepository\Core\Feature\NodeMove\NodeMove;
3036
use Neos\ContentRepository\Core\Feature\NodeReferencing\Command\SetNodeReferences;
3137
use Neos\ContentRepository\Core\Feature\NodeReferencing\Command\SetSerializedNodeReferences;
32-
use Neos\ContentRepository\Core\Feature\NodeRemoval\Command\RemoveNodeAggregate;
33-
use Neos\ContentRepository\Core\Feature\NodeRenaming\Command\ChangeNodeAggregateName;
34-
use Neos\ContentRepository\Core\Feature\NodeVariation\Command\CreateNodeVariant;
35-
use Neos\ContentRepository\Core\Feature\RootNodeCreation\Command\CreateRootNodeAggregateWithNode;
36-
use Neos\ContentRepository\Core\Feature\RootNodeCreation\RootNodeCreation;
37-
use Neos\ContentRepository\Core\DimensionSpace;
38-
use Neos\ContentRepository\Core\Feature\NodeTypeChange\Command\ChangeNodeAggregateType;
39-
use Neos\ContentRepository\Core\Feature\Common\ConstraintChecks;
40-
use Neos\ContentRepository\Core\Feature\NodeCreation\NodeCreation;
41-
use Neos\ContentRepository\Core\Feature\NodeDisabling\NodeDisabling;
42-
use Neos\ContentRepository\Core\Feature\NodeModification\NodeModification;
43-
use Neos\ContentRepository\Core\Feature\NodeMove\NodeMove;
4438
use Neos\ContentRepository\Core\Feature\NodeReferencing\NodeReferencing;
39+
use Neos\ContentRepository\Core\Feature\NodeRemoval\Command\RemoveNodeAggregate;
4540
use Neos\ContentRepository\Core\Feature\NodeRemoval\NodeRemoval;
41+
use Neos\ContentRepository\Core\Feature\NodeRenaming\Command\ChangeNodeAggregateName;
4642
use Neos\ContentRepository\Core\Feature\NodeRenaming\NodeRenaming;
43+
use Neos\ContentRepository\Core\Feature\NodeTypeChange\Command\ChangeNodeAggregateType;
4744
use Neos\ContentRepository\Core\Feature\NodeTypeChange\NodeTypeChange;
45+
use Neos\ContentRepository\Core\Feature\NodeVariation\Command\CreateNodeVariant;
4846
use Neos\ContentRepository\Core\Feature\NodeVariation\NodeVariation;
49-
use Neos\ContentRepository\Core\Feature\Common\TetheredNodeInternals;
50-
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeAggregate;
51-
use Neos\ContentRepository\Core\NodeType\NodeTypeManager;
47+
use Neos\ContentRepository\Core\Feature\RootNodeCreation\Command\CreateRootNodeAggregateWithNode;
48+
use Neos\ContentRepository\Core\Feature\RootNodeCreation\RootNodeCreation;
5249
use Neos\ContentRepository\Core\Infrastructure\Property\PropertyConverter;
50+
use Neos\ContentRepository\Core\NodeType\NodeTypeManager;
51+
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeAggregate;
52+
use Neos\ContentRepository\Core\SharedModel\Exception\NodeConstraintException;
5353

5454
/**
5555
* @internal from userland, you'll use ContentRepository::handle to dispatch commands
@@ -106,23 +106,9 @@ public function __construct(
106106

107107
public function canHandle(CommandInterface $command): bool
108108
{
109-
return $command instanceof SetNodeProperties
110-
|| $command instanceof SetSerializedNodeProperties
111-
|| $command instanceof SetNodeReferences
112-
|| $command instanceof SetSerializedNodeReferences
113-
|| $command instanceof ChangeNodeAggregateType
114-
|| $command instanceof RemoveNodeAggregate
115-
|| $command instanceof CreateNodeAggregateWithNode
116-
|| $command instanceof CreateNodeAggregateWithNodeAndSerializedProperties
117-
|| $command instanceof MoveNodeAggregate
118-
|| $command instanceof CreateNodeVariant
119-
|| $command instanceof CreateRootNodeAggregateWithNode
120-
|| $command instanceof DisableNodeAggregate
121-
|| $command instanceof EnableNodeAggregate
122-
|| $command instanceof ChangeNodeAggregateName;
109+
return method_exists($this, 'handle' . (new \ReflectionClass($command))->getShortName());
123110
}
124111

125-
/** @codingStandardsIgnoreStart */
126112
public function handle(CommandInterface $command, ContentRepository $contentRepository): EventsToPublish
127113
{
128114
// @phpstan-ignore-next-line
@@ -143,7 +129,6 @@ public function handle(CommandInterface $command, ContentRepository $contentRepo
143129
ChangeNodeAggregateName::class => $this->handleChangeNodeAggregateName($command),
144130
};
145131
}
146-
/** @codingStandardsIgnoreStop */
147132

148133
protected function getNodeTypeManager(): NodeTypeManager
149134
{
@@ -253,4 +238,5 @@ protected function checkConstraintsImposedByAncestors(ChangeNodeAggregateType $c
253238
}
254239
}
255240
}
241+
256242
}

Neos.ContentRepository.Core/Classes/Feature/NodeCreation/NodeCreation.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,31 @@
1515
namespace Neos\ContentRepository\Core\Feature\NodeCreation;
1616

1717
use Neos\ContentRepository\Core\ContentRepository;
18+
use Neos\ContentRepository\Core\DimensionSpace;
1819
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePointSet;
1920
use Neos\ContentRepository\Core\EventStore\Events;
2021
use Neos\ContentRepository\Core\EventStore\EventsToPublish;
22+
use Neos\ContentRepository\Core\Feature\Common\NodeAggregateEventPublisher;
2123
use Neos\ContentRepository\Core\Feature\ContentStreamEventStreamName;
22-
use Neos\ContentRepository\Core\NodeType\NodeType;
23-
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
24-
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
25-
use Neos\ContentRepository\Core\Projection\ContentGraph\NodePath;
26-
use Neos\ContentRepository\Core\NodeType\NodeTypeName;
27-
use Neos\ContentRepository\Core\SharedModel\Exception\NodeTypeNotFoundException;
28-
use Neos\ContentRepository\Core\Infrastructure\Property\PropertyConverter;
29-
use Neos\ContentRepository\Core\DimensionSpace;
30-
use Neos\ContentRepository\Core\SharedModel\Exception\ContentStreamDoesNotExistYet;
3124
use Neos\ContentRepository\Core\Feature\NodeCreation\Command\CreateNodeAggregateWithNode;
32-
33-
/** @codingStandardsIgnoreStart */
3425
use Neos\ContentRepository\Core\Feature\NodeCreation\Command\CreateNodeAggregateWithNodeAndSerializedProperties;
35-
/** @codingStandardsIgnoreEnd */
36-
use Neos\ContentRepository\Core\Feature\NodeCreation\Event\NodeAggregateWithNodeWasCreated;
37-
use Neos\ContentRepository\Core\SharedModel\Exception\PropertyCannotBeSet;
38-
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateClassification;
39-
use Neos\ContentRepository\Core\Feature\Common\NodeAggregateEventPublisher;
4026
use Neos\ContentRepository\Core\Feature\NodeCreation\Dto\NodeAggregateIdsByNodePaths;
41-
use Neos\ContentRepository\Core\SharedModel\Node\PropertyName;
27+
use Neos\ContentRepository\Core\Feature\NodeCreation\Event\NodeAggregateWithNodeWasCreated;
28+
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyValuesToWrite;
4229
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\SerializedPropertyValue;
4330
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\SerializedPropertyValues;
44-
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyValuesToWrite;
31+
use Neos\ContentRepository\Core\Infrastructure\Property\PropertyConverter;
4532
use Neos\ContentRepository\Core\Infrastructure\Property\PropertyType;
33+
use Neos\ContentRepository\Core\NodeType\NodeType;
34+
use Neos\ContentRepository\Core\NodeType\NodeTypeName;
35+
use Neos\ContentRepository\Core\Projection\ContentGraph\NodePath;
36+
use Neos\ContentRepository\Core\SharedModel\Exception\ContentStreamDoesNotExistYet;
37+
use Neos\ContentRepository\Core\SharedModel\Exception\NodeTypeNotFoundException;
38+
use Neos\ContentRepository\Core\SharedModel\Exception\PropertyCannotBeSet;
39+
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateClassification;
40+
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
41+
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
42+
use Neos\ContentRepository\Core\SharedModel\Node\PropertyName;
4643
use Neos\EventStore\Model\EventStream\ExpectedVersion;
4744

4845
/**

Neos.ContentRepository.Core/Classes/Feature/NodeDuplication/NodeDuplicationCommandHandler.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use Neos\ContentRepository\Core\Feature\Common\ConstraintChecks;
3333
use Neos\ContentRepository\Core\Feature\Common\NodeAggregateEventPublisher;
3434
use Neos\ContentRepository\Core\DimensionSpace\OriginDimensionSpacePoint;
35-
use Neos\ContentRepository\Core\Feature\NodeDuplication\Dto\NodeAggregateIdMapping;
3635
use Neos\ContentRepository\Core\SharedModel\User\UserId;
3736
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
3837
use Neos\ContentRepository\Core\Feature\NodeDuplication\Command\CopyNodesRecursively;
@@ -62,19 +61,16 @@ protected function getAllowedDimensionSubspace(): DimensionSpacePointSet
6261
return $this->contentDimensionZookeeper->getAllowedDimensionSubspace();
6362
}
6463

65-
6664
public function canHandle(CommandInterface $command): bool
6765
{
68-
return $command instanceof CopyNodesRecursively;
66+
return method_exists($this, 'handle' . (new \ReflectionClass($command))->getShortName());
6967
}
7068

7169
public function handle(CommandInterface $command, ContentRepository $contentRepository): EventsToPublish
7270
{
73-
if ($command instanceof CopyNodesRecursively) {
74-
return $this->handleCopyNodesRecursively($command, $contentRepository);
75-
}
76-
77-
throw new \RuntimeException('Command not supported');
71+
return match ($command::class) {
72+
CopyNodesRecursively::class => $this->handleCopyNodesRecursively($command, $contentRepository),
73+
};
7874
}
7975

8076
/**

Neos.ContentRepository.Core/Classes/Feature/NodeModification/NodeModification.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Neos\ContentRepository\Core\Feature\NodeModification\Event\NodePropertiesWereSet;
2929
use Neos\ContentRepository\Core\Feature\Common\NodeAggregateEventPublisher;
3030
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyScope;
31-
use Neos\ContentRepository\Core\SharedModel\Node\ReadableNodeAggregateInterface;
3231
use Neos\EventStore\Model\EventStream\ExpectedVersion;
3332

3433
/**

Neos.ContentRepository.Core/Classes/Feature/NodeTypeChange/NodeTypeChange.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,31 @@
1616

1717
use Neos\ContentRepository\Core\ContentRepository;
1818
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePointSet;
19+
use Neos\ContentRepository\Core\DimensionSpace\OriginDimensionSpacePointSet;
1920
use Neos\ContentRepository\Core\EventStore\Events;
2021
use Neos\ContentRepository\Core\EventStore\EventsToPublish;
21-
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeAggregate;
22-
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
23-
use Neos\ContentRepository\Core\Projection\ContentGraph\NodePath;
24-
use Neos\ContentRepository\Core\NodeType\NodeType;
25-
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
26-
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
27-
use Neos\ContentRepository\Core\DimensionSpace\OriginDimensionSpacePointSet;
28-
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
29-
use Neos\ContentRepository\Core\SharedModel\Exception\NodeConstraintException;
30-
use Neos\ContentRepository\Core\SharedModel\Exception\NodeTypeNotFoundException;
22+
use Neos\ContentRepository\Core\Feature\Common\NodeAggregateEventPublisher;
3123
use Neos\ContentRepository\Core\Feature\ContentStreamEventStreamName;
24+
use Neos\ContentRepository\Core\Feature\NodeCreation\Dto\NodeAggregateIdsByNodePaths;
25+
use Neos\ContentRepository\Core\Feature\NodeRemoval\Event\NodeAggregateWasRemoved;
3226
use Neos\ContentRepository\Core\Feature\NodeTypeChange\Command\ChangeNodeAggregateType;
27+
use Neos\ContentRepository\Core\Feature\NodeTypeChange\Dto\NodeAggregateTypeChangeChildConstraintConflictResolutionStrategy;
3328
use Neos\ContentRepository\Core\Feature\NodeTypeChange\Event\NodeAggregateTypeWasChanged;
34-
use Neos\ContentRepository\Core\Feature\NodeRemoval\Event\NodeAggregateWasRemoved;
29+
use Neos\ContentRepository\Core\NodeType\NodeType;
30+
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
31+
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeAggregate;
32+
use Neos\ContentRepository\Core\Projection\ContentGraph\NodePath;
33+
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
3534
use Neos\ContentRepository\Core\SharedModel\Exception\NodeAggregatesTypeIsAmbiguous;
35+
use Neos\ContentRepository\Core\SharedModel\Exception\NodeConstraintException;
3636
use Neos\ContentRepository\Core\SharedModel\Exception\NodeTypeNotFound;
37-
use Neos\ContentRepository\Core\Feature\Common\NodeAggregateEventPublisher;
38-
use Neos\ContentRepository\Core\Feature\NodeCreation\Dto\NodeAggregateIdsByNodePaths;
39-
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
37+
use Neos\ContentRepository\Core\SharedModel\Exception\NodeTypeNotFoundException;
38+
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
39+
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
4040
use Neos\ContentRepository\Core\SharedModel\User\UserId;
41+
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
4142
use Neos\EventStore\Model\EventStream\ExpectedVersion;
4243

43-
/** @codingStandardsIgnoreStart */
44-
use Neos\ContentRepository\Core\Feature\NodeTypeChange\Dto\NodeAggregateTypeChangeChildConstraintConflictResolutionStrategy;
45-
/** @codingStandardsIgnoreEnd */
46-
4744
/**
4845
* @internal implementation detail of Command Handlers
4946
*/
@@ -419,7 +416,7 @@ private function deleteObsoleteTetheredNodesWhenChangingNodeType(
419416
* Example: In this case, we want to find exactly the bold edge between PAR1 and A.
420417
*
421418
* ╔══════╗ <------ $parentNodeAggregate (PAR1)
422-
* ┌──────┐ ║ PAR1║ ┌──────┐
419+
* ┌──────┐ ║ PAR1 ║ ┌──────┐
423420
* │ PAR3 │ ╚══════╝ │ PAR2 │
424421
* └──────┘ ║ └──────┘
425422
* ╲ ║ ╱

0 commit comments

Comments
 (0)