Completed
Push — master ( 54e267...8d7cab )
by Marco
02:36
created

RpcRequest::setId()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7.049

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 9
cts 10
cp 0.9
rs 8.6666
c 0
b 0
f 0
cc 7
nc 2
nop 1
crap 7.049
1
<?php namespace Comodojo\RpcClient;
2
3
use \Comodojo\Foundation\Utils\UniqueId;
4
use \InvalidArgumentException;
5
use \Exception;
6
7
/**
8
 * @package     Comodojo Spare Parts
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     MIT
11
 *
12
 * LICENSE:
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
 * THE SOFTWARE.
21
 */
22
23
class RpcRequest {
24
25
    /**
26
     * @var string
27
     */
28
    private $method;
29
30
    /**
31
     * @var array
32
     */
33
    private $parameters = [];
34
35
    /**
36
     * @var array
37
     */
38
    private $special_types = [];
39
40
    private static $supported_special_types = [
41
        "base64",
42
        "datetime",
43
        "cdata"
44
    ];
45
46
    /**
47
     * Request's id
48
     *
49
     * @var boolean|integer
50
     */
51
    private $id = true;
52
53
    /**
54
     * @var string
55
     */
56
    private $uid;
57
58 63
    public function __construct() {
59
60 63
        $this->uid = UniqueId::generate(32);
61
62 63
    }
63
64
    /**
65
     * Get request's unique id
66
     *
67
     * @return string
68
     */
69 12
    public function getUniqueId() {
70
71 12
        return $this->uid;
72
73
    }
74
75
    /**
76
     * Get rpc method
77
     *
78
     * @return string|null
79
     */
80 63
    public function getMethod() {
81
82 63
        return $this->method;
83
84
    }
85
86
    /**
87
     * Set rpc method
88
     *
89
     * @param string $method
90
     * @return self
91
     * @throws InvalidArgumentException
92
     */
93 63
    public function setMethod($method) {
94
95 63
        if ( empty($method) || !is_string($method) ) throw new InvalidArgumentException("Invalid RPC method");
96
97 63
        $this->method = $method;
98
99 63
        return $this;
100
101
    }
102
103
    /**
104
     * Get parameters
105
     *
106
     * @return array
107
     */
108 63
    public function getParameters() {
109
110 63
        return $this->parameters;
111
112
    }
113
114
    /**
115
     * Set parameters
116
     *
117
     * @param array $params
118
     * @return self
119
     */
120 63
    public function setParameters(array $params = []) {
121
122 63
        $this->parameters = $params;
123
124 63
        return $this;
125
126
    }
127
128
    /**
129
     * Get values marked as special type
130
     *
131
     * @return array
132
     */
133 39
    public function getSpecialTypes() {
134
135 39
        return $this->special_types;
136
137
    }
138
139
    /**
140
     * Set values as special type
141
     *
142
     * @param string $value
143
     * @param string $type
144
     * @return self
145
     * @throws InvalidArgumentException
146
     */
147 9
    public function setSpecialType(&$value, $type) {
148
149 9
        $type = strtolower($type);
150
151 9
        if ( empty($value) || !in_array($type, self::$supported_special_types) ) {
152
            throw new InvalidArgumentException("Invalid value type");
153
        }
154
155 9
        $this->special_types[$value] = $type;
156
157 9
        return $this;
158
159
    }
160
161
    /**
162
     * Get request's id
163
     *
164
     * @return int|bool|null
165
     */
166 24
    public function getId() {
167
168 24
        return $this->id;
169
170
    }
171
172
    /**
173
     * Set request's id
174
     *
175
     * @param int|bool|null $id
176
     * @return self
177
     * @throws InvalidArgumentException
178
     */
179 63
    public function setId($id = null) {
180
181
        if (
182 63
            $id === null ||
183 60
            is_bool($id) ||
184 9
            ( is_int($id) && $id > 0 ) ||
185 44
            ( is_string($id) && !empty($id) )
186 21
        ) {
187
188 63
            $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id can also be of type string. However, the property $id is declared as type boolean|integer. 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...
189
190 21
        } else {
191
192
            throw new InvalidArgumentException("Invalid RPC id");
193
194
        }
195
196 63
        return $this;
197
198
    }
199
200
    /**
201
     * Export request as an array
202
     *
203
     * @return array
204
     */
205 36
    public function toArray() {
206
207
        return [
208 36
            'uid' => $this->uid,
209 36
            'method' => $this->method,
210 36
            'parameters' => $this->parameters,
211 36
            'special_types' => $this->special_types,
212 36
            'id' => $this->id
213 12
        ];
214
215
    }
216
217
    /**
218
     * Static constructor
219
     *
220
     * @param string $method
221
     * @param array $parameters
222
     * @param int|bool|null $id
223
     * @return RpcRequest
224
     * @throws Exception
225
     */
226 63
    public static function create($method, array $parameters = [], $id = true) {
227
228 63
        $request = new RpcRequest();
229
230
        try {
231
232 63
            $request->setMethod($method)
233 63
                ->setParameters($parameters)
234 63
                ->setId($id);
235
236 21
        } catch (Exception $e) {
237
238
            throw $e;
239
240
        }
241
242 63
        return $request;
243
244
    }
245
246
}
247