Completed
Push — master ( 52f4ed...808c26 )
by Marco
02:51
created

src/Reserved/Multicall.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Comodojo\RpcServer\Reserved;
2
3
use \Comodojo\RpcServer\RpcServer;
4
use \Comodojo\RpcServer\Request\Parameters;
5
use \Comodojo\RpcServer\Request\XmlProcessor;
6
use \Comodojo\Exception\RpcException;
7
use \Exception;
8
9
/**
10
 * The system.multicall method implementation.
11
 *
12
 * This method is available ONLY for XMLRPC protocol; json v2 SHOULD use batch requests.
13
 *
14
 * @package     Comodojo Spare Parts
15
 * @author      Marco Giovinazzi <[email protected]>
16
 * @license     MIT
17
 *
18
 * LICENSE:
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 */
28
29
class Multicall {
30
31
    /**
32
     * Execute call
33
     *
34
     * @param \Comodojo\RpcServer\Request\Parameters $params
35
     *
36
     * @return array
37
     */
38 9
    final public static function execute($params) {
39
40 9
        if ( $params->protocol() != RpcServer::XMLRPC ) {
41
42 3
            throw new RpcException($params->errors()->get(-31000), -31000);
43
44
        }
45
46 6
        $boxcarred_requests = $params->get('requests');
47
48 6
        $results = array();
49
50 6
        foreach ( $boxcarred_requests as $position => $request ) {
51
52 6
            $new_parameters = new Parameters(
53 6
                $params->capabilities(),
54 6
                $params->methods(),
55 6
                $params->errors(),
56 6
                $params->logger(),
57 6
                $params->protocol()
58 6
            );
59
60 6
            $results[$position] = self::singleCall($request, $new_parameters);
61
62 6
        }
63
64 6
        return $results;
65
66
    }
67
68
    /**
69
     * Perform a single call
70
     *
71
     * @param array                                  $request
72
     * @param \Comodojo\RpcServer\Request\Parameters $parameters_object
73
     *
74
     * @return mixed
75
     */
76 6
    private static function singleCall($request, $parameters_object) {
77
78 6
        if ( !isset($request[0]) || !isset($request[1]) ) {
79
80
            return self::packError(-32600, $parameters_object->errors()->get(-32600));
0 ignored issues
show
$parameters_object->errors()->get(-32600) is of type array|null, but the function expects a object<Comodojo\RpcServer\Reserved\srting>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
82
        }
83
84 6
        if ( $request[0] == 'system.multicall' ) {
85
86 3
            return self::packError(-31001, $parameters_object->errors()->get(-31001));
0 ignored issues
show
$parameters_object->errors()->get(-31001) is of type array|null, but the function expects a object<Comodojo\RpcServer\Reserved\srting>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
88
        }
89
90 6
        $payload = array($request[0], $request[1]);
91
92
        try {
93
94 6
            $result = XmlProcessor::process($payload, $parameters_object, $parameters_object->logger());
95
96 6
        } catch (RpcException $re) {
97
98 3
            return self::packError($re->getCode(), $re->getMessage());
99
100
        } catch (Exception $e) {
101
102
            return self::packError(-32500, $re->getMessage());
103
104
        }
105
106 6
        return $result;
107
108
    }
109
110
    /**
111
     * Pack an XMLRPC error
112
     *
113
     * @param integer $code
114
     * @param srting  $message
115
     *
116
     * @return mixed
117
     */
118 6
    private static function packError($code, $message) {
119
120 6
        return array('faultCode' => $code, 'faultString' => $message);
121
122
    }
123
124
}
125