Passed
Pull Request — master (#84)
by Frank
14:11 queued 04:11
created

Message::withTimeOfRecording()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing;
6
7
use DateTimeImmutable;
8
use RuntimeException;
9
use function assert;
10
11
final class Message
12
{
13
    private const TIME_OF_RECORDING_FORMAT = 'Y-m-d H:i:s.uO';
14
15
    public function __construct(private object $event, private array $headers = [])
16
    {
17
    }
18
19
    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...
20
    {
21 25
        $clone = clone $this;
22
        $clone->headers[$key] = $value;
23 25
24 25
        return $clone;
25 25
    }
26
27
    public function withHeaders(array $headers): Message
28
    {
29
        $clone = clone $this;
30 5
        $clone->headers = $headers + $clone->headers;
31
32 5
        return $clone;
33 5
    }
34
35 5
    public function withTimeOfRecording(DateTimeImmutable $timeOfRecording): Message
36
    {
37
        return $this->withHeader(Header::TIME_OF_RECORDING, $timeOfRecording->format(self::TIME_OF_RECORDING_FORMAT));
38 14
    }
39
40 14
    public function aggregateVersion(): int
41 14
    {
42
        $version = $this->headers[Header::AGGREGATE_ROOT_VERSION] ?? null;
43 14
44
        if (null === $version) {
45
            throw new RuntimeException("Can't get the version if the message has none.");
46 7
        }
47
48 7
        return (int) $version;
49
    }
50 7
51 1
    public function aggregateRootId(): ?AggregateRootId
52
    {
53
        return $this->headers[Header::AGGREGATE_ROOT_ID] ?? null;
54 6
    }
55
56
    public function timeOfRecording(): DateTimeImmutable
57 1
    {
58
        /* @var DateTimeImmutable */
59 1
        $timeOfRecording = DateTimeImmutable::createFromFormat(
60
            self::TIME_OF_RECORDING_FORMAT,
61
            $this->headers[Header::TIME_OF_RECORDING] ?? ''
62 1
        );
63
64 1
        assert(
65
            $timeOfRecording instanceof DateTimeImmutable,
66
            'Your messages are not being decorated with the default headers, please ' .
67
            'ensure your messages are decorated with a time of recording.'
68
        );
69
70 9
        return $timeOfRecording;
71
    }
72 9
73
    public function header(string $key): int|string|array|AggregateRootId|null
74
    {
75 12
        return $this->headers[$key] ?? null;
76
    }
77 12
78
    public function headers(): array
79
    {
80 20
        return $this->headers;
81
    }
82 20
83
    public function event(): object
84
    {
85
        return $this->event;
86
    }
87
}
88