Completed
Push — master ( 420d6f...d26b40 )
by Frank
02:21
created

Message::aggregateVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 15
    public function __construct(object $event, array $headers = [])
22
    {
23 15
        $this->event = $event;
24 15
        $this->headers = $headers;
25 15
    }
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 7
    public function withHeaders(array $headers): Message
36
    {
37 7
        $clone = clone $this;
38 7
        $clone->headers = $headers + $clone->headers;
39
40 7
        return $clone;
41
    }
42
43 4
    public function aggregateVersion(): int
44
    {
45 4
        $version = $this->headers[Header::AGGREGATE_ROOT_VERSION] ?? null;
46
47 4
        if ($version === null) {
48 1
            throw new RuntimeException("Can't get the version if the message has none.");
49
        }
50
51 3
        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 4
    public function header(string $key)
60
    {
61 4
        return $this->headers[$key] ?? null;
62
    }
63
64 8
    public function headers(): array
65
    {
66 8
        return $this->headers;
67
    }
68
69 11
    public function event(): object
70
    {
71 11
        return $this->event;
72
    }
73
}
74