Completed
Pull Request — master (#11)
by Bas
06:18
created

SoapSerializationVisitorFactory::getVisitor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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