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

ArgumentsReader::readArguments()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 8.017

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 46
ccs 29
cts 31
cp 0.9355
rs 5.5555
cc 8
eloc 29
nc 8
nop 2
crap 8.017
1
<?php
2
namespace GoetasWebservices\SoapServices\SoapClient\Arguments;
3
4
use Doctrine\Common\Inflector\Inflector;
5
use Doctrine\Instantiator\Instantiator;
6
use JMS\Serializer\Serializer;
7
8
class ArgumentsReader implements ArgumentsReaderInterface
9
{
10
    /**
11
     * @var Serializer
12
     */
13
    private $serializer;
14
15 14
    public function __construct(Serializer $serializer)
16
    {
17 14
        $this->serializer = $serializer;
18 14
    }
19
20
    /**
21
     * @param array $args
22
     * @param array $input
23
     * @return null|object
24
     */
25 12
    public function readArguments(array $args, array $input)
26
    {
27 12
        if (!count($input['parts'])) {
28 2
            return null;
29
        }
30
31 10
        $instantiator = new Instantiator();
32 10
        $factory = $this->serializer->getMetadataFactory();
33 10
        $instance = $instantiator->instantiate($input['part_fqcn']);
34 10
        $classMetadata = $factory->getMetadataForClass($input['part_fqcn']);
35
36 10
        if (count($input['parts']) > 1) {
37
38 1
            if (count($input['parts']) !== count($args)) {
39
                throw new \Exception("Expected to have exactly " . count($input['parts']) . " arguments, supplied " . count($args));
40
            }
41
42 1
            foreach ($input['parts'] as $paramName) {
43
                //@todo $propertyName should use the xsd2php naming strategy (or do in the metadata extractor)
44 1
                $propertyName = Inflector::camelize(str_replace(".", " ", $paramName));
45 1
                $propertyMetadata = $classMetadata->propertyMetadata[$propertyName];
46 1
                $propertyMetadata->setValue($instance, array_shift($args));
47 1
            }
48 1
            return $instance;
49
        }
50
51 9
        $propertyName = Inflector::camelize(str_replace(".", " ", reset($input['parts'])));
52 9
        $propertyMetadata = $classMetadata->propertyMetadata[$propertyName];
53 9
        if ($args[0] instanceof $propertyMetadata->type['name']) {
54 1
            $propertyMetadata->setValue($instance, reset($args));
55 1
            return $instance;
56
        }
57
58 8
        $instance2 = $instantiator->instantiate($propertyMetadata->type['name']);
59 8
        $classMetadata2 = $factory->getMetadataForClass($propertyMetadata->type['name']);
60 8
        $propertyMetadata->setValue($instance, $instance2);
61
62 8
        foreach ($classMetadata2->propertyMetadata as $propertyMetadata2) {
63 8
            if (!count($args)) {
64
                throw new \Exception("Not enough arguments provided. Can't fina a parameter to set " . $propertyMetadata2->name);
65
            }
66 8
            $value = array_pop($args);
67 8
            $propertyMetadata2->setValue($instance2, $value);
68 8
        }
69 8
        return $instance;
70
    }
71
}
72