Completed
Push — master ( 4be915...c4363c )
by Asmir
06:33
created

HeaderHandler::deserializeHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
namespace GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler;
4
5
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Header;
6
use JMS\Serializer\DeserializationContext;
7
use JMS\Serializer\GraphNavigator;
8
use JMS\Serializer\Handler\SubscribingHandlerInterface;
9
use JMS\Serializer\Metadata\StaticPropertyMetadata;
10
use JMS\Serializer\SerializationContext;
11
use JMS\Serializer\XmlDeserializationVisitor;
12
use JMS\Serializer\XmlSerializationVisitor;
13
use Symfony\Component\DependencyInjection\SimpleXMLElement;
14
15
class HeaderHandler implements SubscribingHandlerInterface
16
{
17
    const SOAP = 'http://schemas.xmlsoap.org/soap/envelope/';
18
    const SOAP_12 = 'http://www.w3.org/2003/05/soap-envelope';
19
20
    protected $headerData = [];
21
22 32
    public static function getSubscribingMethods()
23
    {
24
        return array(
25
            array(
26 32
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
27 32
                'format' => 'xml',
28 32
                'type' => HeaderPlaceholder::class,
29
                'method' => 'serializeHeader'
30 32
            ),
31
            array(
32 32
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
33 32
                'format' => 'xml',
34 32
                'type' => 'GoetasWebservices\SoapServices\SoapEnvelope\Headers',
35
                'method' => 'deserializeHeaders'
36 32
            ),
37 32
        );
38
    }
39
40
    public function deserializeHeaders(XmlDeserializationVisitor $visitor, \SimpleXMLElement $data, array $type, DeserializationContext $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        $newType = [
43
            'name' => $type['params'][0],
44
            'params' => []
45
        ];
46
        return $context->getNavigator()->accept($data, $newType, $context);
47
    }
48
49 2
    public function addHeaderData(HeaderPlaceholder $reference, $data)
50
    {
51 2
        $this->headerData[spl_object_hash($reference)][] = $data;
52 2
    }
53
54 2
    public function serializeHeader(XmlSerializationVisitor $visitor, HeaderPlaceholder $data, array $type, SerializationContext $context)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56 2
        if (!isset($this->headerData[spl_object_hash($data)])) {
57
            return;
58
        }
59 2
        $factory = $context->getMetadataFactory();
60
        /**
61
         * @var $header Header
62
         */
63 2
        foreach ($this->headerData[spl_object_hash($data)] as $header) {
64
            /**
65
             * @var $classMetadata \JMS\Serializer\Metadata\ClassMetadata
66
             */
67 2
            $classMetadata = $factory->getMetadataForClass(get_class($header->getData()));
68
69 2
            $name = ($pos = strpos($classMetadata->xmlRootName, ':')) !== false ? substr($classMetadata->xmlRootName, $pos + 1) : $classMetadata->xmlRootName;
70
71 2
            $metadata = new StaticPropertyMetadata($classMetadata->name, $name, $header->getData());
72 2
            $metadata->xmlNamespace = $classMetadata->xmlRootNamespace;
73 2
            $metadata->serializedName = $name;
74
75 2
            $visitor->visitProperty($metadata, $header->getData(), $context);
76
77 2
            $this->handleOptions($visitor, $header);
78 2
        }
79 2
    }
80
81 2
    private function handleOptions(XmlSerializationVisitor $visitor, $header)
82
    {
83 2
        $options = $header->getOptions();
84 2
        if (!count($options)) {
85 2
            return;
86
        }
87
        /**
88
         * @var $currentNode \DOMNode
89
         */
90 2
        $currentNode = $visitor->getCurrentNode();
91 2
        foreach ($options as $option => $value) {
92 2
            if (in_array($option, ['mustUnderstand', 'required', 'role', 'actor'])) {
93
94 2
                if ($currentNode->ownerDocument->documentElement->namespaceURI === self::SOAP_12) {
95 1
                    $envelopeNS = self::SOAP_12;
96 1
                } else {
97 1
                    $envelopeNS = self::SOAP;
98
                }
99 2
                $this->setAttributeOnNode($currentNode->lastChild, $option, $value, $envelopeNS);
0 ignored issues
show
Compatibility introduced by
$currentNode->lastChild of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
100 2
            }
101 2
        }
102 2
    }
103
104 2
    private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace)
105
    {
106 2
        if (!($prefix = $node->lookupPrefix($namespace)) && !($prefix = $node->ownerDocument->lookupPrefix($namespace))) {
107
            $prefix = 'ns-' . substr(sha1($namespace), 0, 8);
108
        }
109 2
        $node->setAttributeNS($namespace, $prefix . ':' . $name, is_bool($value) || is_null($value) ? ($value ? 'true' : 'false') : $value);
110 2
    }
111
112
}
113
114
115
116