MessageSerializer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 24
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 10 1
A unserialize() 0 27 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Queue\AMQP;
6
7
use InvalidArgumentException;
8
use JsonException;
9
use Yiisoft\Queue\AMQP\Exception\NoKeyInPayloadException;
10
use Yiisoft\Queue\Message\Message;
11
use Yiisoft\Queue\Message\MessageInterface;
12
13
class MessageSerializer implements MessageSerializerInterface
14
{
15
    /**
16
     * @throws JsonException
17
     */
18
    public function serialize(MessageInterface $message): string
19
    {
20
        $payload = [
21
            'id' => $message->getId(),
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Yiisoft\Queue\Message\MessageInterface. It seems like you code against a sub-type of Yiisoft\Queue\Message\MessageInterface such as Yiisoft\Queue\Message\IdEnvelope. ( Ignorable by Annotation )

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

21
            'id' => $message->/** @scrutinizer ignore-call */ getId(),
Loading history...
22
            'name' => $message->getHandlerName(),
23
            'data' => $message->getData(),
24
            'meta' => $message->getMetadata(),
25
        ];
26
27
        return json_encode($payload, JSON_THROW_ON_ERROR);
28
    }
29
30
    /**
31
     * @throws JsonException
32
     * @throws NoKeyInPayloadException
33
     * @throws InvalidArgumentException
34
     */
35
    public function unserialize(string $value): Message
36
    {
37
        $payload = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
38
        if (!is_array($payload)) {
39
            throw new InvalidArgumentException('Payload must be array. Got ' . get_debug_type($payload) . '.');
40
        }
41
42
        $name = $payload['name'] ?? null;
43
        if (!is_string($name)) {
44
            throw new NoKeyInPayloadException('name', $payload);
45
        }
46
47
        $id = $payload['id'] ?? null;
48
        if ($id !== null && !is_string($id)) {
49
            throw new NoKeyInPayloadException('id', $payload);
50
        }
51
52
        $meta = $payload['meta'] ?? [];
53
        if (!is_array($meta)) {
54
            throw new NoKeyInPayloadException('meta', $payload);
55
        }
56
57
        return new Message(
58
            $name,
59
            $payload['data'] ?? null,
60
            $meta,
61
            $id,
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Queue\Message\Message::__construct() has too many arguments starting with $id. ( Ignorable by Annotation )

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

61
        return /** @scrutinizer ignore-call */ new Message(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
62
        );
63
    }
64
}
65