Completed
Push — master ( a823a2...ede1e0 )
by Adrien
03:52 queued 02:34
created

Reader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 99
ccs 22
cts 30
cp 0.7332
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A readDom() 0 11 2
A readString() 0 7 1
A readDoc() 0 8 1
A readFile() 0 8 2
A getMessageFormatForXmlNs() 0 11 3
A getMessageFormat() 0 4 1
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 3
    public function __construct(Config $config)
28
    {
29 3
        $this->config = $config;
30 3
    }
31
32
    /**
33
     * @param DOMDocument $document
34
     * @return mixed
35
     * @throws ReaderException
36
     */
37 3
    public function readDom(DOMDocument $document)
38
    {
39 3
        if ($document->documentElement === null) {
40 1
            throw new ReaderException("Empty document");
41
        }
42
43 2
        $xmlNs = $document->documentElement->getAttribute('xmlns');
44 2
        $this->messageFormat = $this->getMessageFormatForXmlNs($xmlNs);
45
46 1
        return $this->messageFormat->getDecoder()->decode($document);
47
    }
48
49
    /**
50
     * @param $string
51
     * @return mixed
52
     * @throws ReaderException
53
     */
54 1
    public function readString($string)
55
    {
56 1
        $dom = new DOMDocument('1.0', 'UTF-8');
57 1
        $dom->loadXML($string);
58
59 1
        return $this->readDom($dom);
60
    }
61
62
    public function readDoc($string)
63
    {
64
        $string = file_get_contents($string);
65
        $dom = new DOMDocument('1.0', 'UTF-8');
66
        $dom->loadXML($string);
67
68
        return $dom;
69
    }
70
71
72
    /**
73
     * @param  string $file
74
     * @return mixed|Message
75
     * @throws ReaderException
76
     */
77 1
    public function readFile($file)
78
    {
79 1
        if (!file_exists($file)) {
80
            throw new ReaderException("{$file} does not exists");
81
        }
82
83 1
        return $this->readString(file_get_contents($file));
84
    }
85
86
    /**
87
     * @param $xmlNs
88
     * @return MessageFormatInterface
89
     * @throws ReaderException
90
     */
91 2
    private function getMessageFormatForXmlNs($xmlNs)
92
    {
93 2
        $messageFormats = $this->config->getMessageFormats();
94 2
        foreach ($messageFormats as $messageFormat) {
95 2
            if ($messageFormat->getXmlNs() === $xmlNs) {
96 1
                return $messageFormat;
97
            }
98
        }
99
100 1
        throw new ReaderException("Unsupported format, cannot find message format with xmlns {$xmlNs}");
101
    }
102
103
    /**
104
     * @return MessageFormatInterface
105
     */
106
    public function getMessageFormat()
107
    {
108
        return $this->messageFormat;
109
    }
110
}
111