Passed
Push — master ( e28714...75c076 )
by Daniel
02:16 queued 12s
created

NovaParameterWriter::appendToDocument()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
namespace OrcaServices\NovaApi\Soap;
4
5
use DOMDocument;
6
use DOMElement;
7
8
class NovaParameterWriter
9
{
10
    /**
11
     * Dom document
12
     *
13
     * @var DOMDocument
14
     */
15
    private $document;
16
17
    /**
18
     * Parent element
19
     *
20
     * @var DOMElement
21
     */
22
    private $parent;
23
24
    /**
25
     * Parameter prefix
26
     *
27
     * @var string
28
     */
29
    private $parameterPrefix;
30
31
    /**
32
     * NovaParameterWriter constructor.
33
     *
34
     * @param DOMDocument $document DOM Document
35
     * @param DOMElement $parent Parent element
36
     * @param string $namespace Parameter namespace
37
     */
38 3
    public function __construct(DOMDocument $document, DOMElement $parent, string $namespace = 'novagp:')
39
    {
40 3
        $this->document = $document;
41 3
        $this->parent = $parent;
42 3
        $this->parameterPrefix = $namespace;
43 3
    }
44
45
    /**
46
     * Appends the given parameters to the document inside the parent element.
47
     *
48
     * @param NovaParameterMap $parameterMap Parameter map
49
     * @return void
50
     */
51 3
    public function appendToDocument(NovaParameterMap $parameterMap)
52
    {
53 3
        foreach ($parameterMap->map as $parameterName => $value) {
54 3
            if (!$value) {
55 3
                continue;
56
            }
57
58 3
            $element = $this->document->createElement("{$this->parameterPrefix}{$parameterName}");
59 3
            $this->parent->appendChild($element);
60 3
            $element->appendChild($this->document->createTextNode($value));
61
        }
62 3
    }
63
}
64