Completed
Push — feature/app ( 2d92a3...b199ab )
by Yo
02:39
created

RawResponseSerializer::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\JsonRpcServer\Infra\Serialization;
3
4
use Yoanm\JsonRpcServer\App\Serialization\ResponseNormalizer;
5
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse;
6
use Yoanm\JsonRpcServer\Infra\RawObject\JsonRpcRawResponse;
7
8
/**
9
 * Class RawResponseSerializer
10
 */
11
class RawResponseSerializer
12
{
13
    const KEY_JSON_RPC = 'json-rpc';
14
    const KEY_ID = 'id';
15
    const KEY_RESULT = 'result';
16
    const KEY_ERROR = 'error';
17
18
    const SUB_KEY_ERROR_CODE = 'code';
19
    const SUB_KEY_ERROR_MESSAGE = 'message';
20
    const SUB_KEY_ERROR_DATA = 'data';
21
22
    /** @var ResponseNormalizer */
23
    private $responseNormalizer;
24
25
    /**
26
     * @param ResponseNormalizer $responseNormalizer
27
     */
28 15
    public function __construct(ResponseNormalizer $responseNormalizer)
29
    {
30 15
        $this->responseNormalizer = $responseNormalizer;
31 15
    }
32
33
    /**
34
     * @param JsonRpcRawResponse $rawReponse
35
     *
36
     * @return string
37
     */
38 7
    public function serialize(JsonRpcRawResponse $rawReponse) : string
39
    {
40 7
        return $this->encode(
41 7
            $this->normalize($rawReponse)
42
        );
43
    }
44
45
    /**
46
     * @param mixed $normalizedContent
47
     *
48
     * @return string
49
     */
50 8
    public function encode($normalizedContent) : string
51
    {
52 8
        return json_encode($normalizedContent);
53
    }
54
55
    /**
56
     * @param JsonRpcRawResponse $rawResponse
57
     *
58
     * @return array|null
59
     */
60 14
    public function normalize(JsonRpcRawResponse $rawResponse)
61
    {
62 14
        if ($rawResponse->isBatch()) {
63 4
            return $this->normalizeBatchResponse($rawResponse->getResponseList());
64
        } else {
65 10
            $responseList = $rawResponse->getResponseList();
66
67 10
            return $this->responseNormalizer->normalize(array_shift($responseList));
68
        }
69
    }
70
71
    /**
72
     * @param JsonRpcResponse[] $responseList
73
     *
74
     * @return array|null
75
     */
76 4
    private function normalizeBatchResponse(array $responseList)
77
    {
78 4
        $resultList = [];
79 4
        foreach ($responseList as $response) {
80
            // Notifications must not have a response, even if they are on error
81 4
            if (!$response->isNotification()) {
82 4
                $resultList[] = $this->responseNormalizer->normalize($response);
83
            }
84
        }
85
86
        // if no result, it means It was a batch call with only notifications => return null response
87 4
        return count($resultList) > 0
88 4
            ? $resultList
89 4
            : null
90
        ;
91
    }
92
}
93