Completed
Push — master ( 6aef24...286712 )
by Márk
06:17
created

Zend2Parser::parse()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 40
Code Lines 22

Duplication

Lines 40
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 40
loc 40
ccs 0
cts 30
cp 0
rs 6.7273
cc 7
eloc 22
nc 5
nop 2
crap 56
1
<?php
2
3
namespace Fxmlrpc\Serialization\Parser;
4
5
use Fxmlrpc\Serialization\Exception\ParserException;
6
use Fxmlrpc\Serialization\Parser;
7
use Fxmlrpc\Serialization\Value\Base64Value;
8
use Zend\XmlRpc\Response;
9
10
/**
11
 * Parser to parse XML responses into its PHP representation using XML RPC extension.
12
 *
13
 * @author Márk Sági-Kazár <[email protected]>
14
 */
15 View Code Duplication
final class Zend2Parser implements Parser
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function parse($xmlString, &$isFault)
21
    {
22
        $response = new Response();
23
24
        try {
25
            $response->loadXml($xmlString);
26
        } catch (\Exception $e) {
27
            throw new ParserException($e->getMessage(), $e->getCode(), $e);
28
        }
29
30
        $isFault = $response->isFault();
31
32
        if ($isFault) {
33
            $fault = $response->getFault();
34
35
            return [
36
                'faultCode'   => $fault->getCode(),
37
                'faultString' => $fault->getMessage(),
38
            ];
39
        }
40
41
        $result = $response->getReturnValue();
42
43
        $toBeVisited = [&$result];
44
45
        while (isset($toBeVisited[0]) && $value = &$toBeVisited[0]) {
46
            $type = gettype($value);
47
            if ($type === 'array') {
48
                foreach ($value as &$element) {
49
                    $toBeVisited[] = &$element;
50
                }
51
52
                reset($value); // Reset all array pointers
53
            }
54
55
            array_shift($toBeVisited);
56
        }
57
58
        return $result;
59
    }
60
}
61