Completed
Push — master ( cb1e89...5328e2 )
by Asmir
45:55 queued 39:45
created

ArgumentsReader::readArguments()   C

Complexity

Conditions 13
Paths 25

Size

Total Lines 78
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 13.1417

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 48
cts 53
cp 0.9057
rs 5.1663
c 0
b 0
f 0
cc 13
eloc 48
nc 25
nop 2
crap 13.1417

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace GoetasWebservices\SoapServices\SoapClient\Arguments;
3
4
use Doctrine\Common\Inflector\Inflector;
5
use Doctrine\Instantiator\Instantiator;
6
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler\HeaderHandler;
7
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler\HeaderPlaceholder;
8
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Header;
9
use JMS\Serializer\Serializer;
10
11
class ArgumentsReader implements ArgumentsReaderInterface
12
{
13
    /**
14
     * @var Serializer
15
     */
16
    private $serializer;
17
    /**
18
     * @var HeaderHandler
19
     */
20
    private $headerHandler;
21
22 16
    public function __construct(Serializer $serializer, HeaderHandler $headerHandler)
23
    {
24 16
        $this->serializer = $serializer;
25 16
        $this->headerHandler = $headerHandler;
26 16
    }
27
28
    /**
29
     * @param array $args
30
     * @param array $input
31
     * @return null|object
32
     */
33 14
    public function readArguments(array $args, array $input)
34
    {
35
36
        $envelope = array_filter($args, function ($item) use ($input) {
37 14
            return $item instanceof $input['message_fqcn'];
38 14
        });
39 14
        if ($envelope) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $envelope of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
            return reset($envelope);
41
        }
42
43 14
        $instantiator = new Instantiator();
44 14
        $envelope = $instantiator->instantiate($input['message_fqcn']);
45
46
        $headers = array_filter($args, function ($item) use ($input) {
47 14
            return $item instanceof $input['headers_fqcn'];
48 14
        });
49 14
        if ($headers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50
            $envelope->setHeader(reset($headers));
51
        } else {
52
53
            $headers = array_filter($args, function ($item) {
54 14
                return $item instanceof Header;
55 14
            });
56 14
            if (count($headers)) {
57 1
                $headerPlaceholder = new HeaderPlaceholder();
58 1
                foreach ($headers as $headerInfo) {
59 1
                    $this->headerHandler->addHeaderData($headerPlaceholder, $headerInfo);
60 1
                }
61 1
                $envelope->setHeader($headerPlaceholder);
62 1
            }
63
        }
64
65 14
        $args = array_filter($args, function ($item) use ($input) {
66 14
            return !($item instanceof Header) && !($item instanceof $input['headers_fqcn']);
67 14
        });
68
69 14
        if (!count($input['parts'])) {
70 2
            return $envelope;
71
        }
72
73 12
        $body = $instantiator->instantiate($input['part_fqcn']);
74 12
        $envelope->setBody($body);
75 12
        $factory = $this->serializer->getMetadataFactory();
76 12
        $classMetadata = $factory->getMetadataForClass($input['part_fqcn']);
77
78 12
        if (count($input['parts']) > 1) {
79
80 1
            if (count($input['parts']) !== count($args)) {
81
                throw new \Exception("Expected to have exactly " . count($input['parts']) . " arguments, supplied " . count($args));
82
            }
83
84 1
            foreach ($input['parts'] as $paramName => $elementName) {
85 1
                $propertyMetadata = $classMetadata->propertyMetadata[$paramName];
86 1
                $propertyMetadata->setValue($body, array_shift($args));
87 1
            }
88 1
            return $envelope;
89
        }
90
91 11
        $propertyName = key($input['parts']);
92 11
        $propertyMetadata = $classMetadata->propertyMetadata[$propertyName];
93 11
        if ($args[0] instanceof $propertyMetadata->type['name']) {
94 1
            $propertyMetadata->setValue($body, reset($args));
95 1
            return $envelope;
96
        }
97
98 10
        $instance2 = $instantiator->instantiate($propertyMetadata->type['name']);
99 10
        $classMetadata2 = $factory->getMetadataForClass($propertyMetadata->type['name']);
100 10
        $propertyMetadata->setValue($body, $instance2);
101
102 10
        foreach ($classMetadata2->propertyMetadata as $propertyMetadata2) {
103 10
            if (!count($args)) {
104
                throw new \Exception("Not enough arguments provided. Can't fina a parameter to set " . $propertyMetadata2->name);
105
            }
106 10
            $value = array_pop($args);
107 10
            $propertyMetadata2->setValue($instance2, $value);
108 10
        }
109 10
        return $envelope;
110
    }
111
}
112