Passed
Push — master ( 2944ee...dfe2d2 )
by Asmir
02:04
created

HeaderHandler   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 54
c 1
b 0
f 0
dl 0
loc 121
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribingMethods() 0 26 1
A serializeHeader() 0 18 2
A handleOptions() 0 19 5
A deserializeHeader() 0 18 4
A deserializeHeaderPlaceholder() 0 5 1
A setAttributeOnNode() 0 7 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GoetasWebservices\SoapServices\Metadata\Arguments\Headers\Handler;
6
7
use GoetasWebservices\SoapServices\Metadata\Arguments\Headers\Header;
8
use GoetasWebservices\SoapServices\Metadata\Arguments\Headers\HeaderBag;
9
use JMS\Serializer\DeserializationContext;
10
use JMS\Serializer\GraphNavigator;
11
use JMS\Serializer\Handler\SubscribingHandlerInterface;
12
use JMS\Serializer\Metadata\StaticPropertyMetadata;
13
use JMS\Serializer\SerializationContext;
14
use JMS\Serializer\XmlDeserializationVisitor;
15
use JMS\Serializer\XmlSerializationVisitor;
16
17
class HeaderHandler implements SubscribingHandlerInterface
18
{
19
    public const SOAP = 'http://schemas.xmlsoap.org/soap/envelope/';
20
    public const SOAP_12 = 'http://www.w3.org/2003/05/soap-envelope';
21
22
    public static function getSubscribingMethods(): array
23
    {
24
        return [
25
            [
26
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
27
                'format' => 'xml',
28
                'type' => HeaderPlaceholder::class,
29
                'method' => 'serializeHeaderPlaceholder',
30
            ],
31
            [
32
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
33
                'format' => 'xml',
34
                'type' => HeaderPlaceholder::class,
35
                'method' => 'deserializeHeaderPlaceholder',
36
            ],
37
            [
38
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
39
                'format' => 'xml',
40
                'type' => 'GoetasWebservices\SoapServices\SoapEnvelope\Header',
41
                'method' => 'deserializeHeader',
42
            ],
43
            [
44
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
45
                'format' => 'xml',
46
                'type' => Header::class,
47
                'method' => 'serializeHeader',
48
            ],
49
        ];
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55
    public function deserializeHeaderPlaceholder(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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    public function deserializeHeaderPlaceholder(/** @scrutinizer ignore-unused */ XmlDeserializationVisitor $visitor, \SimpleXMLElement $data, array $type, DeserializationContext $context)

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

Loading history...
56
    {
57
        $type = ['name' => $type['params'][0], 'params' => []];
58
59
        return $context->getNavigator()->accept($data, $type, $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\GraphNavigatorInterface::accept() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        return $context->getNavigator()->/** @scrutinizer ignore-call */ accept($data, $type, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65
    public function deserializeHeader(XmlDeserializationVisitor $visitor, \SimpleXMLElement $data, array $type, DeserializationContext $context)
66
    {
67
        $type = ['name' => $type['params'][0], 'params' => []];
68
69
        $return = $context->getNavigator()->accept($data, $type, $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\GraphNavigatorInterface::accept() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        $return = $context->getNavigator()->/** @scrutinizer ignore-call */ accept($data, $type, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
70
71
        $mustUnderstandAttr = $data->attributes(self::SOAP_12)->mustUnderstand ?: $data->attributes(self::SOAP)->mustUnderstand;
72
        $mustUnderstand = null !== $mustUnderstandAttr && $visitor->visitBoolean($mustUnderstandAttr, [], $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\XmlDeseri...Visitor::visitBoolean() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        $mustUnderstand = null !== $mustUnderstandAttr && $visitor->/** @scrutinizer ignore-call */ visitBoolean($mustUnderstandAttr, [], $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
73
        $headerBag = $context->getAttribute('headers_bag');
74
        \assert($headerBag instanceof HeaderBag);
75
76
        if ($mustUnderstand) {
77
            $headerBag->addMustUnderstandHeader($return);
78
        } else {
79
            $headerBag->addHeader($return);
80
        }
81
82
        return $return;
83
    }
84
85
    public function serializeHeader(XmlSerializationVisitor $visitor, Header $header, array $type, SerializationContext $context): void
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

85
    public function serializeHeader(XmlSerializationVisitor $visitor, Header $header, /** @scrutinizer ignore-unused */ array $type, SerializationContext $context): void

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

Loading history...
86
    {
87
        $factory = $context->getMetadataFactory();
88
89
        /**
90
         * @var $classMetadata \JMS\Serializer\Metadata\ClassMetadata
91
         */
92
        $classMetadata = $factory->getMetadataForClass(get_class($header->getData()));
93
94
        $name = false !== ($pos = strpos($classMetadata->xmlRootName, ':')) ? substr($classMetadata->xmlRootName, $pos + 1) : $classMetadata->xmlRootName;
95
96
        $metadata = new StaticPropertyMetadata($classMetadata->name, $name, $header->getData());
97
        $metadata->xmlNamespace = $classMetadata->xmlRootNamespace;
98
        $metadata->serializedName = $name;
99
100
        $visitor->visitProperty($metadata, $header->getData(), $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\XmlSerial...isitor::visitProperty() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
        $visitor->/** @scrutinizer ignore-call */ 
101
                  visitProperty($metadata, $header->getData(), $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
101
102
        $this->handleOptions($visitor, $header->getOptions());
103
    }
104
105
    private function handleOptions(XmlSerializationVisitor $visitor, array $options): void
106
    {
107
        if (!count($options)) {
108
            return;
109
        }
110
111
        /**
112
         * @var $currentNode \DOMNode
113
         */
114
        $currentNode = $visitor->getCurrentNode();
115
        foreach ($options as $option => $value) {
116
            if (in_array($option, ['mustUnderstand', 'required', 'role', 'actor'])) {
117
                if (self::SOAP_12 === $currentNode->ownerDocument->documentElement->namespaceURI) {
118
                    $envelopeNS = self::SOAP_12;
119
                } else {
120
                    $envelopeNS = self::SOAP;
121
                }
122
123
                $this->setAttributeOnNode($currentNode->lastChild, $option, $value, $envelopeNS);
124
            }
125
        }
126
    }
127
128
    /**
129
     * @param mixed $value
130
     */
131
    private function setAttributeOnNode(\DOMElement $node, string $name, $value, string $namespace): void
132
    {
133
        if (!($prefix = $node->lookupPrefix($namespace)) && !($prefix = $node->ownerDocument->lookupPrefix($namespace))) {
134
            $prefix = 'ns-' . substr(sha1($namespace), 0, 8);
135
        }
136
137
        $node->setAttributeNS($namespace, $prefix . ':' . $name, is_bool($value) || null === $value ? ($value ? 'true' : 'false') : $value);
138
    }
139
}
140