Passed
Pull Request — master (#27)
by
unknown
02:19
created

SoapHeaderEventSubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 81
ccs 25
cts 26
cp 0.9615
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getSubscribedEvents() 0 7 1
A hasSoapHeader() 0 3 1
A addSoapHeader() 0 34 4
1
<?php
2
3
namespace DMT\Soap\Serializer;
4
5
use DOMDocument;
6
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
7
use JMS\Serializer\EventDispatcher\ObjectEvent;
8
use JMS\Serializer\Metadata\ClassMetadata;
9
use JMS\Serializer\SerializationContext;
10
use JMS\Serializer\XmlSerializationVisitor;
11
12
/**
13
 * Class SoapHeaderEventSubscriber
14
 *
15
 * @package DMT\Soap
16
 */
17
class SoapHeaderEventSubscriber implements EventSubscriberInterface
18
{
19
    /**
20
     * @var SoapHeaderInterface
21
     */
22
    protected $header;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public static function getSubscribedEvents(): array
28
    {
29
        return [
30
            [
31 1
                'event' => 'serializer.post_serialize',
32
                'method' => 'addSoapHeader',
33
                'format' => 'soap',
34
            ],
35
        ];
36
    }
37
38
    /**
39
     * EventSubscriber constructor.
40
     *
41
     * @param SoapHeaderInterface $header
42
     */
43 1
    public function __construct(SoapHeaderInterface $header)
44
    {
45 1
        $this->header = $header;
46 1
    }
47
48
    /**
49
     * @param ObjectEvent $event
50
     */
51 1
    public function addSoapHeader(ObjectEvent $event)
52
    {
53
        /** @var SerializationContext $context */
54 1
        $context = $event->getContext();
55
        /** @var XmlSerializationVisitor $visitor */
56 1
        $visitor = $event->getVisitor();
57
        /** @var DOMDocument $document */
58 1
        $document = $visitor->getDocument();
59
60 1
        if (!$this->hasSoapHeader($document) && $context->getDepth() === 0) {
61
            /** @var ClassMetadata $metadata */
62 1
            $metadata = $context->getMetadataFactory()->getMetadataForClass(get_class($this->header));
63
64 1
            $visitor->setCurrentNode($document->firstChild);
0 ignored issues
show
Bug introduced by
It seems like $document->firstChild can also be of type null; however, parameter $node of JMS\Serializer\XmlSerial...sitor::setCurrentNode() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

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

64
            $visitor->setCurrentNode(/** @scrutinizer ignore-type */ $document->firstChild);
Loading history...
65 1
            $visitor->setCurrentNode(
66 1
                $header = $document->firstChild->insertBefore(
0 ignored issues
show
Bug introduced by
The method insertBefore() does not exist on null. ( Ignorable by Annotation )

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

66
                $header = $document->firstChild->/** @scrutinizer ignore-call */ insertBefore(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67 1
                    $document->createElementNS($document->lookupNamespaceUri('soap'), 'Header'),
68 1
                    $document->firstChild->firstChild
69
                )
70
            );
71
            
72 1
            if(null !== $metadata->xmlRootPrefix){
73
                $tagName = $metadata->xmlRootPrefix . ':' . $metadata->xmlRootName;
74
            }else{
75 1
                $tagName = $metadata->xmlRootName;
76
            }
77
           
78 1
            $visitor->setCurrentNode(
79 1
                $header->appendChild(
80 1
                    $document->createElementNS($metadata->xmlRootNamespace, $tagName)
81
                )
82
            );
83
84 1
            $context->getNavigator()->accept($this->header, null);
85
        }
86 1
    }
87
88
    /**
89
     * Check if the header is already added.
90
     *
91
     * @param DOMDocument $document
92
     *
93
     * @return bool
94
     */
95 1
    protected function hasSoapHeader(DOMDocument $document): bool
96
    {
97 1
        return $document->getElementsByTagNameNS($document->lookupNamespaceUri('soap'), 'Header')->length > 0;
98
    }
99
}
100