Completed
Push — master ( b5ae25...349033 )
by Frederik
02:37
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 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 67
ccs 25
cts 25
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;
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
/**
14
 * Class Decoder
15
 * @package Genkgo\Camt
16
 */
17
class Decoder implements DecoderInterface
18
{
19
    /**
20
     * @var SimpleXMLElement[]
21
     */
22
    private $document;
23
24
    /**
25
     * @var Decoder\Message
26
     */
27
    private $messageDecoder;
28
29
    /**
30
     * Path to the schema definition
31
     * @var string
32
     */
33
    protected $schemeDefinitionPath;
34
35
    /**
36
     * @param Decoder\Message $messageDecoder
37
     * @param string          $schemeDefinitionPath
38
     */
39 14
    public function __construct(Decoder\Message $messageDecoder, $schemeDefinitionPath)
40 13
    {
41 14
        $this->messageDecoder       = $messageDecoder;
42 14
        $this->schemeDefinitionPath = $schemeDefinitionPath;
43 14
    }
44
45
    /**
46
     * @param DOMDocument $document
47
     * @throws InvalidMessageException
48
     */
49 14
    private function validate(DOMDocument $document)
50
    {
51 14
        libxml_use_internal_errors(true);
52 14
        $valid = $document->schemaValidate(dirname(__DIR__).$this->schemeDefinitionPath);
53 14
        $errors = libxml_get_errors();
54 14
        libxml_clear_errors();
55
56 14
        if (!$valid) {
57 1
            $messages = [];
58 1
            foreach ($errors as $error) {
59 1
                $messages[] = $error->message;
60 1
            }
61
62 1
            $errorMessage = implode("\n", $messages);
63 1
            throw new InvalidMessageException("Provided XML is not valid according to the XSD:\n{$errorMessage}");
64
        }
65 13
    }
66
67
    /**
68
     * @param DOMDocument $document
69
     * @return Message
70
     * @throws InvalidMessageException
71
     */
72 14
    public function decode(DOMDocument $document)
73
    {
74 14
        $this->validate($document);
75 13
        $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...
76
77 13
        $message = new DTO\Message();
78 13
        $this->messageDecoder->addGroupHeader($message, $this->document);
79 13
        $this->messageDecoder->addRecords($message, $this->document);
80
81 13
        return $message;
82
    }
83
}
84