Completed
Push — master ( 8ca68a...b0dc5f )
by Hannes
02:09
created

BankgiroProcessor::afterRejectMandateRequestNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro.
4
 *
5
 * byrokrat\autogiro 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\autogiro 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\autogiro. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Processor;
24
25
use byrokrat\autogiro\Tree\Node;
26
use byrokrat\autogiro\Tree\OpeningNode;
27
use byrokrat\autogiro\Tree\Record\Request\CreateMandateRequestNode;
28
use byrokrat\autogiro\Tree\Record\Request\AcceptMandateRequestNode;
29
use byrokrat\autogiro\Tree\Record\Request\RejectMandateRequestNode;
30
use byrokrat\autogiro\Tree\Record\Request\UpdateMandateRequestNode;
31
use byrokrat\autogiro\Tree\Record\Request\DeleteMandateRequestNode;
32
use byrokrat\autogiro\Tree\Record\Response\MandateResponseNode;
33
34
/**
35
 * Validates the consistency of payee bankgiro account numbers
36
 */
37
class BankgiroProcessor extends Processor
38
{
39
    /**
40
     * @var string
41
     */
42
    private $currentBankgiro;
43
44
    /**
45
     * Collect the current valid bankgiro number
46
     */
47
    public function beforeOpeningNode(OpeningNode $node)
48
    {
49
        $this->currentBankgiro = $node->getChild('bankgiro')->getValue();
50
    }
51
52
    public function afterCreateMandateRequestNode(CreateMandateRequestNode $node)
53
    {
54
        $this->validateBankgiro($node->getChild('bankgiro')->getValue(), $node);
55
    }
56
57
    public function afterAcceptMandateRequestNode(AcceptMandateRequestNode $node)
58
    {
59
        $this->validateBankgiro($node->getChild('bankgiro')->getValue(), $node);
60
    }
61
62
    public function afterRejectMandateRequestNode(RejectMandateRequestNode $node)
63
    {
64
        $this->validateBankgiro($node->getChild('bankgiro')->getValue(), $node);
65
    }
66
67
    public function afterUpdateMandateRequestNode(UpdateMandateRequestNode $node)
68
    {
69
        $this->validateBankgiro($node->getChild('bankgiro')->getValue(), $node);
70
71
        if ($node->getChild('bankgiro')->getValue() != $node->getChild('new_bankgiro')->getValue()) {
72
            $this->addError(
73
                "Non-matching second bankgiro number in %s (expecting: %s, found: %s) on line %s",
74
                $node->getType(),
75
                $node->getChild('bankgiro')->getValue(),
76
                $node->getChild('new_bankgiro')->getValue(),
77
                (string)$node->getLineNr()
78
            );
79
        }
80
    }
81
82
    public function afterDeleteMandateRequestNode(DeleteMandateRequestNode $node)
83
    {
84
        $this->validateBankgiro($node->getChild('bankgiro')->getValue(), $node);
85
    }
86
87
    public function afterMandateResponseNode(MandateResponseNode $node)
88
    {
89
        $this->validateBankgiro($node->getChild('bankgiro')->getValue(), $node);
90
    }
91
92
    /**
93
     * Validate that bankgiro equals current bankgiro
94
     */
95
    private function validateBankgiro(string $account, Node $node)
96
    {
97
        if ($account != $this->currentBankgiro) {
98
            $this->addError(
99
                "Non-matching bankgiro number in %s (expecting: %s, found: %s) on line %s",
100
                $node->getType(),
101
                $this->currentBankgiro,
102
                $account,
103
                (string)$node->getLineNr()
104
            );
105
        }
106
    }
107
}
108