Completed
Push — master ( 2b2467...86fe2f )
by Marco
12:06
created

XmlProcessor::encodeMulticall()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 35
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
rs 8.5806
cc 4
eloc 14
nc 6
nop 1
1
<?php namespace Comodojo\RpcClient\Processor;
2
3
use \Psr\Log\LoggerInterface;
4
use \Comodojo\Exception\RpcException;
5
use \Comodojo\RpcClient\RpcRequest;
6
use \Comodojo\Xmlrpc\XmlrpcEncoder;
7
use \Comodojo\Xmlrpc\XmlrpcDecoder;
8
use \Exception;
9
10
class XmlProcessor implements ProcessorInterface {
11
12
    private $encoding;
13
14
    private $logger;
15
16
    private $encoder;
17
18
    private $decoder;
19
20
    private $requests;
21
22
    public function __construct($encoding, LoggerInterface $logger) {
23
24
        $this->encoding = $encoding;
25
26
        $this->logger = $logger;
27
28
        $this->encoder = new XmlrpcEncoder();
29
30
        $this->decoder = new XmlrpcDecoder();
31
32
    }
33
34
    public function encode($requests) {
35
36
        $this->requests = $requests;
37
38
        try {
39
40
            $payload = ( sizeof($requests) > 1 ) ? $this->encodeMulticall($requests) : $this->encodeSingleCall($requests[0]);
41
42
        } catch (XmlrpcException $xe) {
0 ignored issues
show
Bug introduced by
The class Comodojo\RpcClient\Processor\XmlrpcException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
43
44
            throw $xe;
45
46
        }
47
48
        return $payload;
49
50
    }
51
52
    public function decode($response) {
53
54
        try {
55
56
            $content = $this->decoder->decodeResponse($response);
57
58
            if ( $this->decoder->isFault() ) throw new RpcException($content['faultString'], $content['faultCode']);
59
60
        } catch (XmlrpcException $xe) {
0 ignored issues
show
Bug introduced by
The class Comodojo\RpcClient\Processor\XmlrpcException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
61
62
            throw $xe;
63
64
        }
65
66
        return $content;
67
68
    }
69
70
    private function encodeSingleCall(RpcRequest $request) {
71
72
        $this->requests = $request;
73
74
    	$this->logger->notice("Performing a single XML call");
75
76
    	$this->logger->debug("Data dump before encoding", $request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Comodojo\RpcClient\RpcRequest>, but the function expects a array.

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...
77
78
        try {
79
80
        	// encoding
81
82
        	foreach ( $request->getSpecialTypes() as $key => $value ) {
83
84
                $this->encoder->setValueType($key, $value);
85
86
            }
87
88
            $encoded_request = $this->encoder->encodeCall($request->getMethod(), $request->getParameters());
89
90
        } catch (XmlrpcException $xe) {
0 ignored issues
show
Bug introduced by
The class Comodojo\RpcClient\Processor\XmlrpcException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
91
92
            throw $xe;
93
94
        }
95
96
        $this->logger->debug("Data dump after encoding", $encoded_request);
0 ignored issues
show
Documentation introduced by
$encoded_request is of type string, but the function expects a array.

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...
97
98
        return $encoded_request;
99
100
    }
101
102
    /**
103
     * Perform an xml multicall
104
     *
105
     * @param   array   $requests
106
     *
107
     * @return  array
108
     *
109
     * @throws \Comodojo\Exception\RpcException
110
     * @throws \Comodojo\Exception\HttpException
111
     * @throws \Comodojo\Exception\XmlrpcException
112
     * @throws \Exception
113
     */
114
    private function encodeMulticall($requests) {
115
116
        $this->requests = $requests;
117
118
        $composed_requests = array();
119
120
    	$this->logger->notice("Performing an XML multicall");
121
122
    	$this->logger->debug("Data dump before encoding", $requests);
123
124
        foreach ($requests as $request) {
125
126
            $composed_requests[] = array($request->getMethod(), $request->getParameters());
127
128
            foreach ( $request->getSpecialTypes() as $key => $value ) {
129
130
                $this->encoder->setValueType($key, $value);
131
132
            }
133
134
        }
135
136
        try {
137
138
            $encoded_requests = $this->encoder->setEncoding($this->encoding)->encodeMulticall($composed_requests);
0 ignored issues
show
Unused Code introduced by
$encoded_requests is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
139
140
        } catch (XmlrpcException $xe) {
0 ignored issues
show
Bug introduced by
The class Comodojo\RpcClient\Processor\XmlrpcException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
141
142
            throw $xe;
143
144
        }
145
146
        return $encoded_request;
0 ignored issues
show
Bug introduced by
The variable $encoded_request does not exist. Did you mean $encoded_requests?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
147
148
    }
149
150
}
151