Completed
Pull Request — master (#33)
by Robbert van den
03:13 queued 01:49
created

ResultCreator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 50
ccs 26
cts 30
cp 0.8667
rs 10
c 0
b 0
f 0

2 Methods

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