JsonSerializer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 30
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 4 1
A unserialize() 0 10 2
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
}