XmlParser   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 10 2
A dump() 0 6 1
A getSupportedExtensions() 0 4 1
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
}