Passed
Pull Request — master (#156)
by Hannes
02:53
created

onMandateResponseReceived()   B

Complexity

Conditions 8
Paths 19

Size

Total Lines 89
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 53
nc 19
nop 3
dl 0
loc 89
rs 7.781
c 0
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-18 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Listener;
24
25
use byrokrat\giroapp\Mapper\DonorMapper;
26
use byrokrat\giroapp\Events;
27
use byrokrat\giroapp\Event\DonorEvent;
28
use byrokrat\giroapp\Event\LogEvent;
29
use byrokrat\giroapp\Event\NodeEvent;
30
use byrokrat\giroapp\State\StatePool;
31
use byrokrat\giroapp\States;
32
use Symfony\Component\EventDispatcher\EventDispatcherInterface as Dispatcher;
33
34
/**
35
 * Add or reject mandates based on autogiro response
36
 */
37
class MandateResponseListener
38
{
39
    /**
40
     * @var DonorMapper
41
     */
42
    private $donorMapper;
43
44
    /**
45
     * @var StatePool
46
     */
47
    private $statePool;
48
49
    public function __construct(DonorMapper $donorMapper, StatePool $statePool)
50
    {
51
        $this->donorMapper = $donorMapper;
52
        $this->statePool = $statePool;
53
    }
54
55
    public function onMandateResponseReceived(NodeEvent $nodeEvent, string $eventName, Dispatcher $dispatcher): void
56
    {
57
        $node = $nodeEvent->getNode();
58
59
        $donor = $this->donorMapper->findByActivePayerNumber(
60
            $node->getValueFrom('PayerNumber')
61
        );
62
63
        /** @var \byrokrat\id\IdInterface $nodeId */
64
        if ($nodeId = $node->getChild('StateId')->getValueFrom('Object')) {
65
            if ($donor->getDonorId()->format('S-sk') != $nodeId->format('S-sk')) {
66
                $dispatcher->dispatch(
67
                    Events::WARNING,
68
                    new LogEvent(
69
                        sprintf(
70
                            "Invalid mandate response for payer number '%s', found donor id '%s', expecting '%s'",
71
                            $donor->getPayerNumber(),
72
                            $nodeId->format('S-sk'),
73
                            $donor->getDonorId()->format('S-sk')
74
                        )
75
                    )
76
                );
77
78
                // stop processing on invalid id
79
                $nodeEvent->stopPropagation();
80
                return;
81
            }
82
        }
83
84
        /** @var \byrokrat\banking\AccountNumber $nodeAccount */
85
        if ($nodeAccount = $node->getChild('Account')->getValueFrom('Object')) {
86
            if (!$donor->getAccount()->equals($nodeAccount)) {
87
                $dispatcher->dispatch(
88
                    Events::WARNING,
89
                    new LogEvent(
90
                        sprintf(
91
                            "Invalid mandate response for payer number '%s', found account '%s', expecting '%s'",
92
                            $donor->getPayerNumber(),
93
                            $nodeAccount->getNumber(),
94
                            $donor->getAccount()->getNumber()
95
                        )
96
                    )
97
                );
98
99
                // stop processing on invalid account
100
                $nodeEvent->stopPropagation();
101
                return;
102
            }
103
        }
104
105
        $donorEvent = new DonorEvent(
106
            sprintf(
107
                '%s: %s',
108
                $donor->getMandateKey(),
109
                (string)$node->getChild('Status')->getValueFrom('Text')
110
            ),
111
            $donor
112
        );
113
114
        if ($node->hasChild('CreatedFlag')) {
115
            $donor->setState($this->statePool->getState(States::MANDATE_APPROVED));
116
            $dispatcher->dispatch(Events::MANDATE_APPROVED, $donorEvent);
117
            return;
118
        }
119
120
        if ($node->hasChild('DeletedFlag')) {
121
            $donor->setState($this->statePool->getState(States::INACTIVE));
122
            $dispatcher->dispatch(Events::MANDATE_REVOKED, $donorEvent);
123
            return;
124
        }
125
126
        if ($node->hasChild('ErrorFlag')) {
127
            $donor->setState($this->statePool->getState(States::ERROR));
128
            $dispatcher->dispatch(Events::MANDATE_INVALIDATED, $donorEvent);
129
            return;
130
        }
131
132
        $dispatcher->dispatch(
133
            Events::WARNING,
134
            new LogEvent(
135
                sprintf(
136
                    '%s: invalid mandate status code: %s',
137
                    $donor->getMandateKey(),
138
                    $node->getChild('Status')->getValueFrom('Number')
139
                )
140
            )
141
        );
142
143
        $nodeEvent->stopPropagation();
144
145
        /*
146
        switch ($node->getChild('status')->getAttribute('message_id')) {
147
            case Messages::STATUS_MANDATE_DELETED_BY_PAYER:
148
            case Messages::STATUS_MANDATE_DELETED_DUE_TO_UNANSWERED_ACCOUNT_REQUEST:
149
            case Messages::STATUS_MANDATE_DELETED:
150
            case Messages::STATUS_MANDATE_DELETED_DUE_TO_CLOSED_PAYER_BG:
151
            case Messages::STATUS_MANDATE_DELETED_BY_BANK:
152
            case Messages::STATUS_MANDATE_DELETED_BY_BGC:
153
                $donor->setState($this->statePool->getState(States::INACTIVE));
154
                $dispatcher->dispatch(Events::MANDATE_REVOKED, $donorEvent);
155
                break;
156
157
            case Messages::STATUS_MANDATE_CREATED:
158
                $donor->setState($this->statePool->getState(States::MANDATE_APPROVED));
159
                $dispatcher->dispatch(Events::MANDATE_APPROVED, $donorEvent);
160
                break;
161
162
            case Messages::STATUS_MANDATE_ACCOUNT_NOT_ALLOWED:
163
            case Messages::STATUS_MANDATE_DOES_NOT_EXIST:
164
            case Messages::STATUS_MANDATE_INVALID_ACCOUNT_OR_ID:
165
            case Messages::STATUS_MANDATE_PAYER_NUMBER_DOES_NOT_EXIST:
166
            case Messages::STATUS_MANDATE_ALREADY_EXISTS:
167
            case Messages::STATUS_MANDATE_INVALID_ID_OR_BG_NOT_ALLOWED:
168
            case Messages::STATUS_MANDATE_INVALID_PAYER_NUMBER:
169
            case Messages::STATUS_MANDATE_INVALID_ACCOUNT:
170
            case Messages::STATUS_MANDATE_INVALID_PAYEE_ACCOUNT:
171
            case Messages::STATUS_MANDATE_INACTIVE_PAYEE_ACCOUNT:
172
            case Messages::STATUS_MANDATE_BLOCKED_BY_PAYER:
173
            case Messages::STATUS_MANDATE_BLOCK_REMOVED:
174
            case Messages::STATUS_MANDATE_MAX_AMOUNT_NOT_ALLOWED:
175
                $donor->setState($this->statePool->getState(States::ERROR));
176
                $dispatcher->dispatch(Events::MANDATE_INVALIDATED, $donorEvent);
177
                break;
178
179
            default:
180
                $dispatcher->dispatch(
181
                    Events::WARNING,
182
                    new LogEvent(
183
                        sprintf(
184
                            '%s: invalid mandate status code: %s',
185
                            $donor->getMandateKey(),
186
                            $node->getChild('status')->getValue()
187
                        )
188
                    )
189
                );
190
                $nodeEvent->stopPropagation();
191
        }
192
        */
193
    }
194
}
195