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

Message   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 20
c 3
b 0
f 0
dl 0
loc 68
ccs 25
cts 25
cp 1
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A aggregateVersion() 0 9 2
A timeOfRecording() 0 3 1
A withHeader() 0 6 1
A __construct() 0 4 1
A aggregateRootId() 0 3 1
A withHeaders() 0 6 1
A header() 0 3 1
A event() 0 3 1
A headers() 0 3 1
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