Completed
Push — master ( 9b8515...2320df )
by Adrien
02:36
created

Reader::readFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Genkgo\Camt;
3
4
use DOMDocument;
5
use Genkgo\Camt\DTO\Message;
6
use Genkgo\Camt\Exception\ReaderException;
7
8
/**
9
 * Class Reader
10
 * @package Genkgo\Camt
11
 */
12
class Reader
13
{
14
    /**
15
     * @var Config
16
     */
17
    private $config;
18
19
    /**
20
     * @var MessageFormatInterface
21
     */
22
    private $messageFormat;
23
24
    /**
25
     * @param Config $config
26
     */
27 4
    public function __construct(Config $config)
28
    {
29 4
        $this->config = $config;
30 4
    }
31
32
    /**
33
     * @param DOMDocument $document
34
     * @return mixed
35
     * @throws ReaderException
36
     */
37 4
    public function readDom(DOMDocument $document)
38
    {
39 4
        if ($document->documentElement === null) {
40 1
            throw new ReaderException("Empty document");
41
        }
42
43 3
        $xmlNs = $document->documentElement->getAttribute('xmlns');
44 3
        $this->messageFormat = $this->getMessageFormatForXmlNs($xmlNs);
45
46 2
        return $this->messageFormat->getDecoder()->decode($document, $this->config->getXsdValidation());
47
    }
48
49
    /**
50
     * @param string $string
51
     * @return mixed
52
     * @throws ReaderException
53
     */
54 2
    public function readString($string)
55
    {
56 2
        $dom = new DOMDocument('1.0', 'UTF-8');
57 2
        $dom->loadXML($string);
58
59 2
        return $this->readDom($dom);
60
    }
61
62
    /**
63
     * @param  string $file
64
     * @return mixed|Message
65
     * @throws ReaderException
66
     */
67 2
    public function readFile($file)
68
    {
69 2
        if (!file_exists($file)) {
70
            throw new ReaderException("{$file} does not exists");
71
        }
72
73 2
        $string = file_get_contents($file);
74 2
        if ($string === false) {
75
            throw new ReaderException("Could not read file {$file}");
76
        }
77
78 2
        return $this->readString($string);
79
    }
80
81
    /**
82
     * @param string $xmlNs
83
     * @return MessageFormatInterface
84
     * @throws ReaderException
85
     */
86 3
    private function getMessageFormatForXmlNs($xmlNs)
87
    {
88 3
        $messageFormats = $this->config->getMessageFormats();
89 3
        foreach ($messageFormats as $messageFormat) {
90 3
            if ($messageFormat->getXmlNs() === $xmlNs) {
91 2
                return $messageFormat;
92
            }
93
        }
94
95 1
        throw new ReaderException("Unsupported format, cannot find message format with xmlns {$xmlNs}");
96
    }
97
98
    /**
99
     * @return MessageFormatInterface
100
     */
101
    public function getMessageFormat()
102
    {
103
        return $this->messageFormat;
104
    }
105
}
106