Completed
Pull Request — master (#16)
by Frank
02:46
created

DefaultHeadersDecorator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A decorate() 0 16 2
1
<?php
2
3
namespace EventSauce\EventSourcing;
4
5
class DefaultHeadersDecorator implements MessageDecorator
6
{
7
    /**
8
     * @var ClassNameInflector
9
     */
10
    private $inflector;
11
12 10
    public function __construct(ClassNameInflector $inflector = null)
13
    {
14 10
        $this->inflector = $inflector ?: new DotSeparatedSnakeCaseInflector();
15 10
    }
16
17 5
    public function decorate(Message $message): Message
18
    {
19 5
        $event = $message->event();
20 5
        $id = $message->header(Header::AGGREGATE_ROOT_ID);
21
        $headers = [
22 5
            Header::EVENT_TYPE        => $this->inflector->instanceToType($event),
23 5
            Header::TIME_OF_RECORDING => $event->timeOfRecording()->toString(),
24
        ];
25
26 5
        if ($id instanceof AggregateRootId) {
27 3
            $headers[Header::AGGREGATE_ROOT_ID] = $id->toString();
28 3
            $headers[Header::AGGREGATE_ROOT_ID_TYPE] = $this->inflector->instanceToType($id);
29
        }
30
31 5
        return $message->withHeaders($headers);
32
    }
33
}