Passed
Push — master ( aa1cc2...059b9c )
by butschster
14:47 queued 03:34
created

ProtoSerializer::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Serializer\Serializer;
6
7
use Spiral\Serializer\Exception\InvalidArgumentException;
8
use Spiral\Serializer\Exception\SerializerException;
9
use Spiral\Serializer\Exception\UnserializeException;
10
use Spiral\Serializer\SerializerInterface;
11
use Google\Protobuf\Internal\Message;
12
13
final class ProtoSerializer implements SerializerInterface
14
{
15 327
    public function __construct()
16
    {
17 327
        if (!\class_exists(Message::class)) {
18
            throw new SerializerException('Package `google/protobuf` is not installed.');
19
        }
20
    }
21
22
    /**
23
     * @throws InvalidArgumentException
24
     */
25 3
    public function serialize(mixed $payload): string|\Stringable
26
    {
27 3
        if (!$payload instanceof Message) {
28 1
            throw new InvalidArgumentException(\sprintf(
29 1
                'Payload must be of type `%s`, received `%s`.',
30 1
                Message::class,
31 1
                \get_debug_type($payload)
32 1
            ));
33
        }
34
35 2
        return $payload->serializeToString();
36
    }
37
38
    /**
39
     * @throws UnserializeException
40
     * @throws InvalidArgumentException
41
     */
42 5
    public function unserialize(\Stringable|string $payload, object|string|null $type = null): mixed
43
    {
44 5
        if (\is_object($type)) {
45 2
            $type = $type::class;
46
        }
47
48 5
        if ($type === null || !\class_exists($type) || !\is_a($type, Message::class, true)) {
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type object; however, parameter $class of class_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        if ($type === null || !\class_exists(/** @scrutinizer ignore-type */ $type) || !\is_a($type, Message::class, true)) {
Loading history...
49 4
            throw new InvalidArgumentException(\sprintf(
50 4
                'Parameter `$type` must be of type: `%s`, received `%s`.',
51 4
                Message::class,
52 4
                \get_debug_type($type)
53 4
            ));
54
        }
55
56 1
        $object = new $type();
57
58
        try {
59 1
            $object->mergeFromString((string) $payload);
60
        } catch (\Throwable $e) {
61
            throw new UnserializeException(
62
                \sprintf('Failed to unserialize data: %s', $e->getMessage()),
63
                $e->getCode(),
64
                $e
65
            );
66
        }
67
68 1
        return $object;
69
    }
70
}
71