Passed
Pull Request — master (#84)
by Frank
09:52
created

Message   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 85
ccs 27
cts 27
cp 1
rs 10
c 2
b 0
f 0
wmc 11

10 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 timeOfRecording() 0 6 1
A event() 0 3 1
A withTimeOfRecording() 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 DateTimeImmutable;
8
use RuntimeException;
9
10
final class Message
11
{
12
    private const TIME_OF_RECORDING_FORMAT = 'Y-m-d H:i:s.uO';
13
14
    /**
15
     * @var object
16
     */
17
    private $event;
18
19
    /**
20
     * @var array
21 25
     */
22
    private $headers;
23 25
24 25
    public function __construct(object $event, array $headers = [])
25 25
    {
26
        $this->event = $event;
27
        $this->headers = $headers;
28
    }
29
30 5
    /**
31
     * @param mixed $value
32 5
     */
33 5
    public function withHeader(string $key, $value): Message
34
    {
35 5
        $clone = clone $this;
36
        $clone->headers[$key] = $value;
37
38 14
        return $clone;
39
    }
40 14
41 14
    public function withHeaders(array $headers): Message
42
    {
43 14
        $clone = clone $this;
44
        $clone->headers = $headers + $clone->headers;
45
46 7
        return $clone;
47
    }
48 7
49
    public function withTimeOfRecording(DateTimeImmutable $timeOfRecording): Message
50 7
    {
51 1
        return $this->withHeader(Header::TIME_OF_RECORDING, $timeOfRecording->format(self::TIME_OF_RECORDING_FORMAT));
52
    }
53
54 6
    public function aggregateVersion(): int
55
    {
56
        $version = $this->headers[Header::AGGREGATE_ROOT_VERSION] ?? null;
57 1
58
        if (null === $version) {
59 1
            throw new RuntimeException("Can't get the version if the message has none.");
60
        }
61
62 1
        return (int) $version;
63
    }
64 1
65
    public function aggregateRootId(): ?AggregateRootId
66
    {
67
        return $this->headers[Header::AGGREGATE_ROOT_ID] ?? null;
68
    }
69
70 9
    public function timeOfRecording(): DateTimeImmutable
71
    {
72 9
        /** @var DateTimeImmutable */
73
        return DateTimeImmutable::createFromFormat(
0 ignored issues
show
Bug Best Practice introduced by
The expression return DateTimeImmutable...er::TIME_OF_RECORDING]) could return the type false which is incompatible with the type-hinted return DateTimeImmutable. Consider adding an additional type-check to rule them out.
Loading history...
74
            self::TIME_OF_RECORDING_FORMAT,
75 12
            $this->headers[Header::TIME_OF_RECORDING]
76
        );
77 12
    }
78
79
    /**
80 20
     * @return mixed|null
81
     */
82 20
    public function header(string $key)
83
    {
84
        return $this->headers[$key] ?? null;
85
    }
86
87
    public function headers(): array
88
    {
89
        return $this->headers;
90
    }
91
92
    public function event(): object
93
    {
94
        return $this->event;
95
    }
96
}
97