Passed
Push — master ( 977f82...f4c7d5 )
by Mr
07:24
created

CommandHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 41
ccs 17
cts 21
cp 0.8095
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A commit() 0 5 2
A checkout() 0 5 1
A handle() 0 12 2
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
Bug introduced by
The call to Daikon\EventSourcing\Agg...ommandHandler::commit() has too few arguments starting with metadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        $this->/** @scrutinizer ignore-call */ 
48
               commit(...$handler($commandMessage, $envelope->getMetadata()));

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.

Loading history...
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