Passed
Branch master (613786)
by Hannes
02:27
created

ImportXmlMandateFileHandler::handle()   A

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
 * 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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, byrokrat\giroapp\CommandBus\CommandBus. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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