Completed
Push — feature/symfony3-compat ( 63d226 )
by Lucas
17:23
created

XmlManipulator::saveDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.064
Metric Value
dl 0
loc 6
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1.064
1
<?php
2
/**
3
 *
4
 */
5
namespace Graviton\GeneratorBundle\Manipulator\File;
6
7
use Graviton\GeneratorBundle\Manipulator\ManipulatorException;
8
9
/**
10
 * change the code of a xml file
11
 *
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class XmlManipulator
17
{
18
    /** @var string */
19
    private $options = LIBXML_NOBLANKS;
20
21
    /** @var array */
22
    private $nodes = [];
23
24
    /** @var  \DomDocument */
25
    private $document;
26
27
    /**
28
     * Gathers the provides nodes in a collection to be added to a xml string later.
29
     *
30
     * @param string $nodes Xml data to be inserted in to a xml document.
31
     *
32
     * @return XmlManipulator
33
     */
34 8
    public function addNodes($nodes)
35
    {
36 8
        if (!empty($nodes)) {
37 8
            $this->nodes[] = $nodes;
38 4
        }
39
40 8
        return $this;
41
    }
42
43
    /**
44
     * Resets the current instance
45
     *
46
     * @return $this
47
     */
48 2
    public function reset()
49
    {
50 2
        $this->nodes = [];
51
52 2
        return $this;
53
    }
54
55
    /**
56
     * Renders the gathered nodes into a XML document.
57
     *
58
     * @param string $xml Text to be imported to a DomDocument.
59
     *
60
     * @return XmlManipulator
61
     */
62 4
    public function renderDocument($xml)
63
    {
64 4
        $this->document = $this->initDomDocument($xml);
65
66 4
        foreach ($this->nodes as $nodeXml) {
67 4
            $mergeDoc = $this->initDomDocument($nodeXml);
68
69 4
            $importNodes = $mergeDoc->getElementsByTagNameNS(
70 4
                'http://symfony.com/schema/dic/constraint-mapping',
71 2
                'class'
72 2
            );
73
74 4
            foreach ($importNodes as $importNode) {
75 4
                $importNode = $this->document->importNode($importNode, true);
76 4
                $this->document->documentElement->appendChild($importNode);
77 2
            }
78 2
        }
79
80 4
        return $this;
81
    }
82
83
    /**
84
     * Stores the current document to the file system.
85
     *
86
     * @param string $path Location of the file to be stored.
87
     *
88
     * @return void
89
     */
90 2
    public function saveDocument($path)
91
    {
92 2
        set_error_handler(array($this, 'handleXmlError'));
93 2
        $this->document->save($path);
94
        restore_error_handler();
95
    }
96
97
    /**
98
     * Loads the provides file into a DomDocument;
99
     *
100
     * @param string   $xml     XML string/text to be loaded
101
     * @param int|null $options Set of libxml constants.
102
     *
103
     * @return \DOMDocument
104
     *
105
     * @link http://php.net/manual/en/libxml.constants.php
106
     */
107 4
    private function initDomDocument($xml, $options = null)
108
    {
109 4
        $doc = new \DOMDocument();
110 4
        $doc->formatOutput = true;
111 4
        $doc->preserveWhiteSpace = false;
112
113 4
        if (!empty($options)) {
114
            $options = $this->options . '|' . $options;
115
        } else {
116 4
            $options = $this->options;
117
        }
118
119 4
        set_error_handler(array($this, 'handleXmlError'));
120 4
        $doc->loadXml($xml, $options);
121 4
        restore_error_handler();
122
123 4
        return $doc;
124
    }
125
126
    /**
127
     * Handles any error while reading the xml into a DomDocument
128
     *
129
     * @param string $errno  Error code
130
     * @param string $errstr Error message
131
     *
132
     * @throws ManipulatorException
133
     * @return false
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
134
     */
135 22
    public function handleXmlError($errno, $errstr)
0 ignored issues
show
Coding Style introduced by
function handleXmlError() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
136
    {
137 22
        if ($errno == E_WARNING && (substr_count($errstr, "DOMDocument::loadXML()") > 0)) {
138
            throw new ManipulatorException('Failed to load the provided xml string into a DomDocument');
139 22
        } elseif ($errno == E_WARNING && (substr_count($errstr, "DOMDocument::save()") > 0)) {
140 2
            throw new ManipulatorException('Failed to save document to the filesystem.');
141
        } else {
142 20
            return false;
143
        }
144
    }
145
}
146