Completed
Pull Request — master (#9)
by Asmir
163:43 queued 133:54
created

HeaderHandler::handleOptions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 2
crap 4
1
<?php
2
namespace GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler;
3
4
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Header;
5
use JMS\Serializer\DeserializationContext;
6
use JMS\Serializer\GraphNavigator;
7
use JMS\Serializer\Handler\SubscribingHandlerInterface;
8
use JMS\Serializer\Metadata\StaticPropertyMetadata;
9
use JMS\Serializer\SerializationContext;
10
use JMS\Serializer\XmlDeserializationVisitor;
11
use JMS\Serializer\XmlSerializationVisitor;
12
13
class HeaderHandler implements SubscribingHandlerInterface
14
{
15
    protected $headerData = [];
16
17
    public static function getSubscribingMethods()
18
    {
19
        return array(
20
            array(
21 32
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
22
                'format' => 'xml',
23
                'type' => HeaderPlaceholder::class,
24
                'method' => 'serializeHeader'
25 32
            ),
26 32
            array(
27 32
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
28
                'format' => 'xml',
29 32
                'type' => 'GoetasWebservices\SoapServices\SoapEnvelope\Headers',
30
                'method' => 'deserializeHeaders'
31 32
            ),
32 32
        );
33 32
    }
34
35 32
    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...
36 32
    {
37
        $newType = [
38
            'name' => $type['params'][0],
39 1
            'params' => []
40
        ];
41
        return $context->getNavigator()->accept($data, $newType, $context);
42 1
    }
43 1
44 1
    public function addHeaderData(HeaderPlaceholder $reference, $data)
45 1
    {
46
        $this->headerData[spl_object_hash($reference)][] = $data;
47
    }
48 2
49
    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...
50 2
    {
51 2
        if (!isset($this->headerData[spl_object_hash($data)])) {
52
            return;
53 2
        }
54
        $factory = $context->getMetadataFactory();
55 2
        /**
56
         * @var $header Header
57
         */
58 2
        foreach ($this->headerData[spl_object_hash($data)] as $header) {
59
            /**
60
             * @var $classMetadata \JMS\Serializer\Metadata\ClassMetadata
61
             */
62 2
            $classMetadata = $factory->getMetadataForClass(get_class($header->getData()));
63
64
            $metadata = new StaticPropertyMetadata($classMetadata->name, $classMetadata->xmlRootName, $header->getData());
65
            $metadata->xmlNamespace = $classMetadata->xmlRootNamespace;
66 2
            $metadata->serializedName = $classMetadata->xmlRootName ?: 'header';
67
68 2
            $visitor->visitProperty($metadata, $header->getData(), $context);
69 2
70 2
            $this->handleOptions($visitor, $header);
71
        }
72 2
    }
73
74 2
    private function handleOptions(XmlSerializationVisitor $visitor, $header)
75 2
    {
76 2
        $options = $header->getOptions();
77
        if (!count($options)) {
78 2
            return;
79
        }
80 2
        /**
81 2
         * @var $currentNode \DOMNode
82 2
         */
83
        $currentNode = $visitor->getCurrentNode();
84
        foreach ($options as $option => $value) {
85
            if (in_array($option, ['mustUnderstand', 'required', 'role', 'actor'])) {
86
87 2
                $this->setAttributeOnNode($currentNode->lastChild, $option, $value, $currentNode->ownerDocument->documentElement->namespaceURI);
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...
88 2
            }
89 2
        }
90
    }
91 2
92 1
    private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace)
93 1
    {
94 1
        if (!($prefix = $node->lookupPrefix($namespace)) && !($prefix = $node->ownerDocument->lookupPrefix($namespace))) {
95
            $prefix = 'ns-' . substr(sha1($namespace), 0, 8);
96 2
        }
97 2
        $node->setAttributeNS($namespace, $prefix . ':' . $name, is_bool($value) || is_null($value) ? ($value ? 'true' : 'false') : $value);
98 2
    }
99 2
100
}
101 2
102
103
104