Completed
Push — master ( 9aa566...61ed88 )
by Asmir
30:54
created

HeaderHandler::serializeHeader()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
rs 8.9713
cc 3
eloc 11
nc 3
nop 4
1
<?php
2
namespace GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler;
3
4
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Header;
5
use JMS\Serializer\GraphNavigator;
6
use JMS\Serializer\Handler\SubscribingHandlerInterface;
7
use JMS\Serializer\Metadata\StaticPropertyMetadata;
8
use JMS\Serializer\SerializationContext;
9
use JMS\Serializer\XmlSerializationVisitor;
10
11
class HeaderHandler implements SubscribingHandlerInterface
12
{
13
    protected $headerData = [];
14
15
    public static function getSubscribingMethods()
16
    {
17
        return array(
18
            array(
19
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
20
                'format' => 'xml',
21
                'type' => HeaderPlaceholder::class,
22
                'method' => 'serializeHeader'
23
            ),
24
        );
25
    }
26
27
    public function addHeaderData(HeaderPlaceholder $reference, $data)
28
    {
29
        $this->headerData[spl_object_hash($reference)][] = $data;
30
    }
31
32
    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...
33
    {
34
        if (!isset($this->headerData[spl_object_hash($data)])) {
35
            return;
36
        }
37
        $factory = $context->getMetadataFactory();
38
        /**
39
         * @var $header Header
40
         */
41
        foreach ($this->headerData[spl_object_hash($data)] as $header) {
42
            /**
43
             * @var $classMetadata \JMS\Serializer\Metadata\ClassMetadata
44
             */
45
            $classMetadata = $factory->getMetadataForClass(get_class($header->getData()));
46
47
            $metadata = new StaticPropertyMetadata($classMetadata->name, $classMetadata->xmlRootName, $header->getData());
48
            $metadata->xmlNamespace = $classMetadata->xmlRootNamespace;
49
            $metadata->serializedName = $classMetadata->xmlRootName;
50
51
            $visitor->visitProperty($metadata, $header->getData(), $context);
52
53
            $this->handleOptions($visitor, $header);
54
        }
55
    }
56
57
    private function handleOptions(XmlSerializationVisitor $visitor, $header)
58
    {
59
        $options = $header->getOptions();
60
        if (!count($options)) {
61
            return;
62
        }
63
        $currentNode = $visitor->getCurrentNode();
64
        foreach ($options as $option => $value) {
65
            if ($option === 'mustUnderstand' || $option === 'required') {
66
                $this->setAttributeOnNode($currentNode->lastChild, $option, "true", 'http://schemas.xmlsoap.org/soap/envelope/');
67
            }
68
        }
69
    }
70
71
    private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace)
72
    {
73
        if (!($prefix = $node->lookupPrefix($namespace)) && !($prefix = $node->ownerDocument->lookupPrefix($namespace))) {
74
            $prefix = 'ns-' . substr(sha1($namespace), 0, 8);
75
        }
76
        $node->setAttributeNS($namespace, $prefix . ':' . $name, $value);
77
    }
78
79
}
80
81
82
83