Passed
Push — master ( 123c3e...98b08a )
by Frank
10:17 queued 22s
created

Message::header()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing;
6
7
use RuntimeException;
8
9
final class Message
10
{
11
    /**
12
     * @var object
13
     */
14
    private $event;
15
16
    /**
17
     * @var array
18
     */
19
    private $headers;
20
21 24
    public function __construct(object $event, array $headers = [])
22
    {
23 24
        $this->event = $event;
24 24
        $this->headers = $headers;
25 24
    }
26
27 4
    public function withHeader(string $key, $value): Message
28
    {
29 4
        $clone = clone $this;
30 4
        $clone->headers[$key] = $value;
31
32 4
        return $clone;
33
    }
34
35 14
    public function withHeaders(array $headers): Message
36
    {
37 14
        $clone = clone $this;
38 14
        $clone->headers = $headers + $clone->headers;
39
40 14
        return $clone;
41
    }
42
43 7
    public function aggregateVersion(): int
44
    {
45 7
        $version = $this->headers[Header::AGGREGATE_ROOT_VERSION] ?? null;
46
47 7
        if (null === $version) {
48 1
            throw new RuntimeException("Can't get the version if the message has none.");
49
        }
50
51 6
        return (int) $version;
52
    }
53
54 1
    public function aggregateRootId(): ?AggregateRootId
55
    {
56 1
        return $this->headers[Header::AGGREGATE_ROOT_ID] ?? null;
57
    }
58
59 9
    public function timeOfRecording(): PointInTime
60
    {
61 9
        return PointInTime::fromString($this->headers[Header::TIME_OF_RECORDING]);
62
    }
63
64 12
    public function header(string $key)
65
    {
66 12
        return $this->headers[$key] ?? null;
67
    }
68
69 20
    public function headers(): array
70
    {
71 20
        return $this->headers;
72
    }
73
74
    public function event(): object
75
    {
76
        return $this->event;
77
    }
78
}
79