unserializePayload()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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