Conditions | 8 |
Paths | 19 |
Total Lines | 89 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
195 |