Multicall::packError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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 Parameters $params
35
     *
36
     * @return array
37
     * @throws RpcException
38
     */
39 9
    final public static function execute(Parameters $params) {
40
41 9
        if ( $params->protocol() != RpcServer::XMLRPC ) {
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\RpcServer\Request\Parameters::protocol() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

41
        if ( /** @scrutinizer ignore-deprecated */ $params->protocol() != RpcServer::XMLRPC ) {
Loading history...
42
43 3
            throw new RpcException($params->errors()->get(-31000), -31000);
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\RpcServer\Request\Parameters::errors() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

43
            throw new RpcException(/** @scrutinizer ignore-deprecated */ $params->errors()->get(-31000), -31000);
Loading history...
44
45
        }
46
47 6
        $boxcarred_requests = $params->get('requests');
48
49 6
        $results = [];
50
51 6
        foreach ( $boxcarred_requests as $position => $request ) {
52
53 6
            $new_parameters = new Parameters(
54 6
                $params->capabilities(),
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\RpcServer\Reque...ameters::capabilities() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

54
                /** @scrutinizer ignore-deprecated */ $params->capabilities(),
Loading history...
55 6
                $params->methods(),
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\RpcServer\Request\Parameters::methods() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

55
                /** @scrutinizer ignore-deprecated */ $params->methods(),
Loading history...
56 6
                $params->errors(),
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\RpcServer\Request\Parameters::errors() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

56
                /** @scrutinizer ignore-deprecated */ $params->errors(),
Loading history...
57 6
                $params->logger(),
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\RpcServer\Request\Parameters::logger() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

57
                /** @scrutinizer ignore-deprecated */ $params->logger(),
Loading history...
58 6
                $params->protocol()
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\RpcServer\Request\Parameters::protocol() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

58
                /** @scrutinizer ignore-deprecated */ $params->protocol()
Loading history...
59
            );
60
61 6
            $results[$position] = self::singleCall($request, $new_parameters);
62
63
        }
64
65 6
        return $results;
66
67
    }
68
69
    /**
70
     * Perform a single call
71
     *
72
     * @param array $request
73
     * @param Parameters $parameters_object
74
     *
75
     * @return mixed
76
     */
77 6
    private static function singleCall(array $request, Parameters $parameters_object) {
78
79 6
        if ( !isset($request[0]) || !isset($request[1]) ) {
80
81
            return self::packError(-32600, $parameters_object->errors()->get(-32600));
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\RpcServer\Request\Parameters::errors() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

81
            return self::packError(-32600, /** @scrutinizer ignore-deprecated */ $parameters_object->errors()->get(-32600));
Loading history...
82
83
        }
84
85 6
        if ( $request[0] == 'system.multicall' ) {
86
87 3
            return self::packError(-31001, $parameters_object->errors()->get(-31001));
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\RpcServer\Request\Parameters::errors() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

87
            return self::packError(-31001, /** @scrutinizer ignore-deprecated */ $parameters_object->errors()->get(-31001));
Loading history...
88
89
        }
90
91 6
        $payload = array($request[0], $request[1]);
92
93
        try {
94
95 6
            return XmlProcessor::process($payload, $parameters_object, $parameters_object->logger());
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\RpcServer\Request\Parameters::logger() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

95
            return XmlProcessor::process($payload, $parameters_object, /** @scrutinizer ignore-deprecated */ $parameters_object->logger());
Loading history...
96
97 3
        } catch (RpcException $re) {
98
99 3
            return self::packError($re->getCode(), $re->getMessage());
100
101
        } catch (Exception $e) {
102
103
            return self::packError(-32500, $re->getMessage());
104
105
        }
106
107
    }
108
109
    /**
110
     * Pack an XMLRPC error
111
     *
112
     * @param integer $code
113
     * @param string  $message
114
     *
115
     * @return mixed
116
     */
117 6
    private static function packError($code, $message) {
118
119 6
        return array('faultCode' => $code, 'faultString' => $message);
120
121
    }
122
123
}
124