Completed
Pull Request — feature/app (#10)
by Yo
01:51
created

RawResponseSerializer::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
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 10
            return $this->responseNormalizer->normalize(array_shift($responseList));
67
        }
68
    }
69
70
    /**
71
     * @param JsonRpcResponse[] $responseList
72
     *
73
     * @return array|null
74
     */
75 4
    private function normalizeBatchResponse(array $responseList)
76
    {
77 4
        $resultList = [];
78
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