Completed
Pull Request — dev (#11)
by
unknown
04:51
created

JsonRpcRequest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 15
c 4
b 1
f 1
lcom 1
cbo 1
dl 0
loc 98
ccs 23
cts 23
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A getPost() 0 4 2
A getVersion() 0 4 2
A getMethod() 0 4 1
A getIdentifier() 0 4 2
A getParameters() 0 8 3
A isValid() 0 5 2
1
<?php
2
3
namespace Vectorface\SnappyRouter\Request;
4
5
use \Exception;
6
use \Vectorface\SnappyRouter\Handler\JsonRpcHandler;
7
8
/**
9
 * A class representing a JSON-RPC request.
10
 *
11
 * @copyright Copyright (c) 2014, VectorFace, Inc.
12
 */
13
class JsonRpcRequest extends HttpRequest
14
{
15
    /**
16
     * Holds the JSON-RPC request payload.
17
     *
18
     * @var Object
19
     */
20
    private $payload;
21
22
    /**
23
     * Constructor for a request.
24
     *
25
     * @param string $controller The controller being requested.
26
     * @param object $payload The action being invoked.
27
     * @param string $verb (optional) The HTTP verb (usually POST).
28
     */
29 6
    public function __construct($controller, $payload, $verb = 'POST')
30
    {
31
        // extract a minimal "action" value from the payload
32 6
        $action = '';
33 6
        if (is_object($payload) && isset($payload->method)) {
34 6
            $action = $payload->method;
35 6
        }
36 6
        parent::__construct($controller, $action, $verb, 'php://input');
37 6
        $this->payload = $payload;
38 6
    }
39
40
    /**
41
     * Returns the POST data parameter associated with the specified key.
42
     *
43
     * Since JSON-RPC and POST'ed data are mutually exclusive this returns null, or the default if provided.
44
     *
45
     * @param string $param The POST data parameter to retrieve.
46
     * @param mixed $defaultValue The default value to use when the key is not present.
47
     * @param mixed $filters The array of filters (or single filter) to apply to the data. Ignored.
48
     * @return mixed Returns null because POST is not possible, or the default value if the parameter is not present.
49
     */
50 1
    public function getPost($param, $defaultValue = null, $filters = array())
51
    {
52 1
        return isset($defaultValue) ? $defaultValue : null;
53
    }
54
55
    /**
56
     * Get the request version.
57
     *
58
     * @return string The request's version string. "1.0" is assumed if version is not present in the request.
59
     */
60 4
    public function getVersion()
61
    {
62 4
        return isset($this->payload->jsonrpc) ? $this->payload->jsonrpc : "1.0";
63
    }
64
65
    /**
66
     * Get the request method.
67
     *
68
     * @return string The request method name.
69
     */
70 1
    public function getMethod()
71
    {
72 1
        return $this->payload->method;
73
    }
74
75
    /**
76
     * Get the request identifier
77
     *
78
     * @return mixed The request identifier. This is generally a string, but the JSON-RPC spec isn't strict.
79
     */
80 4
    public function getIdentifier()
81
    {
82 4
        return isset($this->payload->id) ? $this->payload->id : null;
83
    }
84
85
    /**
86
     * Get request parameters.
87
     *
88
     * Note: Since PHP does not support named params, named params are turned into a single request object parameter.
89
     *
90
     * @return array An array of request paramters
91
     */
92 3
    public function getParameters()
93
    {
94 3
        if (isset($this->payload->params)) {
95
            /* JSON-RPC 2 can pass named params. For PHP's sake, turn that into a single object param. */
96 1
            return is_array($this->payload->params) ? $this->payload->params : array($this->payload->params);
97
        }
98 3
        return array();
99
    }
100
101
    /**
102
     * Returns whether this request is minimally valid for JSON RPC.
103
     * @return Returns true if the payload is valid and false otherwise.
104
     */
105 3
    public function isValid()
106
    {
107 3
        $action = $this->getAction();
108 3
        return is_object($this->payload) && !empty($action);
109
    }
110
}
111