XmlMandateParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 57
dl 0
loc 107
rs 10
c 7
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parseXml() 0 87 7
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of byrokrat\giroapp.
5
 *
6
 * byrokrat\giroapp is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * byrokrat\giroapp is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Copyright 2016-21 Hannes Forsgård
20
 */
21
22
declare(strict_types=1);
23
24
namespace byrokrat\giroapp\Xml;
25
26
use byrokrat\giroapp\DependencyInjection;
27
use byrokrat\giroapp\Domain\PostalAddress;
28
use byrokrat\giroapp\Exception\InvalidDataException;
29
use byrokrat\giroapp\Exception\ValidatorException;
30
use byrokrat\giroapp\Validator\AccountValidator;
31
use byrokrat\giroapp\Validator\IdValidator;
32
use byrokrat\giroapp\Validator\PayerNumberValidator;
33
use byrokrat\giroapp\Validator\PostalCodeValidator;
34
use byrokrat\giroapp\Validator\StringValidator;
35
use byrokrat\banking\AccountNumber;
36
use byrokrat\id\IdInterface;
37
38
/**
39
 * Create XmlMandate objects from xml source
40
 */
41
class XmlMandateParser
42
{
43
    use DependencyInjection\AccountFactoryProperty;
44
    use DependencyInjection\IdFactoryProperty;
45
46
    /** @var IdInterface */
47
    private $payeeOrgNr;
48
49
    /** @var AccountNumber */
50
    private $payeeBankgiro;
51
52
    public function __construct(IdInterface $payeeOrgNr, AccountNumber $payeeBankgiro)
53
    {
54
        $this->payeeOrgNr = $payeeOrgNr;
55
        $this->payeeBankgiro = $payeeBankgiro;
56
    }
57
58
    /**
59
     * @return array<XmlMandate>
60
     */
61
    public function parseXml(XmlObject $xmlRoot): array
62
    {
63
        /** @var array<XmlMandate> */
64
        $mandates = [];
65
66
        $stringValidator = new StringValidator();
67
68
        foreach ($xmlRoot->getElements('/DocumentElement/MedgivandeViaHemsida') as $xmlSource) {
69
            // Validate payee organisational number
70
            $orgNr = $xmlSource->readElement('/MedgivandeViaHemsida/Organisationsnr', new IdValidator());
71
            if ($this->payeeOrgNr->format('S-sk') != $orgNr) {
72
                // Hard failure, implicit rollback
73
                throw new InvalidDataException(sprintf(
74
                    'Invalid payee org nr %s, expecting %s',
75
                    $orgNr,
76
                    $this->payeeOrgNr->format('S-sk')
77
                ));
78
            }
79
80
            // Validate payee bankgiro account number
81
            $bankgiro = $xmlSource->readElement('/MedgivandeViaHemsida/Bankgironr', new AccountValidator());
82
            if (preg_replace('/\D/', '', $this->payeeBankgiro->getNumber()) != preg_replace('/\D/', '', $bankgiro)) {
83
                // Hard failure, implicit rollback
84
                throw new InvalidDataException(sprintf(
85
                    'Invalid payee bankgiro %s, expecting %s',
86
                    $bankgiro,
87
                    $this->payeeBankgiro->getNumber()
88
                ));
89
            }
90
91
            // require this empty element to exist
92
            $xmlSource->readElement('/MedgivandeViaHemsida/Autogiroanmälan_x002C__x0020_medgivande', $stringValidator);
93
94
            $mandate = new XmlMandate();
95
96
            $mandate->donorId = $this->idFactory->createId(
97
                $xmlSource->readElement('/MedgivandeViaHemsida/Kontoinnehavarens_x0020_personnr', new IdValidator())
98
            );
99
100
            if ($xmlSource->hasElement('/MedgivandeViaHemsida/Betalarnummer')) {
101
                try {
102
                    $mandate->payerNumber = $xmlSource->readElement(
103
                        '/MedgivandeViaHemsida/Betalarnummer',
104
                        new PayerNumberValidator()
105
                    );
106
                } catch (ValidatorException $e) {
107
                    // intentionally empty
108
                }
109
            }
110
111
            $mandate->account = $this->accountFactory->createAccount(
112
                $xmlSource->readElement('/MedgivandeViaHemsida/Kontonr', new AccountValidator())
113
            );
114
115
            $mandate->name = $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_namn', $stringValidator);
116
117
            $mandate->address = [
118
                'line1' =>
119
                    $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_adress_1', $stringValidator),
120
                'line2' =>
121
                    $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_adress_2', $stringValidator),
122
                'line3' =>
123
                    $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_adress_3', $stringValidator),
124
                'postalCode' =>
125
                    $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_postnr', new PostalCodeValidator()),
126
                'postalCity' =>
127
                    $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_postort', $stringValidator)
128
            ];
129
130
            foreach ($xmlSource->getElements('/MedgivandeViaHemsida/Övrig_x0020_info/customdata') as $custom) {
131
                $mandate->attributes[$custom->readElement('/customdata/name', $stringValidator)]
132
                    = $custom->readElement('/customdata/value', $stringValidator);
133
            }
134
135
            $mandate->attributes['online_form_id']
136
                = $xmlSource->readElement('/MedgivandeViaHemsida/Formulärnamn', $stringValidator);
137
138
            $mandate->attributes['online_verification_time']
139
                = $xmlSource->readElement('/MedgivandeViaHemsida/Verifieringstid', $stringValidator);
140
141
            $mandate->attributes['online_verification_code']
142
                = $xmlSource->readElement('/MedgivandeViaHemsida/Verifieringsreferens', $stringValidator);
143
144
            $mandates[] = $mandate;
145
        }
146
147
        return $mandates;
148
    }
149
}
150