AbstractAdapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 49
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
configureSchema() 0 1 ?
A __construct() 0 9 2
A getFile() 0 4 1
A validateSchema() 0 17 3
1
<?php
2
3
namespace Magium\Configuration\File;
4
5
abstract class AbstractAdapter implements AdapterInterface
6
{
7
8
    protected $file;
9
10 24
    public function __construct(
11
        $file
12
    )
13
    {
14 24
        $this->file = realpath($file);
15 24
        if ($this->file === false) {
16 3
            throw new InvalidFileException('Unable to find file: ' . $file);
17
        }
18 21
    }
19
20
    /*
21
     * @return string The location of the file
22
     */
23
24 17
    public function getFile()
25
    {
26 17
        return $this->file;
27
    }
28
29
    abstract protected function configureSchema(\DOMElement $element);
30
31
    /**
32
     * @param \DOMDocument $doc
33
     * @throws InvalidFileStructureException
34
     */
35
36 16
    protected function validateSchema(\DOMDocument $doc)
37
    {
38
        try {
39 16
            $element = $doc->firstChild;
40 16
            if (!$element instanceof \DOMElement) {
41
                // This is more for code completion.  If the first child is not a DOMElement, PHP is probably broken
42
                throw new \Exception('Invalid XML file');
43
            }
44 16
            $schema = $this->configureSchema($element);
45 16
            $out = $doc->saveXML();
46 16
            $validateDoc = new \DOMDocument();
47 16
            $validateDoc->loadXML($out);
48 16
            $validateDoc->schemaValidate($schema);
49 1
        } catch (\Exception $e) {
50 1
            throw new InvalidFileStructureException(sprintf('Unable to load file %s due to exception: %s', $this->getFile(), $e->getMessage()), $e->getCode(), $e);
51
        }
52 15
    }
53
}
54