Completed
Push — master ( 7b106f...b39998 )
by Asmir
04:16
created

ResultCreator::prepareResult()   C

Complexity

Conditions 8
Paths 13

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8.4551

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
ccs 21
cts 26
cp 0.8077
rs 5.3846
cc 8
eloc 24
nc 13
nop 2
crap 8.4551
1
<?php
2
namespace GoetasWebservices\SoapServices\SoapClient\Result;
3
4
use JMS\Serializer\Serializer;
5
6
class ResultCreator implements ResultCreatorInterface
7
{
8
    private $unwrap = false;
9
    /**
10
     * @var Serializer
11
     */
12
    private $serializer;
13
14 14
    public function __construct(Serializer $serializer, $unwrap = false)
15
    {
16 14
        $this->serializer = $serializer;
17 14
        $this->unwrap = $unwrap;
18 14
    }
19
20 4
    public function prepareResult($object, array $output)
21
    {
22 4
        if (!count($output['parts'])) {
23
            return null;
24
        }
25 4
        $factory = $this->serializer->getMetadataFactory();
26
27 4
        $classMetadata = $factory->getMetadataForClass($output['message_fqcn']);
28 4
        $bodyMetadata = $classMetadata->propertyMetadata['body'];
29 4
        $bodyClassMetadata = $factory->getMetadataForClass($bodyMetadata->type['name']);
30 4
        $body = $bodyMetadata->getValue($object);
31 4
        $parts = [];
32 4
        foreach ($bodyClassMetadata->propertyMetadata as $propertyMetadata) {
33 4
            $parts[$propertyMetadata->name] = $propertyMetadata->getValue($body);
34 4
        }
35 4
        if (count($output['parts']) > 1) {
36 1
            return $parts;
37
        } else {
38 3
            if ($this->unwrap) {
39 3
                foreach ($bodyClassMetadata->propertyMetadata as $propertyMetadata) {
40 3
                    $propClassMetadata = $factory->getMetadataForClass($propertyMetadata->type['name']);
41
42 3
                    if (count($propClassMetadata->propertyMetadata) > 1) {
43
                        throw new \Exception("When using wrapped mode, the wrapped object can not have multiple properties");
44
                    }
45 3
                    if (!count($propClassMetadata->propertyMetadata)) {
46
                        return null;
47
                    }
48 3
                    $propertyMetadata = reset($propClassMetadata->propertyMetadata);
49 3
                    return $propertyMetadata->getValue(reset($parts));
50
                }
51
            }
52 1
            return reset($parts);
53
        }
54
    }
55
}
56