ImportXmlMandateFileHandler::handle()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 65
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 37
nc 4
nop 1
dl 0
loc 65
rs 9.328
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\CommandBus;
25
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
    use DependencyInjection\DispatcherProperty;
39
    use 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 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 UpdateState($donor, NewMandate::getStateId(), 'Mandate added from xml')
90
            );
91
92
            $this->commandBus->handle(new UpdateName($donor, $xmlMandate->name));
93
94
            $this->commandBus->handle(new 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 UpdateEmail($donor, $xmlMandate->email));
103
104
            $this->commandBus->handle(new UpdatePhone($donor, $xmlMandate->phone));
105
106
            $this->commandBus->handle(new UpdateComment($donor, $xmlMandate->comment));
107
108
            foreach ($xmlMandate->attributes as $attrKey => $attrValue) {
109
                $this->commandBus->handle(new UpdateAttribute($donor, $attrKey, $attrValue));
110
            }
111
        }
112
113
        $this->dispatcher->dispatch(new Event\XmlMandateFileImported($command->getFile()));
114
    }
115
}
116