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-20 Hannes Forsgård |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
22
|
|
|
|
23
|
|
|
namespace byrokrat\giroapp\CommandBus; |
24
|
|
|
|
25
|
|
|
use byrokrat\giroapp\CommandBus; |
|
|
|
|
26
|
|
|
use byrokrat\giroapp\DependencyInjection; |
27
|
|
|
use byrokrat\giroapp\Domain\MandateSources; |
28
|
|
|
use byrokrat\giroapp\Domain\NewDonor; |
29
|
|
|
use byrokrat\giroapp\Domain\PostalAddress; |
30
|
|
|
use byrokrat\giroapp\Domain\State\NewMandate; |
31
|
|
|
use byrokrat\giroapp\Event; |
32
|
|
|
use byrokrat\giroapp\Exception\DonorAlreadyExistsException; |
33
|
|
|
use byrokrat\giroapp\Xml\XmlMandateCompiler; |
34
|
|
|
|
35
|
|
|
final class ImportXmlMandateFileHandler |
36
|
|
|
{ |
37
|
|
|
use DependencyInjection\CommandBusProperty, |
38
|
|
|
DependencyInjection\DispatcherProperty, |
39
|
|
|
DependencyInjection\DonorRepositoryProperty; |
40
|
|
|
|
41
|
|
|
/** @var XmlMandateCompiler */ |
42
|
|
|
private $xmlMandateCompiler; |
43
|
|
|
|
44
|
|
|
public function __construct(XmlMandateCompiler $xmlMandateCompiler) |
45
|
|
|
{ |
46
|
|
|
$this->xmlMandateCompiler = $xmlMandateCompiler; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function handle(ImportXmlMandateFile $command): void |
50
|
|
|
{ |
51
|
|
|
$this->dispatcher->dispatch( |
52
|
|
|
new Event\InfoEvent( |
53
|
|
|
"<info>Importing mandates from {$command->getFile()->getFilename()}</info>", |
54
|
|
|
['filename' => $command->getFile()->getFilename()] |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
foreach ($this->xmlMandateCompiler->compileFile($command->getFile()) as $xmlMandate) { |
59
|
|
|
try { |
60
|
|
|
$this->commandBus->handle( |
61
|
|
|
new CommandBus\AddDonor( |
62
|
|
|
new NewDonor( |
63
|
|
|
MandateSources::MANDATE_SOURCE_ONLINE_FORM, |
64
|
|
|
$xmlMandate->payerNumber, |
65
|
|
|
$xmlMandate->account, |
66
|
|
|
$xmlMandate->donorId, |
67
|
|
|
$xmlMandate->donationAmount |
68
|
|
|
) |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} catch (DonorAlreadyExistsException $exception) { |
72
|
|
|
// Dispatching error means that failure can be picked up in an outer layer |
73
|
|
|
$this->dispatcher->dispatch( |
74
|
|
|
new Event\ErrorEvent( |
75
|
|
|
$exception->getMessage(), |
76
|
|
|
[ |
77
|
|
|
'payer_number' => $xmlMandate->payerNumber, |
78
|
|
|
'donor_id' => $xmlMandate->donorId->format('CS-sk') |
79
|
|
|
] |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$donor = $this->donorRepository->requireByPayerNumber($xmlMandate->payerNumber); |
87
|
|
|
|
88
|
|
|
$this->commandBus->handle( |
89
|
|
|
new CommandBus\UpdateState($donor, NewMandate::getStateId(), 'Mandate added from xml') |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->commandBus->handle(new CommandBus\UpdateName($donor, $xmlMandate->name)); |
93
|
|
|
|
94
|
|
|
$this->commandBus->handle(new CommandBus\UpdatePostalAddress($donor, new PostalAddress( |
95
|
|
|
$xmlMandate->address['line1'], |
96
|
|
|
$xmlMandate->address['line2'], |
97
|
|
|
$xmlMandate->address['line3'], |
98
|
|
|
$xmlMandate->address['postalCode'], |
99
|
|
|
$xmlMandate->address['postalCity'] |
100
|
|
|
))); |
101
|
|
|
|
102
|
|
|
$this->commandBus->handle(new CommandBus\UpdateEmail($donor, $xmlMandate->email)); |
103
|
|
|
|
104
|
|
|
$this->commandBus->handle(new CommandBus\UpdatePhone($donor, $xmlMandate->phone)); |
105
|
|
|
|
106
|
|
|
$this->commandBus->handle(new CommandBus\UpdateComment($donor, $xmlMandate->comment)); |
107
|
|
|
|
108
|
|
|
foreach ($xmlMandate->attributes as $attrKey => $attrValue) { |
109
|
|
|
$this->commandBus->handle(new CommandBus\UpdateAttribute($donor, $attrKey, $attrValue)); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->dispatcher->dispatch(new Event\XmlMandateFileImported($command->getFile())); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: