| Total Complexity | 11 |
| Total Lines | 85 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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( |
||
|
|
|||
| 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) |
|
| 85 | } |
||
| 86 | |||
| 87 | public function headers(): array |
||
| 88 | { |
||
| 89 | return $this->headers; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function event(): object |
||
| 95 | } |
||
| 96 | } |
||
| 97 |