Passed
Pull Request — master (#43)
by Mihail
09:00
created

Response::getRpcVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of Guzzle HTTP JSON-RPC
5
 *
6
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @see  http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE
12
 * @link http://github.com/graze/guzzle-jsonrpc
13
 */
14
15
namespace Graze\GuzzleHttp\JsonRpc\Message;
16
17
use Graze\GuzzleHttp\JsonRpc;
18
use GuzzleHttp\Psr7\Response as HttpResponse;
19
20
class Response extends HttpResponse implements ResponseInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getRpcErrorCode()
26
    {
27
        $error = $this->getFieldFromBody('error');
28
29
        return isset($error['code']) ? $error['code'] : null;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getRpcErrorMessage()
36
    {
37
        $error = $this->getFieldFromBody('error');
38
39
        return isset($error['message']) ? $error['message'] : null;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getRpcErrorData()
46
    {
47
        $error = $this->getFieldFromBody('error');
48
49
        return isset($error['data']) ? $error['data'] : null;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getRpcId()
56
    {
57
        return $this->getFieldFromBody('id');
58
    }
59
60
    /**
61
     * @return mixed
62
     */
63
    public function getRpcResult()
64
    {
65
        return $this->getFieldFromBody('result');
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getRpcVersion()
72
    {
73
        return $this->getFieldFromBody('jsonrpc');
74
    }
75
76
    /**
77
     * @param  string $key
78
     *
79
     * @return mixed
80
     */
81
    protected function getFieldFromBody($key)
82
    {
83
        $rpc = JsonRpc\json_decode((string) $this->getBody(), true);
84
85
        return isset($rpc[$key]) ? $rpc[$key] : null;
86
    }
87
}
88