Completed
Push — soap-12 ( 6e3908...80b930 )
by Asmir
26:02
created

HeaderHandler::handleOptions()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 2
crap 5
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
    const SOAP = 'http://schemas.xmlsoap.org/soap/envelope/';
14
    const SOAP_12 = 'http://www.w3.org/2003/05/soap-envelope';
15
16
    protected $headerData = [];
17
18 28
    public static function getSubscribingMethods()
19
    {
20
        return array(
21
            array(
22 28
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
23 28
                'format' => 'xml',
24 28
                'type' => HeaderPlaceholder::class,
25
                'method' => 'serializeHeader'
26 28
            ),
27 28
        );
28
    }
29
30 2
    public function addHeaderData(HeaderPlaceholder $reference, $data)
31
    {
32 2
        $this->headerData[spl_object_hash($reference)][] = $data;
33 2
    }
34
35 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...
36
    {
37 2
        if (!isset($this->headerData[spl_object_hash($data)])) {
38
            return;
39
        }
40 2
        $factory = $context->getMetadataFactory();
41
        /**
42
         * @var $header Header
43
         */
44 2
        foreach ($this->headerData[spl_object_hash($data)] as $header) {
45
            /**
46
             * @var $classMetadata \JMS\Serializer\Metadata\ClassMetadata
47
             */
48 2
            $classMetadata = $factory->getMetadataForClass(get_class($header->getData()));
49
50 2
            $metadata = new StaticPropertyMetadata($classMetadata->name, $classMetadata->xmlRootName, $header->getData());
51 2
            $metadata->xmlNamespace = $classMetadata->xmlRootNamespace;
52 2
            $metadata->serializedName = $classMetadata->xmlRootName;
53
54 2
            $visitor->visitProperty($metadata, $header->getData(), $context);
55
56 2
            $this->handleOptions($visitor, $header);
57 2
        }
58 2
    }
59
60 2
    private function handleOptions(XmlSerializationVisitor $visitor, $header)
61
    {
62 2
        $options = $header->getOptions();
63 2
        if (!count($options)) {
64 2
            return;
65
        }
66
        /**
67
         * @var $currentNode \DOMNode
68
         */
69 2
        $currentNode = $visitor->getCurrentNode();
70 2
        foreach ($options as $option => $value) {
71 2
            if (in_array($option, ['mustUnderstand', 'required', 'role', 'actor'])) {
72
73 2
                if ($currentNode->ownerDocument->documentElement->namespaceURI === self::SOAP_12) {
74 1
                    $envelopeNS = self::SOAP_12;
75 1
                } else {
76 1
                    $envelopeNS = self::SOAP;
77
                }
78 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...
79 2
            }
80 2
        }
81 2
    }
82
83 2
    private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace)
84
    {
85 2
        if (!($prefix = $node->lookupPrefix($namespace)) && !($prefix = $node->ownerDocument->lookupPrefix($namespace))) {
86
            $prefix = 'ns-' . substr(sha1($namespace), 0, 8);
87
        }
88 2
        $node->setAttributeNS($namespace, $prefix . ':' . $name, is_bool($value) || is_null($value) ? ($value ? 'true' : 'false') : $value);
89 2
    }
90
91
}
92
93
94
95