Completed
Push — master ( 86fe2f...647445 )
by Marco
14:10
created

RpcRequest::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php namespace Comodojo\RpcClient;
2
3
use \Exception;
4
5
/**
6
 * Comodojo RPC client. It's able to talk in XML and JSON (2.0).
7
 *
8
 * It optionally supports a not standard encrypted transport
9
 *
10
 * @package     Comodojo Spare Parts
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @license     MIT
13
 *
14
 * LICENSE:
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
class RpcRequest {
26
27
    private $method;
28
29
    private $parameters = array();
30
31
    private $special_types = array();
32
33
    private $id = true;
34
35
    private $uid;
36
37
    public function __construct() {
38
39
        $this->uid = md5(uniqid(rand(), true));
40
41
    }
42
43
    public function setMethod($method) {
44
45
        if ( empty($method) || !is_string($method) ) throw new Exception("Invalid RPC method");
46
47
        $this->method = $method;
48
49
        return $this;
50
51
    }
52
53
    public function setParameters($params) {
54
55
        if ( !is_array($params) ) throw new Exception("Invalid RPC parameters");
56
57
        if ( !empty($params) ) $this->parameters = $params;
58
59
        return $this;
60
61
    }
62
63
    public function setSpecialType(&$value, $type) {
64
65
        $type = strtolower($type);
66
67
        if ( empty($value) OR !in_array($type, array("base64", "datetime", "cdata")) ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
68
69
            throw new Exception("Invalid value type");
70
71
        }
72
73
        $this->special_types[$value] = $type;
74
75
        return $this;
76
77
    }
78
79
    public function setId($id = null) {
80
81
        if ( is_null($id) || is_int($id) || is_bool($id) ) {
82
83
            $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id can also be of type integer. However, the property $id is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
84
85
        } else {
86
87
            throw new Exception("Invalid RPC id");
88
89
        }
90
91
        return $this;
92
93
    }
94
95
    public function getMethod() {
96
97
        return $this->method;
98
99
    }
100
101
    public function getParameters() {
102
103
        return $this->parameters;
104
105
    }
106
107
    public function getSpecialTypes() {
108
109
        return $this->special_types;
110
111
    }
112
113
    public function getId() {
114
115
        return $this->id;
116
117
    }
118
119
    public function getUniqueId() {
120
121
        return $this->uid;
122
123
    }
124
125
    public function toArray() {
126
127
        return array(
128
            'uid' => $this->uid,
129
            'method' => $this->method,
130
            'parameters' => $this->parameters,
131
            'special_types' => $this->special_types,
132
            'id' => $this->id
133
        );
134
135
    }
136
137
    public static function create($method, $parameters = array(), $id = true) {
138
139
        $request = new RpcRequest();
140
141
        try {
142
143
            $request->setMethod($method)
144
                ->setParameters($parameters)
145
                ->setId($id);
146
147
        } catch (Exception $e) {
148
149
            throw $e;
150
151
        }
152
153
        return $request;
154
155
    }
156
157
}
158