Message   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 73
ccs 27
cts 27
cp 1
rs 10
c 2
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A header() 0 3 1
A event() 0 3 1
A headers() 0 3 1
A withHeader() 0 6 1
A aggregateVersion() 0 9 2
A __construct() 0 2 1
A timeOfRecording() 0 13 2
A withTimeOfRecording() 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
    public const TIME_OF_RECORDING_FORMAT = 'Y-m-d H:i:s.uO';
13
14
    public function __construct(private object $event, private array $headers = [])
15
    {
16
    }
17
18
    public function withHeader(string $key, int|string|null|AggregateRootId $value): Message
0 ignored issues
show
Bug introduced by
The type EventSauce\EventSourcing\null was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
    {
20
        $clone = clone $this;
21 25
        $clone->headers[$key] = $value;
22
23 25
        return $clone;
24 25
    }
25 25
26
    public function withHeaders(array $headers): Message
27
    {
28
        $clone = clone $this;
29
        $clone->headers = $headers + $clone->headers;
30 5
31
        return $clone;
32 5
    }
33 5
34
    public function withTimeOfRecording(DateTimeImmutable $timeOfRecording): Message
35 5
    {
36
        return $this->withHeader(Header::TIME_OF_RECORDING, $timeOfRecording->format(self::TIME_OF_RECORDING_FORMAT));
37
    }
38 14
39
    public function aggregateVersion(): int
40 14
    {
41 14
        $version = $this->headers[Header::AGGREGATE_ROOT_VERSION] ?? null;
42
43 14
        if (null === $version) {
44
            throw new RuntimeException("Can't get the version if the message has none.");
45
        }
46 7
47
        return (int) $version;
48 7
    }
49
50 7
    public function aggregateRootId(): ?AggregateRootId
51 1
    {
52
        return $this->headers[Header::AGGREGATE_ROOT_ID] ?? null;
53
    }
54 6
55
    public function timeOfRecording(): DateTimeImmutable
56
    {
57 1
        /* @var DateTimeImmutable */
58
        $timeOfRecording = DateTimeImmutable::createFromFormat(
59 1
            self::TIME_OF_RECORDING_FORMAT,
60
            $header = ($this->headers[Header::TIME_OF_RECORDING] ?? '')
61
        );
62 1
63
        if ( ! $timeOfRecording instanceof DateTimeImmutable) {
0 ignored issues
show
introduced by
$timeOfRecording is always a sub-type of DateTimeImmutable.
Loading history...
64 1
            throw UnableToResolveTimeOfRecording::fromFormatAndHeader(self::TIME_OF_RECORDING_FORMAT, $header);
65
        }
66
67
        return $timeOfRecording;
68
    }
69
70 9
    public function header(string $key): int|string|array|AggregateRootId|null
71
    {
72 9
        return $this->headers[$key] ?? null;
73
    }
74
75 12
    public function headers(): array
76
    {
77 12
        return $this->headers;
78
    }
79
80 20
    public function event(): object
81
    {
82 20
        return $this->event;
83
    }
84
}
85