Completed
Push — master ( aabf4d...606f02 )
by Frederik
9s
created

Decoder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 21
Bugs 5 Features 6
Metric Value
wmc 5
c 21
b 5
f 6
lcom 1
cbo 3
dl 0
loc 67
ccs 24
cts 24
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 0 17 3
A decode() 0 11 1
1
<?php
2
namespace Genkgo\Camt\Camt053;
3
4
use DateTimeImmutable;
5
use DOMDocument;
6
use Genkgo\Camt\DecoderInterface;
7
use Genkgo\Camt\Exception\InvalidMessageException;
8
use Genkgo\Camt\Util\StringToUnits;
9
use Money\Currency;
10
use Money\Money;
11
use SimpleXMLElement;
12
13
class Decoder implements DecoderInterface
14
{
15
    /**
16
     * @var SimpleXMLElement[]
17
     */
18
    private $document;
19
20
    /**
21
     * @var Decoder\Message
22
     */
23
    private $messageDecoder;
24
25
    /**
26
     * Path to the schema definition
27
     * @var string
28
     */
29
    protected $schemeDefinitionPath;
30
31
    /**
32
     * @param Decoder\Message $messageDecoder
33
     * @param string          $schemeDefinitionPath
34
     */
35 10
    public function __construct(Decoder\Message $messageDecoder, $schemeDefinitionPath)
36
    {
37 10
        $this->messageDecoder       = $messageDecoder;
38 10
        $this->schemeDefinitionPath = $schemeDefinitionPath;
39 10
    }
40
41
    /**
42
     * @param DOMDocument $document
43
     * @throws InvalidMessageException
44
     */
45 10
    private function validate(DOMDocument $document)
46
    {
47 10
        libxml_use_internal_errors(true);
48 10
        $valid = $document->schemaValidate(dirname(dirname(__DIR__)).$this->schemeDefinitionPath);
49 10
        $errors = libxml_get_errors();
50 10
        libxml_clear_errors();
51
52 10
        if (!$valid) {
53 1
            $messages = [];
54 1
            foreach ($errors as $error) {
55 1
                $messages[] = $error->message;
56 1
            }
57
58 1
            $errorMessage = implode("\n", $messages);
59 1
            throw new InvalidMessageException("Provided XML is not valid according to the XSD:\n{$errorMessage}");
60
        }
61 9
    }
62
63
    /**
64
     * @param DOMDocument $document
65
     * @return Message
66
     * @throws InvalidMessageException
67
     */
68 10
    public function decode(DOMDocument $document)
69
    {
70 10
        $this->validate($document);
71 9
        $this->document = simplexml_import_dom($document);
0 ignored issues
show
Documentation Bug introduced by
It seems like simplexml_import_dom($document) of type object<SimpleXMLElement> is incompatible with the declared type array<integer,object<SimpleXMLElement>> of property $document.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
73 9
        $message = new DTO\Message();
74 9
        $this->messageDecoder->addGroupHeader($message, $this->document);
75 9
        $this->messageDecoder->addStatements($message, $this->document);
76
77 9
        return $message;
78
    }
79
}
80