Passed
Push — master ( 6d1632...ddd5df )
by Thorsten
01:48
created

CommandHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 44
ccs 17
cts 20
cp 0.85
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 11 2
A commit() 0 10 4
A checkout() 0 6 1
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/cqrs 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
declare(strict_types=1);
10
11
namespace Daikon\EventSourcing\Aggregate;
12
13
use Daikon\EventSourcing\EventStore\CommitInterface;
14
use Daikon\EventSourcing\EventStore\StreamRevision;
15
use Daikon\EventSourcing\EventStore\UnitOfWorkInterface;
16
use Daikon\MessageBus\Channel\Subscription\MessageHandler\MessageHandlerInterface;
17
use Daikon\MessageBus\EnvelopeInterface;
18
use Daikon\MessageBus\MessageBusInterface;
19
use Daikon\MessageBus\Metadata\Metadata;
20
21
abstract class CommandHandler implements MessageHandlerInterface
22
{
23
    /** @var MessageBusInterface */
24
    private $messageBus;
25
26
    /** @var UnitOfWorkInterface */
27
    private $unitOfWork;
28
29 2
    public function __construct(UnitOfWorkInterface $unitOfWork, MessageBusInterface $messageBus)
30
    {
31 2
        $this->messageBus = $messageBus;
32 2
        $this->unitOfWork = $unitOfWork;
33 2
    }
34
35 2
    public function handle(EnvelopeInterface $envelope): bool
36
    {
37 2
        $commandMessage = $envelope->getMessage();
38 2
        $handlerName = (new \ReflectionClass($commandMessage))->getShortName();
39 2
        $handlerMethod = "handle".ucfirst($handlerName);
40 2
        $handler = [ $this, $handlerMethod ];
41 2
        if (!is_callable($handler)) {
42
            throw new \Exception("Handler '$handlerMethod' isn't callable on ".static::class);
43
        }
44 2
        return call_user_func($handler, $commandMessage, $envelope->getMetadata());
45
    }
46
47 2
    protected function commit(AggregateRootInterface $aggregateRoot, Metadata $metadata): bool
48
    {
49 2
        $committed = false;
50 2
        foreach ($this->unitOfWork->commit($aggregateRoot, $metadata) as $newCommit) {
51 1
            if ($this->messageBus->publish($newCommit, "commits") && !$committed) {
52 1
                $committed = true;
53
            }
54
        }
55 2
        return $committed;
56
    }
57
58
    protected function checkout(
59
        AggregateIdInterface $aggregateId,
60
        AggregateRevision $revision = null
61
    ): AggregateRootInterface {
62
        return $this->unitOfWork->checkout($aggregateId, $revision);
0 ignored issues
show
Bug introduced by
It seems like $revision defined by parameter $revision on line 60 can be null; however, Daikon\EventSourcing\Eve...rkInterface::checkout() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
63
    }
64
}
65