XmlParser::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * slince config library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Config\Parser;
7
8
use LSS\Array2XML;
9
use LSS\XML2Array;
10
use Slince\Config\Exception\ParseException;
11
12
class XmlParser implements ParserInterface
13
{
14
    /**
15
     * The xml root node name
16
     * @var string
17
     */
18
    public static $rootNodeName = 'configuration';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function parse($file)
24
    {
25
        $xml = file_get_contents($file);
26
        try {
27
            $data = XML2Array::createArray($xml);
28
        } catch (\Exception $exception) {
29
            throw new ParseException($exception->getMessage(), $exception->getCode());
30
        }
31
        return reset($data);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function dump($file, array $data)
38
    {
39
        $xml = Array2XML::createXML(static::$rootNodeName, $data);
40
        @mkdir(dirname($file), 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
41
        return @file_put_contents($file, $xml->saveXML()) !== false;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public static function getSupportedExtensions()
48
    {
49
        return ['xml'];
50
    }
51
}