XmlFormatter::format()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
crap 2.0054
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of graze/telnet-client.
4
 *
5
 * Copyright (c) 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/xml-utils/blob/master/LICENSE
11
 * @link https://github.com/graze/xml-utils
12
 */
13
14
namespace Graze\XmlUtils;
15
16
use DOMDocument;
17
use Exception;
18
19
class XmlFormatter
20
{
21
    /**
22
     * Return the XML with correct indentation.
23
     *
24
     * @param string $xml
25
     * @return string
26
     * @throws Exception
27
     */
28 1
    public function format($xml)
29
    {
30 1
        $dom = new DOMDocument('1.0');
31 1
        $dom->preserveWhiteSpace = false;
32 1
        $dom->formatOutput = true;
33 1
        $dom->loadXML($xml);
34 1
        $formatted = $dom->saveXML();
35
36 1
        if (!$formatted) {
37
            throw new Exception("An error occurred while formatting the XML");
38
        }
39
40 1
        return $formatted;
41
    }
42
}
43