Failed Conditions
Pull Request — release/3.0.0-dev (#32)
by Yo
01:53
created

JsonRpcCallSerializer::decode()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 19
ccs 7
cts 7
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\JsonRpcServer\App\Serialization;
3
4
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidRequestException;
5
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcParseErrorException;
6
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcCall;
7
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcCallResponse;
8
9
/**
10
 * Class JsonRpcCallSerializer
11
 */
12
class JsonRpcCallSerializer
13
{
14
    /** @var JsonRpcCallDenormalizer */
15
    private $callDenormalizer;
16
    /** @var JsonRpcCallResponseNormalizer */
17
    private $callResponseNormalizer;
18
19
    /**
20
     * @param JsonRpcCallDenormalizer       $callDenormalizer
21
     * @param JsonRpcCallResponseNormalizer $callResponseNormalizer
22
     */
23 42
    public function __construct(
24
        JsonRpcCallDenormalizer $callDenormalizer,
25
        JsonRpcCallResponseNormalizer $callResponseNormalizer
26
    ) {
27 42
        $this->callDenormalizer = $callDenormalizer;
28 42
        $this->callResponseNormalizer = $callResponseNormalizer;
29 42
    }
30
31
    /**
32
     * @param string $content
33
     *
34
     * @return JsonRpcCall
35
     */
36 7
    public function deserialize(string $content) : JsonRpcCall
37
    {
38 7
        return $this->denormalize(
39 7
            $this->decode($content)
40
        );
41
    }
42
43
    /**
44
     * @param JsonRpcCallResponse $jsonRpcCallResponse
45
     *
46
     * @return string
47
     */
48 7
    public function serialize(JsonRpcCallResponse $jsonRpcCallResponse) : string
49
    {
50 7
        return $this->encode(
51 7
            $this->normalize($jsonRpcCallResponse)
52
        );
53
    }
54
55
    /**
56
     * @param mixed $normalizedContent Could be an array or null for instance
57
     *
58
     * @return string
59
     */
60 8
    public function encode($normalizedContent) : string
61
    {
62 8
        return json_encode($normalizedContent);
63
    }
64
65
    /**
66
     * @param string $requestContent
67
     *
68
     * @return array Decoded content
69
     *
70
     * @throws JsonRpcParseErrorException
71
     * @throws JsonRpcInvalidRequestException
72
     */
73 20
    public function decode(string $requestContent) : array
74
    {
75 20
        $decodedContent = json_decode($requestContent, true);
76
77
        // Check if parsing is ok => Parse error
78 20
        if (JSON_ERROR_NONE !== json_last_error()) {
79 1
            throw new JsonRpcParseErrorException($requestContent, json_last_error(), json_last_error_msg());
80
        }
81
82
        // Content must be either an array (normal request) or an array of array (batch request)
83
        //  => so must be an array
84
        // In case it's a batch call, at least one sub request must exist
85
        // and in case not, some required properties must exist
86
        // => array must have at least one child
87 19
        if (!is_array($decodedContent) || count($decodedContent) === 0) {
88 5
            throw new JsonRpcInvalidRequestException($requestContent);
89
        }
90
91 14
        return $decodedContent;
92
    }
93
94
    /**
95
     * @param array $decodedContent
96
     *
97
     * @return JsonRpcCall
98
     */
99 14
    public function denormalize(array $decodedContent) : JsonRpcCall
100
    {
101 14
        return $this->callDenormalizer->denormalize($decodedContent);
102
    }
103
104
    /**
105
     * @param JsonRpcCallResponse $jsonRpcCallResponse
106
     *
107
     * @return array|null
108
     */
109 14
    public function normalize(JsonRpcCallResponse $jsonRpcCallResponse)
110
    {
111 14
        return $this->callResponseNormalizer->normalize($jsonRpcCallResponse);
112
    }
113
}
114