Failed Conditions
Pull Request — master (#188)
by
unknown
01:46
created

XmlMandateParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
B parse() 0 101 7
1
<?php
2
/**
3
 * This file is part of byrokrat\giroapp.
4
 *
5
 * byrokrat\giroapp is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\giroapp is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016-19 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Xml;
24
25
use byrokrat\giroapp\Validator\AccountValidator;
26
use byrokrat\giroapp\Validator\IdValidator;
27
use byrokrat\giroapp\Validator\NumericValidator;
28
use byrokrat\giroapp\Validator\PayerNumberValidator;
29
use byrokrat\giroapp\Validator\PostalCodeValidator;
30
use byrokrat\giroapp\Validator\StringValidator;
31
use byrokrat\giroapp\MandateSources;
32
use byrokrat\giroapp\Model\Builder\DonorBuilder;
33
use byrokrat\giroapp\Model\Donor;
34
use byrokrat\giroapp\Model\PostalAddress;
35
use byrokrat\giroapp\Exception\InvalidDataException;
36
use byrokrat\amount\Currency\SEK;
37
use byrokrat\banking\AccountFactoryInterface;
38
use byrokrat\banking\AccountNumber;
39
use byrokrat\id\IdFactoryInterface;
40
use byrokrat\id\OrganizationId;
41
42
/**
43
 * Parse xml representations of online form mandates
44
 */
45
class XmlMandateParser
46
{
47
    /**
48
     * @var OrganizationId
49
     */
50
    private $payeeOrgNr;
51
52
    /**
53
     * @var AccountNumber
54
     */
55
    private $payeeBankgiro;
56
57
    /**
58
     * @var DonorBuilder
59
     */
60
    private $donorBuilder;
61
62
    /**
63
     * @var XmlFormTranslator
64
     */
65
    private $translator;
66
67
    /**
68
     * @var AccountFactoryInterface
69
     */
70
    private $accountFactory;
71
72
    /**
73
     * @var IdFactoryInterface
74
     */
75
    private $idFactory;
76
77
    public function __construct(
78
        OrganizationId $payeeOrgNr,
79
        AccountNumber $payeeBankgiro,
80
        DonorBuilder $donorBuilder,
81
        XmlFormTranslator $translator,
82
        AccountFactoryInterface $accountFactory,
83
        IdFactoryInterface $idFactory
84
    ) {
85
        $this->payeeOrgNr = $payeeOrgNr;
86
        $this->payeeBankgiro = $payeeBankgiro;
87
        $this->donorBuilder = $donorBuilder;
88
        $this->translator = $translator;
89
        $this->accountFactory = $accountFactory;
90
        $this->idFactory = $idFactory;
91
    }
92
93
    /**
94
     * Get generator that yields parsed Donor instances
95
     */
96
    public function parse(XmlObject $xml): iterable
97
    {
98
        $donors = [];
99
100
        foreach ($xml->getElements('/DocumentElement/MedgivandeViaHemsida') as $mandate) {
101
            $orgNr = $mandate->readElement('/MedgivandeViaHemsida/Organisationsnr', new IdValidator);
102
103
            if ($this->payeeOrgNr->format('S-sk') != $orgNr) {
104
                throw new InvalidDataException(sprintf(
105
                    'Invalid payee org nr %s, expecting %s',
106
                    $orgNr,
107
                    $this->payeeOrgNr->format('S-sk')
108
                ));
109
            }
110
111
            $bankgiro = $mandate->readElement('/MedgivandeViaHemsida/Bankgironr', new AccountValidator);
112
113
            if (preg_replace('/\D/', '', $this->payeeBankgiro->getNumber()) != preg_replace('/\D/', '', $bankgiro)) {
114
                throw new InvalidDataException(sprintf(
115
                    'Invalid payee bankgiro %s, expecting %s',
116
                    $bankgiro,
117
                    $this->payeeBankgiro->getNumber()
118
                ));
119
            }
120
121
            $stringValidator = new StringValidator;
122
123
            // require this empty element to exist
124
            $mandate->readElement(
125
                '/MedgivandeViaHemsida/Autogiroanmälan_x002C__x0020_medgivande',
126
                $stringValidator
127
            );
128
129
            $this->donorBuilder->reset();
130
131
            $this->donorBuilder->setMandateSource(MandateSources::MANDATE_SOURCE_ONLINE_FORM);
132
133
            $this->donorBuilder->setName(
134
                $mandate->readElement('/MedgivandeViaHemsida/Betalares_x0020_namn', $stringValidator)
135
            );
136
137
            $this->donorBuilder->setPostalAddress(
138
                new PostalAddress(
139
                    $mandate->readElement('/MedgivandeViaHemsida/Betalares_x0020_adress_1', $stringValidator),
140
                    $mandate->readElement('/MedgivandeViaHemsida/Betalares_x0020_adress_2', $stringValidator),
141
                    $mandate->readElement('/MedgivandeViaHemsida/Betalares_x0020_adress_3', $stringValidator),
142
                    $mandate->readElement('/MedgivandeViaHemsida/Betalares_x0020_postnr', new PostalCodeValidator),
143
                    $mandate->readElement('/MedgivandeViaHemsida/Betalares_x0020_postort', $stringValidator)
144
                )
145
            );
146
147
            if ($mandate->hasElement('/MedgivandeViaHemsida/Betalarnummer')) {
148
                try {
149
                    $this->donorBuilder->setPayerNumber(
150
                        $mandate->readElement('/MedgivandeViaHemsida/Betalarnummer', new PayerNumberValidator)
151
                    );
152
                }
153
                catch(ValidatorException $e) {
0 ignored issues
show
Bug introduced by
The type byrokrat\giroapp\Xml\ValidatorException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
154
                    $this->donorBuilder->setPayerNumber(
155
                        $mandate->readElement('/MedgivandeViaHemsida/Kontoinnehavarens_x0020_personnr', new IdValidator)
156
                    );
157
                }
158
            }
159
160
            $this->donorBuilder->setAccount(
161
                $this->accountFactory->createAccount(
162
                    $mandate->readElement('/MedgivandeViaHemsida/Kontonr', new AccountValidator)
163
                )
164
            );
165
166
            $this->donorBuilder->setId(
167
                $this->idFactory->createId(
168
                    $mandate->readElement('/MedgivandeViaHemsida/Kontoinnehavarens_x0020_personnr', new IdValidator)
169
                )
170
            );
171
172
            $this->donorBuilder->setAttribute(
173
                'verification_time',
174
                $mandate->readElement('/MedgivandeViaHemsida/Verifieringstid', $stringValidator)
175
            );
176
177
            $this->donorBuilder->setAttribute(
178
                'verification_code',
179
                $mandate->readElement('/MedgivandeViaHemsida/Verifieringsreferens', $stringValidator)
180
            );
181
182
            $formId = $mandate->readElement('/MedgivandeViaHemsida/Formulärnamn', $stringValidator);
183
184
            foreach ($mandate->getElements('/MedgivandeViaHemsida/Övrig_x0020_info/customdata') as $custom) {
185
                $this->translator->writeValue(
186
                    $this->donorBuilder,
187
                    $formId,
188
                    $custom->readElement('/customdata/name', $stringValidator),
189
                    $custom->readElement('/customdata/value', $stringValidator)
190
                );
191
            }
192
193
            $donors[] = $this->donorBuilder->buildDonor();
194
        }
195
196
        return $donors;
197
    }
198
}
199