ResponseBuilder::getErrorMessage()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
rs 6.7272
cc 7
eloc 16
nc 7
nop 1
1
<?php
2
3
namespace PhpJsonRpc\Server;
4
5
use PhpJsonRpc\Common\Interceptor\Interceptor;
6
use PhpJsonRpc\Core\Result\AbstractResult;
7
use PhpJsonRpc\Core\Result\Error;
8
use PhpJsonRpc\Core\Result\Result;
9
use PhpJsonRpc\Core\ResultSpec;
10
use PhpJsonRpc\Error\JsonRpcException;
11
use PhpJsonRpc\Server\ResponseBuilder\BuilderContainer;
12
13
class ResponseBuilder
14
{
15
    /**
16
     * @var Interceptor
17
     */
18
    private $preBuild;
19
20
    /**
21
     * ResponseBuilder constructor.
22
     */
23
    public function __construct()
24
    {
25
        $this->preBuild = Interceptor::createBase();
26
    }
27
28
    /**
29
     * @param ResultSpec $result
30
     *
31
     * @return string
32
     */
33
    public function build(ResultSpec $result): string
34
    {
35
        $response = [];
36
        $units = $result->getResults();
37
38
        foreach ($units as $unit) {
39
            /** @var AbstractResult $unit */
40
            if ($unit instanceof Result) {
41
                /** @var Result $unit */
42
                $response[] = [
43
                    'jsonrpc' => '2.0',
44
                    'result' => $this->preBuild($unit->getResult()),
45
                    'id'     => $unit->getId()
46
                ];
47
            } elseif ($unit instanceof Error) {
48
                /** @var Error $unit */
49
                $baseException = $unit->getBaseException();
50
                $response[] = [
51
                    'jsonrpc' => '2.0',
52
                    'error' => [
53
                        'code'    => $baseException->getJsonRpcCode(),
54
                        'message' => $this->getErrorMessage($baseException->getJsonRpcCode()),
55
                        'data'    => $baseException->getJsonRpcData()
56
                    ],
57
                    'id' => $unit->getId()
58
                ];
59
            }
60
        }
61
62
        if (empty($response)) {
63
            return '';
64
        }
65
66
        if ($result->isSingleResult()) {
67
            return json_encode($response[0]);
68
        }
69
70
        return json_encode($response);
71
    }
72
73
    /**
74
     * @return Interceptor
75
     */
76
    public function onPreBuild(): Interceptor
77
    {
78
        return $this->preBuild;
79
    }
80
81
    /**
82
     * @param mixed $result
83
     *
84
     * @return mixed
85
     */
86
    private function preBuild($result)
87
    {
88
        $container = $this->preBuild->handle(new BuilderContainer($this, $result));
89
90
        if ($container instanceof BuilderContainer) {
91
            return $container->getValue();
92
        }
93
94
        throw new \RuntimeException();
95
    }
96
97
    /**
98
     * @param int $code
99
     * @return string
100
     */
101
    private function getErrorMessage(int $code): string
102
    {
103
        switch ($code) {
104
            case JsonRpcException::PARSE_ERROR:
105
                return 'Parse error';
106
107
            case JsonRpcException::INVALID_REQUEST:
108
                return 'Invalid Request';
109
110
            case JsonRpcException::METHOD_NOT_FOUND:
111
                return 'Method not found';
112
113
            case JsonRpcException::INVALID_PARAMS:
114
                return 'Invalid params';
115
116
            case JsonRpcException::INTERNAL_ERROR:
117
                return 'Internal error';
118
119
            case JsonRpcException::SERVER_ERROR:
120
                return 'Server Error';
121
122
            default:
123
                return 'Internal error';
124
        }
125
    }
126
}
127