TypeValidatingPayloadSerializer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A unserializePayload() 0 7 2
A serializePayload() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing\Serialization;
6
7
final class TypeValidatingPayloadSerializer implements PayloadSerializer
8
{
9
    public function __construct(
10
        private PayloadSerializer $serializer,
11
        private string $eventClassName
12
    ) {
13
    }
14
15
    public function serializePayload(object $event): array
16
    {
17
        if ( ! $event instanceof $this->eventClassName) {
18
            throw new \InvalidArgumentException(sprintf('Cannot serialize event that does not implement "%s".', $this->eventClassName));
19 5
        }
20
21
        return $this->serializer->serializePayload($event);
22
    }
23 5
24 5
    public function unserializePayload(string $className, array $payload): object
25 5
    {
26
        if ( ! is_subclass_of($className, $this->eventClassName)) {
27 2
            throw new \InvalidArgumentException(sprintf('Cannot unserialize payload into an event that does not implement "%s".', $this->eventClassName));
28
        }
29 2
30 1
        return $this->serializer->unserializePayload($className, $payload);
31
    }
32
}
33