1 | <?php declare(strict_types=1); |
||
2 | /** |
||
3 | * This file is part of the daikon-cqrs/event-sourcing project. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | */ |
||
8 | |||
9 | namespace Daikon\EventSourcing\Aggregate\Command; |
||
10 | |||
11 | use Daikon\EventSourcing\Aggregate\AggregateIdInterface; |
||
12 | use Daikon\EventSourcing\Aggregate\AggregateRevision; |
||
13 | use Daikon\EventSourcing\Aggregate\AggregateRootInterface; |
||
14 | use Daikon\EventSourcing\EventStore\UnitOfWorkInterface; |
||
15 | use Daikon\Interop\RuntimeException; |
||
16 | use Daikon\MessageBus\Channel\Subscription\MessageHandler\MessageHandlerInterface; |
||
17 | use Daikon\MessageBus\EnvelopeInterface; |
||
18 | use Daikon\MessageBus\MessageBusInterface; |
||
19 | use Daikon\Metadata\MetadataInterface; |
||
20 | use ReflectionClass; |
||
21 | |||
22 | abstract class CommandHandler implements MessageHandlerInterface |
||
23 | { |
||
24 | private const COMMITS_CHANNEL = 'commits'; |
||
25 | |||
26 | private MessageBusInterface $messageBus; |
||
27 | |||
28 | private UnitOfWorkInterface $unitOfWork; |
||
29 | |||
30 | 1 | public function __construct(UnitOfWorkInterface $unitOfWork, MessageBusInterface $messageBus) |
|
31 | { |
||
32 | 1 | $this->messageBus = $messageBus; |
|
33 | 1 | $this->unitOfWork = $unitOfWork; |
|
34 | 1 | } |
|
35 | |||
36 | 1 | public function handle(EnvelopeInterface $envelope): void |
|
37 | { |
||
38 | 1 | $commandMessage = $envelope->getMessage(); |
|
39 | 1 | $handlerName = (new ReflectionClass($commandMessage))->getShortName(); |
|
40 | 1 | $handlerMethod = 'handle'.ucfirst($handlerName); |
|
41 | 1 | $handler = [$this, $handlerMethod]; |
|
42 | 1 | if (!is_callable($handler)) { |
|
43 | throw new RuntimeException( |
||
44 | sprintf("Handler '%s' is not callable on '%s'.", $handlerMethod, static::class) |
||
45 | ); |
||
46 | } |
||
47 | 1 | $this->commit(...$handler($commandMessage, $envelope->getMetadata())); |
|
0 ignored issues
–
show
|
|||
48 | 1 | } |
|
49 | |||
50 | 1 | protected function commit(AggregateRootInterface $aggregateRoot, MetadataInterface $metadata): void |
|
51 | { |
||
52 | 1 | $newCommits = $this->unitOfWork->commit($aggregateRoot, $metadata); |
|
53 | 1 | foreach ($newCommits as $newCommit) { |
|
54 | 1 | $this->messageBus->publish($newCommit, self::COMMITS_CHANNEL, $metadata); |
|
55 | } |
||
56 | 1 | } |
|
57 | |||
58 | protected function checkout( |
||
59 | AggregateIdInterface $aggregateId, |
||
60 | AggregateRevision $knownAggregateRevision |
||
61 | ): AggregateRootInterface { |
||
62 | return $this->unitOfWork->checkout($aggregateId, $knownAggregateRevision); |
||
63 | } |
||
64 | } |
||
65 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.