SoapSerializationVisitorFactory::addXmlElement()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace DMT\Soap\Serializer;
4
5
use DOMNode;
6
use JMS\Serializer\Exception\InvalidArgumentException;
7
use JMS\Serializer\Visitor\Factory\SerializationVisitorFactory;
8
use JMS\Serializer\Visitor\SerializationVisitorInterface;
9
use JMS\Serializer\XmlSerializationVisitor;
10
11
/**
12
 * CLass SoapSerializationVisitorFactory
13
 *
14
 * @package DMT\Soap
15
 */
16
final class SoapSerializationVisitorFactory implements SerializationVisitorFactory, SoapNamespaceInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $defaultVersion = '1.0';
22
23
    /**
24
     * @var string
25
     */
26
    private $defaultEncoding = 'UTF-8';
27
28
    /**
29
     * @var bool
30
     */
31
    private $formatOutput = true;
32
33
    /**
34
     * @var int
35
     */
36
    private $soapVersion = self::SOAP_1_1;
37
38 11
    public function getVisitor(): SerializationVisitorInterface
39
    {
40 11
        $visitor = new XmlSerializationVisitor(
41 11
            $this->formatOutput,
42 11
            $this->defaultEncoding,
43 11
            $this->defaultVersion
44
        );
45
46 11
        $envelope = $visitor->createRoot(null, 'Envelope', $this->getSoapNamespace(), 'soap');
47
48 10
        $visitor->setCurrentNode($this->addXmlElement($envelope, 'Body', $this->getSoapNamespace()));
49
50 10
        return $visitor;
51
    }
52
53 3
    public function setSoapVersion(int $soapVersion): self
54
    {
55 3
        $this->soapVersion = $soapVersion;
56 3
        return $this;
57
    }
58
59 1
    public function setDefaultVersion(string $version): self
60
    {
61 1
        $this->defaultVersion = $version;
62 1
        return $this;
63
    }
64
65 1
    public function setDefaultEncoding(string $encoding): self
66
    {
67 1
        $this->defaultEncoding = $encoding;
68 1
        return $this;
69
    }
70
71 1
    public function setFormatOutput(bool $formatOutput): self
72
    {
73 1
        $this->formatOutput = $formatOutput;
74 1
        return $this;
75
    }
76
77
    /**
78
     * Get the namespace for the current SOAP version.
79
     *
80
     * @return string
81
     * @throws InvalidArgumentException
82
     */
83 11
    protected function getSoapNamespace(): string
84
    {
85 11
        if (!array_key_exists($this->soapVersion, static::SOAP_NAMESPACES)) {
86 1
            throw new InvalidArgumentException('Unsupported SOAP version');
87
        }
88
89 10
        return static::SOAP_NAMESPACES[$this->soapVersion];
90
    }
91
92
    /**
93
     * Wraps XmlElement(s) that will hold the SOAP message.
94
     *
95
     * @param DOMNode $parentNode
96
     * @param string $nodeName
97
     * @param string $namespace
98
     *
99
     * @return DOMNode
100
     */
101 10
    protected function addXmlElement(DOMNode $parentNode, string $nodeName, string $namespace): DOMNode
102
    {
103 10
        return $parentNode->appendChild(
104 10
            ($parentNode->ownerDocument ?? $parentNode)->createElementNS($namespace, $nodeName)
105
        );
106
    }
107
}
108