ProcessManagerTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A then() 0 3 1
A handle() 0 12 2
1
<?php declare (strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot 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\Boot\Service;
10
11
use Daikon\Boot\Service\Provisioner\MessageBusProvisioner;
12
use Daikon\EventSourcing\Aggregate\Command\CommandInterface;
13
use Daikon\Interop\RuntimeException;
14
use Daikon\MessageBus\EnvelopeInterface;
15
use Daikon\Metadata\MetadataInterface;
16
use ReflectionClass;
17
18
trait ProcessManagerTrait
19
{
20
    public function handle(EnvelopeInterface $envelope): void
21
    {
22
        $message = $envelope->getMessage();
23
        $shortName = (new ReflectionClass($message))->getShortName();
24
        $handlerMethod = 'when'.ucfirst($shortName);
25
        $handler = [$this, $handlerMethod];
26
        if (!is_callable($handler)) {
27
            throw new RuntimeException(
28
                sprintf("Handler method '%s' is not callable in '%s'.", $handlerMethod, static::class)
29
            );
30
        }
31
        $handler($message, $envelope->getMetadata());
32
    }
33
34
    public function then(CommandInterface $command, MetadataInterface $metadata = null): void
35
    {
36
        $this->messageBus->publish($command, MessageBusProvisioner::COMMANDS_CHANNEL, $metadata);
37
    }
38
}
39