HeaderHandler   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 84.91%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 7
dl 0
loc 98
ccs 45
cts 53
cp 0.8491
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribingMethods() 0 17 1
A addHeaderData() 0 4 1
A serializeHeader() 0 26 4
A handleOptions() 0 22 5
A setAttributeOnNode() 0 7 6
A deserializeHeaders() 0 8 1
1
<?php
2
3
namespace GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler;
4
5
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Header;
6
use JMS\Serializer\DeserializationContext;
7
use JMS\Serializer\GraphNavigator;
8
use JMS\Serializer\Handler\SubscribingHandlerInterface;
9
use JMS\Serializer\Metadata\StaticPropertyMetadata;
10
use JMS\Serializer\SerializationContext;
11
use JMS\Serializer\XmlDeserializationVisitor;
12
use JMS\Serializer\XmlSerializationVisitor;
13
use Symfony\Component\DependencyInjection\SimpleXMLElement;
14
15
class HeaderHandler implements SubscribingHandlerInterface
16
{
17
    const SOAP = 'http://schemas.xmlsoap.org/soap/envelope/';
18
    const SOAP_12 = 'http://www.w3.org/2003/05/soap-envelope';
19
20
    protected $headerData = [];
21
22 36
    public static function getSubscribingMethods()
23
    {
24
        return array(
25
            array(
26 36
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
27 36
                'format' => 'xml',
28 36
                'type' => HeaderPlaceholder::class,
29
                'method' => 'serializeHeader'
30 36
            ),
31
            array(
32 36
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
33 36
                'format' => 'xml',
34 36
                'type' => 'GoetasWebservices\SoapServices\SoapEnvelope\Headers',
35
                'method' => 'deserializeHeaders'
36 36
            ),
37 36
        );
38
    }
39
40
    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...
41
    {
42
        $newType = [
43
            'name' => $type['params'][0],
44
            'params' => []
45
        ];
46
        return $context->getNavigator()->accept($data, $newType, $context);
0 ignored issues
show
Unused Code introduced by
The call to GraphNavigatorInterface::accept() has too many arguments starting with $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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
    }
48
49 2
    public function addHeaderData(HeaderPlaceholder $reference, $data)
50
    {
51 2
        $this->headerData[spl_object_hash($reference)][] = $data;
52 2
    }
53
54 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...
55
    {
56 2
        if (!isset($this->headerData[spl_object_hash($data)])) {
57
            return;
58
        }
59 2
        $factory = $context->getMetadataFactory();
60
        /**
61
         * @var $header Header
62
         */
63 2
        foreach ($this->headerData[spl_object_hash($data)] as $header) {
64
            /**
65
             * @var $classMetadata \JMS\Serializer\Metadata\ClassMetadata
66
             */
67 2
            $classMetadata = $factory->getMetadataForClass(get_class($header->getData()));
68
69 2
            $name = ($pos = strpos($classMetadata->xmlRootName, ':')) !== false ? substr($classMetadata->xmlRootName, $pos + 1) : $classMetadata->xmlRootName;
70
71 2
            $metadata = new StaticPropertyMetadata($classMetadata->name, $name, $header->getData());
72 2
            $metadata->xmlNamespace = $classMetadata->xmlRootNamespace;
73 2
            $metadata->serializedName = $name;
74
75 2
            $visitor->visitProperty($metadata, $header->getData(), $context);
0 ignored issues
show
Unused Code introduced by
The call to XmlSerializationVisitor::visitProperty() has too many arguments starting with $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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
76
77 2
            $this->handleOptions($visitor, $header);
78 2
        }
79 2
    }
80
81 2
    private function handleOptions(XmlSerializationVisitor $visitor, $header)
82
    {
83 2
        $options = $header->getOptions();
84 2
        if (!count($options)) {
85 2
            return;
86
        }
87
        /**
88
         * @var $currentNode \DOMNode
89
         */
90 2
        $currentNode = $visitor->getCurrentNode();
91 2
        foreach ($options as $option => $value) {
92 2
            if (in_array($option, ['mustUnderstand', 'required', 'role', 'actor'])) {
93
94 2
                if ($currentNode->ownerDocument->documentElement->namespaceURI === self::SOAP_12) {
95 1
                    $envelopeNS = self::SOAP_12;
96 1
                } else {
97 1
                    $envelopeNS = self::SOAP;
98
                }
99 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...
100 2
            }
101 2
        }
102 2
    }
103
104 2
    private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace)
105
    {
106 2
        if (!($prefix = $node->lookupPrefix($namespace)) && !($prefix = $node->ownerDocument->lookupPrefix($namespace))) {
107
            $prefix = 'ns-' . substr(sha1($namespace), 0, 8);
108
        }
109 2
        $node->setAttributeNS($namespace, $prefix . ':' . $name, is_bool($value) || is_null($value) ? ($value ? 'true' : 'false') : $value);
110 2
    }
111
112
}
113
114
115
116