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

DefaultHeadersDecorator::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 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
}