DeferredFragmentInjection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Raigu\XRoad\SoapEnvelope\Element;
4
5
use Closure;
6
use DOMDocument;
7
8
/**
9
 * I append XML fragment as child to referred node.
10
 * I defer fragment creation to the last moment before appending.
11
 */
12
final class DeferredFragmentInjection implements XmlInjectable
13
{
14
    /**
15
     * @var string
16
     */
17
    private $parentNS;
18
    /**
19
     * @var string
20
     */
21
    private $parentTagName;
22
    /**
23
     * @var Closure
24
     */
25
    private $fragment;
26
27 7
    public function inject(DOMDocument $dom): void
28
    {
29 7
        $elements = $dom->getElementsByTagNameNS($this->parentNS, $this->parentTagName);
30
31 7
        $fragment = $dom->createDocumentFragment();
32 7
        $fragment->appendXML(call_user_func($this->fragment));
33
34 7
        $elements->item(0)->appendChild($fragment);
35 7
    }
36
37 8
    public function __construct(string $parentNS, string $parentTagName, Closure $fragment)
38
    {
39 8
        $this->parentNS = $parentNS;
40 8
        $this->parentTagName = $parentTagName;
41 8
        $this->fragment = $fragment;
42 8
    }
43
}
44