Completed
Push — master ( f27e2e...5dd696 )
by Asmir
10s
created

HeaderHandler   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 94.23%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 7
dl 0
loc 96
ccs 49
cts 52
cp 0.9423
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribingMethods() 0 17 1
A deserializeHeaders() 0 8 1
A addHeaderData() 0 4 1
B serializeHeader() 0 24 3
B handleOptions() 0 22 5
B setAttributeOnNode() 0 7 6
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
use Symfony\Component\DependencyInjection\SimpleXMLElement;
13
14
class HeaderHandler implements SubscribingHandlerInterface
15
{
16
    const SOAP = 'http://schemas.xmlsoap.org/soap/envelope/';
17
    const SOAP_12 = 'http://www.w3.org/2003/05/soap-envelope';
18
19
    protected $headerData = [];
20
21 32
    public static function getSubscribingMethods()
22
    {
23
        return array(
24
            array(
25 32
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
26 32
                'format' => 'xml',
27 32
                'type' => HeaderPlaceholder::class,
28
                'method' => 'serializeHeader'
29 32
            ),
30
            array(
31 32
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
32 32
                'format' => 'xml',
33 32
                'type' => 'GoetasWebservices\SoapServices\SoapEnvelope\Headers',
34
                'method' => 'deserializeHeaders'
35 32
            ),
36 32
        );
37
    }
38
39 1
    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...
40
    {
41
        $newType = [
42 1
            'name' => $type['params'][0],
43 1
            'params' => []
44 1
        ];
45 1
        return $context->getNavigator()->accept($data, $newType, $context);
46
    }
47
48 2
    public function addHeaderData(HeaderPlaceholder $reference, $data)
49
    {
50 2
        $this->headerData[spl_object_hash($reference)][] = $data;
51 2
    }
52
53 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...
54
    {
55 2
        if (!isset($this->headerData[spl_object_hash($data)])) {
56
            return;
57
        }
58 2
        $factory = $context->getMetadataFactory();
59
        /**
60
         * @var $header Header
61
         */
62 2
        foreach ($this->headerData[spl_object_hash($data)] as $header) {
63
            /**
64
             * @var $classMetadata \JMS\Serializer\Metadata\ClassMetadata
65
             */
66 2
            $classMetadata = $factory->getMetadataForClass(get_class($header->getData()));
67
68 2
            $metadata = new StaticPropertyMetadata($classMetadata->name, $classMetadata->xmlRootName, $header->getData());
69 2
            $metadata->xmlNamespace = $classMetadata->xmlRootNamespace;
70 2
            $metadata->serializedName = $classMetadata->xmlRootName;
71
72 2
            $visitor->visitProperty($metadata, $header->getData(), $context);
73
74 2
            $this->handleOptions($visitor, $header);
75 2
        }
76 2
    }
77
78 2
    private function handleOptions(XmlSerializationVisitor $visitor, $header)
79
    {
80 2
        $options = $header->getOptions();
81 2
        if (!count($options)) {
82 2
            return;
83
        }
84
        /**
85
         * @var $currentNode \DOMNode
86
         */
87 2
        $currentNode = $visitor->getCurrentNode();
88 2
        foreach ($options as $option => $value) {
89 2
            if (in_array($option, ['mustUnderstand', 'required', 'role', 'actor'])) {
90
91 2
                if ($currentNode->ownerDocument->documentElement->namespaceURI === self::SOAP_12) {
92 1
                    $envelopeNS = self::SOAP_12;
93 1
                } else {
94 1
                    $envelopeNS = self::SOAP;
95
                }
96 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...
97 2
            }
98 2
        }
99 2
    }
100
101 2
    private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace)
102
    {
103 2
        if (!($prefix = $node->lookupPrefix($namespace)) && !($prefix = $node->ownerDocument->lookupPrefix($namespace))) {
104
            $prefix = 'ns-' . substr(sha1($namespace), 0, 8);
105
        }
106 2
        $node->setAttributeNS($namespace, $prefix . ':' . $name, is_bool($value) || is_null($value) ? ($value ? 'true' : 'false') : $value);
107 2
    }
108
109
}
110
111
112
113