Passed
Push — master ( b22a88...886da7 )
by Marcel
02:28
created

Error::internal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UMA\JsonRpc;
6
7
final class Error extends Response
8
{
9
    /**
10
     * @var int
11
     */
12
    private $code;
13
14
    /**
15
     * @var string
16
     */
17
    private $message;
18
19
    /**
20
     * @var mixed
21
     */
22
    private $data;
23
24
    /**
25
     * @param int             $code
26
     * @param string          $message
27
     * @param mixed           $data
28
     * @param int|string|null $id
29
     */
30 19
    public function __construct(int $code, string $message, $data = null, $id = null)
31
    {
32 19
        $this->code = $code;
33 19
        $this->message = $message;
34 19
        $this->data = $data;
35 19
        $this->id = $id;
36 19
    }
37
38 5
    public static function parsing(): Error
39
    {
40 5
        return new static(-32700, 'Parse error');
41
    }
42
43 8
    public static function invalidRequest(): Error
44
    {
45 8
        return new static(-32600, 'Invalid Request');
46
    }
47
48 5
    public static function unknownMethod($id): Error
49
    {
50 5
        return new static(-32601, 'Method not found', null, $id);
51
    }
52
53 2
    public static function invalidParams($id): Error
54
    {
55 2
        return new static(-32602, 'Invalid params', null, $id);
56
    }
57
58 2
    public static function internal($id): Error
59
    {
60 2
        return new static(-32603, 'Internal error', null, $id);
61
    }
62
63 3
    public static function tooManyBatchRequests(int $limit): Error
64
    {
65 3
        return new static(-32000, 'Too many batch requests sent to server', ['limit' => $limit]);
66
    }
67
68 18
    public function jsonSerialize(): array
69
    {
70
        $payload = [
71 18
            'jsonrpc' => '2.0',
72
            'error' => [
73 18
                'code' => $this->code,
74 18
                'message' => $this->message
75
            ],
76 18
            'id' => $this->id
77
        ];
78
79 18
        if (null !== $this->data) {
80 3
            $payload['error']['data'] = $this->data;
81
        }
82
83 18
        return $payload;
84
    }
85
}
86