summaryrefslogtreecommitdiff
path: root/vendor/fguillot/json-rpc/src/JsonRPC/Response/ResponseBuilder.php
blob: c1caff92f857735c42ac9de31df94000dbed9e64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php

namespace JsonRPC\Response;

use BadFunctionCallException;
use Exception;
use InvalidArgumentException;
use JsonRPC\Exception\AccessDeniedException;
use JsonRPC\Exception\AuthenticationFailureException;
use JsonRPC\Exception\InvalidJsonFormatException;
use JsonRPC\Exception\InvalidJsonRpcFormatException;
use JsonRPC\Exception\ResponseEncodingFailureException;
use JsonRPC\Exception\ResponseException;
use JsonRPC\Validator\JsonEncodingValidator;

/**
 * Class ResponseBuilder
 *
 * @package JsonRPC
 * @author  Frederic Guillot
 */
class ResponseBuilder
{
    /**
     * Payload ID
     *
     * @access private
     * @var mixed
     */
    private $id;

    /**
     * Payload ID
     *
     * @access private
     * @var mixed
     */
    private $result;

    /**
     * Payload error code
     *
     * @access private
     * @var integer
     */
    private $errorCode;

    /**
     * Payload error message
     *
     * @access private
     * @var string
     */
    private $errorMessage;

    /**
     * Payload error data
     *
     * @access private
     * @var mixed
     */
    private $errorData;

    /**
     * HTTP Headers
     *
     * @access private
     * @var array
     */
    private $headers = array(
        'Content-Type' => 'application/json',
    );

    /**
     * HTTP status
     *
     * @access private
     * @var string
     */
    private $status;

    /**
     * Exception
     *
     * @access private
     * @var ResponseException
     */
    private $exception;

    /**
     * Get new object instance
     *
     * @static
     * @access public
     * @return ResponseBuilder
     */
    public static function create()
    {
        return new static();
    }

    /**
     * Set id
     *
     * @access public
     * @param  mixed  $id
     * @return $this
     */
    public function withId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * Set result
     *
     * @access public
     * @param  mixed $result
     * @return $this
     */
    public function withResult($result)
    {
        $this->result = $result;
        return $this;
    }

    /**
     * Set error
     *
     * @access public
     * @param  integer $code
     * @param  string  $message
     * @param  string  $data
     * @return $this
     */
    public function withError($code, $message, $data = '')
    {
        $this->errorCode = $code;
        $this->errorMessage = $message;
        $this->errorData = $data;
        return $this;
    }

    /**
     * Set exception
     *
     * @access public
     * @param  Exception $exception
     * @return $this
     */
    public function withException(Exception $exception)
    {
        $this->exception = $exception;
        return $this;
    }

    /**
     * Add HTTP header
     *
     * @access public
     * @param  string $name
     * @param  string $value
     * @return $this
     */
    public function withHeader($name, $value)
    {
        $this->headers[$name] = $value;
        return $this;
    }

    /**
     * Add HTTP Status
     *
     * @access public
     * @param  string $status
     * @return $this
     */
    public function withStatus($status)
    {
        $this->status = $status;
        return $this;
    }

    /**
     * Get status
     *
     * @access public
     * @return string
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Get headers
     *
     * @access public
     * @return string[]
     */
    public function getHeaders()
    {
        return $this->headers;
    }

    /**
     * Build response
     *
     * @access public
     * @return string
     */
    public function build()
    {
        $encodedResponse = json_encode($this->buildResponse());
        JsonEncodingValidator::validate();

        return $encodedResponse;
    }

    /**
     * Send HTTP headers
     *
     * @access public
     * @return $this
     */
    public function sendHeaders()
    {
        if (! empty($this->status)) {
            header($this->status);
        }

        foreach ($this->headers as $name => $value) {
            header($name.': '.$value);
        }

        return $this;
    }

    /**
     * Build response payload
     *
     * @access private
     * @return array
     */
    private function buildResponse()
    {
        $response = array('jsonrpc' => '2.0');
        $this->handleExceptions();

        if (! empty($this->errorMessage)) {
            $response['error'] = $this->buildErrorResponse();
        } else {
            $response['result'] = $this->result;
        }

        $response['id'] = $this->id;
        return $response;
    }

    /**
     * Build response error payload
     *
     * @access private
     * @return array
     */
    private function buildErrorResponse()
    {
        $response = array(
            'code' => $this->errorCode,
            'message' => $this->errorMessage,
        );

        if (! empty($this->errorData)) {
            $response['data'] = $this->errorData;
        }

        return $response;
    }

    /**
     * Transform exceptions to JSON-RPC errors
     *
     * @access private
     */
    private function handleExceptions()
    {
        if ($this->exception instanceof InvalidJsonFormatException) {
            $this->errorCode = -32700;
            $this->errorMessage = 'Parse error';
            $this->id = null;
        } elseif ($this->exception instanceof InvalidJsonRpcFormatException) {
            $this->errorCode = -32600;
            $this->errorMessage = 'Invalid Request';
            $this->id = null;
        } elseif ($this->exception instanceof BadFunctionCallException) {
            $this->errorCode = -32601;
            $this->errorMessage = 'Method not found';
        } elseif ($this->exception instanceof InvalidArgumentException) {
            $this->errorCode = -32602;
            $this->errorMessage = 'Invalid params';
        } elseif ($this->exception instanceof ResponseEncodingFailureException) {
            $this->errorCode = -32603;
            $this->errorMessage = 'Internal error';
            $this->errorData = $this->exception->getMessage();
        } elseif ($this->exception instanceof AuthenticationFailureException) {
            $this->errorCode = 401;
            $this->errorMessage = 'Unauthorized';
            $this->status = 'HTTP/1.0 401 Unauthorized';
            $this->withHeader('WWW-Authenticate', 'Basic realm="JsonRPC"');
        } elseif ($this->exception instanceof AccessDeniedException) {
            $this->errorCode = 403;
            $this->errorMessage = 'Forbidden';
            $this->status = 'HTTP/1.0 403 Forbidden';
        } elseif ($this->exception instanceof ResponseException) {
            $this->errorCode = $this->exception->getCode();
            $this->errorMessage = $this->exception->getMessage();
            $this->errorData = $this->exception->getData();
        } elseif ($this->exception instanceof Exception) {
            $this->errorCode = $this->exception->getCode();
            $this->errorMessage = $this->exception->getMessage();
        }
    }
}