Passed
Branch reduce-soap-envelope-builder-c... (c98539)
by Rait
02:14
created

SoapEnvelope::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Raigu\XRoad\SoapEnvelope;
4
5
use Raigu\XRoad\SoapEnvelope\Element\DOMElementInjection;
6
use Raigu\XRoad\SoapEnvelope\Element\XmlInjectable;
7
8
/**
9
 * I am a SOAP Envelope.
10
 *
11
 * I can construct myself from given elements and return myself as string.
12
 */
13
final class SoapEnvelope
14
{
15
    /**
16
     * @var XmlInjectable[]
17
     */
18
    private $elements;
19
20
    /**
21
     * Return SOAP envelope
22
     *
23
     * @return string
24
     */
25 2
    public function asStr(): string
26
    {
27
        $envelope = <<<EOD
28 2
<?xml version="1.0" encoding="UTF-8"?>
29
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
30
                   xmlns:id="http://x-road.eu/xsd/identifiers"
31
                   xmlns:xrd="http://x-road.eu/xsd/xroad.xsd">
32
    <env:Header/>
33
    <env:Body/>
34
</env:Envelope>
35
EOD;
36
37 2
        $dom = new \DOMDocument();
38 2
        $dom->loadXML($envelope);
39
40 2
        foreach ($this->elements as $element) {
41 2
            $element->inject($dom);
42
        }
43
44 2
        return $dom->saveXML();
45
    }
46
47
    /**
48
     * @param XmlInjectable[] $elements
49
     */
50 2
    public function __construct(XmlInjectable ...$elements)
51
    {
52 2
        $this->elements = $elements;
53
54 2
        $this->elements[] = new DOMElementInjection(
55 2
            'http://schemas.xmlsoap.org/soap/envelope/',
56 2
            'Header',
57 2
            new \DOMElement(
58 2
                'protocolVersion',
59 2
                '4.0',
60 2
                'http://x-road.eu/xsd/xroad.xsd'
61
            )
62
        );
63 2
    }
64
}
65