Passed
Pull Request — master (#82)
by
unknown
03:45
created

JsonRpcResponseNormalizer::composeDebugErrorData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
namespace Yoanm\JsonRpcServer\App\Serialization;
3
4
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcExceptionInterface;
5
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse;
6
7
/**
8
 * Class JsonRpcResponseNormalizer
9
 */
10
class JsonRpcResponseNormalizer
11
{
12
    const KEY_JSON_RPC = 'jsonrpc';
13
    const KEY_ID = 'id';
14
    const KEY_RESULT = 'result';
15
    const KEY_ERROR = 'error';
16
17
    const SUB_KEY_ERROR_CODE = 'code';
18
    const SUB_KEY_ERROR_MESSAGE = 'message';
19
    const SUB_KEY_ERROR_DATA = 'data';
20
21
    /**
22
     * @var bool whether to display debug data for the errors.
23
     */
24
    protected $debug = false;
25
26 9
    public function __construct(bool $debug = false)
27
    {
28 9
        $this->debug = $debug;
29 9
    }
30
31
    /**
32
     * @param JsonRpcResponse $response
33
     *
34
     * @return array|null
35
     */
36 9
    public function normalize(JsonRpcResponse $response) : ?array
37
    {
38
        // Notifications must not have a response, even if they are on error
39 9
        if ($response->isNotification()) {
40 2
            return null;
41
        }
42
43
        $data = [
44 7
            self::KEY_JSON_RPC => $response->getJsonRpc(),
45 7
            self::KEY_ID => $response->getId()
46
        ];
47
48 7
        if ($response->getError()) {
49 4
            $data[self::KEY_ERROR] = $this->normalizeError(
50 4
                $response->getError()
51
            );
52
        } else {
53 3
            $data[self::KEY_RESULT] = $response->getResult();
54
        }
55
56 7
        return $data;
57
    }
58
59
    /**
60
     * @param JsonRpcExceptionInterface $error
61
     *
62
     * @return array
63
     */
64 4
    private function normalizeError(JsonRpcExceptionInterface $error) : array
65
    {
66
        $normalizedError = [
67 4
            self::SUB_KEY_ERROR_CODE => $error->getErrorCode(),
68 4
            self::SUB_KEY_ERROR_MESSAGE => $error->getErrorMessage()
69
        ];
70
71 4
        $errorData = $error->getErrorData();
72
73 4
        if ($this->debug) {
74 1
            $errorData += $this->composeDebugErrorData($error->getPrevious() ?? $error);
75
        }
76
77
78 4
        if (!empty($errorData)) {
79 2
            $normalizedError[self::SUB_KEY_ERROR_DATA] = $errorData;
80
        }
81
82 4
        return $normalizedError;
83
    }
84
85 1
    private function composeDebugErrorData(\Throwable $error) : array
86
    {
87
        return [
88 1
            '_code' => $error->getCode(),
89 1
            '_message' => $error->getMessage(),
90 1
            '_trace' => $error->getTrace(),
91
        ];
92
    }
93
}
94