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

ResultCreator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 50
ccs 25
cts 30
cp 0.8333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C prepareResult() 0 35 8
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