Passed
Push — devel-3.0 ( ca2338...7158af )
by Rubén
03:39
created

JsonRpcResponse::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Services\Api;
26
27
use SP\Core\Exceptions\SPException;
28
use SP\Http\Json;
29
30
/**
31
 * Class JsonRpcResponse
32
 *
33
 * @package SP\Api
34
 */
35
final class JsonRpcResponse
36
{
37
    const PARSE_ERROR = -32700;
38
    const INVALID_REQUEST = -32600;
39
    const METHOD_NOT_FOUND = -32601;
40
    const INVALID_PARAMS = -32602;
41
    const INTERNAL_ERROR = -32603;
42
    const SERVER_ERROR = -32000;
43
44
    /**
45
     * @param ApiResponse $apiResponse
46
     * @param int         $id
47
     *
48
     * @return string
49
     * @throws \SP\Core\Exceptions\SPException
50
     */
51
    public static function getResponse(ApiResponse $apiResponse, int $id)
52
    {
53
        return Json::getJson([
54
            'jsonrpc' => '2.0',
55
            'result' => $apiResponse->getResponse(),
56
            'id' => $id
57
        ]);
58
    }
59
60
    /**
61
     * @param \Exception $e
62
     * @param  int       $id
63
     *
64
     * @return string
65
     */
66
    public static function getResponseException(\Exception $e, int $id)
67
    {
68
        $data = ($e instanceof SPException) ? $e->getHint() : null;
69
70
        return self::getResponseError($e->getMessage(), $e->getCode(), $id, $data);
71
    }
72
73
    /**
74
     * @param string $message
75
     * @param int    $code
76
     * @param int    $id
77
     * @param mixed  $data
78
     *
79
     * @return string
80
     */
81
    public static function getResponseError(string $message, int $code, int $id, $data = null)
82
    {
83
        return json_encode([
84
            'jsonrpc' => '2.0',
85
            'error' => [
86
                'message' => __($message),
87
                'code' => $code,
88
                'data' => $data
89
            ],
90
            'id' => $id
91
        ], JSON_PARTIAL_OUTPUT_ON_ERROR);
92
    }
93
}