JsonSerializer::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tasksuki\Component\JsonSerializer;
4
5
use Tasksuki\Component\Message\Message;
6
use Tasksuki\Component\Serializer\Exception\NotValidInputException;
7
use Tasksuki\Component\Serializer\Exception\SerializerException;
8
use Tasksuki\Component\Serializer\SerializerInterface;
9
10
/**
11
 * Class JsonSerializer
12
 *
13
 * @package Tasksuki\Component\JsonSerializer
14
 * @author  Aurimas Niekis <[email protected]>
15
 */
16
class JsonSerializer implements SerializerInterface
17
{
18
    /**
19
     * @param Message $message
20
     *
21
     * @return string
22
     */
23 1
    public function serialize(Message $message): string
24
    {
25 1
        return json_encode($message);
26
    }
27
28
    /**
29
     * @param string $data
30
     *
31
     * @return Message
32
     * @throws NotValidInputException
33
     */
34 2
    public function unserialize(string $data): Message
35
    {
36 2
        $data = json_decode($data, true);
37
38 2
        if (JSON_ERROR_NONE !== json_last_error()) {
39 1
            throw new NotValidInputException(json_last_error_msg());
40
        }
41
42 1
        return Message::fromArray($data);
43
    }
44
45
}