Passed
Pull Request — master (#214)
by Viktor
02:55
created

JsonMessageSerializer::unserialize()   C

Complexity

Conditions 12
Paths 15

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 12.1081

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 21
nc 15
nop 1
dl 0
loc 40
ccs 20
cts 22
cp 0.9091
crap 12.1081
rs 6.9666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Queue\Message;
6
7
use InvalidArgumentException;
8
use JsonException;
9
10
final class JsonMessageSerializer implements MessageSerializerInterface
11
{
12
    /**
13
     * @throws JsonException
14
     */
15 4
    public function serialize(MessageInterface $message): string
16
    {
17 4
        $payload = [
18 4
            'name' => $message->getHandlerName(),
19 4
            'data' => $message->getData(),
20 4
            'meta' => $message->getMetadata(),
21 4
        ];
22 4
        if (!isset($payload['meta']['message-class'])) {
23 4
            $payload['meta']['message-class'] = $message instanceof EnvelopeInterface
24 2
                ? $message->getMessage()::class
25 2
                : $message::class;
26
        }
27
28 4
        return json_encode($payload, JSON_THROW_ON_ERROR);
29
    }
30
31
    /**
32
     * @throws JsonException
33
     * @throws InvalidArgumentException
34
     */
35 13
    public function unserialize(string $value): MessageInterface
36
    {
37 13
        $payload = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
38 13
        if (!is_array($payload)) {
39 4
            throw new InvalidArgumentException('Payload must be array. Got ' . get_debug_type($payload) . '.');
40
        }
41
42 9
        $name = $payload['name'] ?? null;
43 9
        if (!isset($name) || !is_string($name)) {
44
            throw new InvalidArgumentException('Handler name must be a string. Got ' . get_debug_type($name) . '.');
45
        }
46
47 9
        $meta = $payload['meta'] ?? [];
48 9
        if (!is_array($meta)) {
49 3
            throw new InvalidArgumentException('Metadata must be an array. Got ' . get_debug_type($meta) . '.');
50
        }
51
52 6
        $envelopes = [];
53 6
        if (isset($meta[EnvelopeInterface::ENVELOPE_STACK_KEY]) && is_array($meta[EnvelopeInterface::ENVELOPE_STACK_KEY])) {
54 3
            $envelopes = $meta[EnvelopeInterface::ENVELOPE_STACK_KEY];
55
        }
56 6
        $meta[EnvelopeInterface::ENVELOPE_STACK_KEY] = [];
57
58 6
        $class = $payload['meta']['message-class'] ?? Message::class;
59 6
        if (!is_subclass_of($class, MessageInterface::class)) {
60
            $class = Message::class;
61
        }
62
63
        /**
64
         * @var class-string<MessageInterface> $class
65
         */
66 6
        $message = $class::fromData($name, $payload['data'] ?? null, $meta);
67
68 6
        foreach ($envelopes as $envelope) {
69 3
            if (is_string($envelope) && class_exists($envelope) && is_subclass_of($envelope, EnvelopeInterface::class)) {
70 3
                $message = $envelope::fromMessage($message);
71
            }
72
        }
73
74 6
        return $message;
75
    }
76
}
77