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

Message   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 63
ccs 25
cts 25
cp 1
rs 10
c 2
b 0
f 0
wmc 9

8 Methods

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