PayeeVisitor::beforeAutogiroFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of byrokrat\autogiro.
5
 *
6
 * byrokrat\autogiro 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\autogiro 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\autogiro. 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\autogiro\Visitor;
25
26
use byrokrat\autogiro\Tree\Node;
27
28
/**
29
 * Validate that payee bankgiro and BGC customer number are constant within tree
30
 */
31
final class PayeeVisitor extends Visitor
32
{
33
    use ErrorAwareTrait;
34
35
    /**
36
     * @var string Payee bankgiro account number
37
     */
38
    private $payeeBg;
39
40
    /**
41
     * @var string Payee BGC customer number
42
     */
43
    private $payeeBgcNr;
44
45
    /**
46
     * Reset payee bankgiro and customer number before a new file is traversed
47
     */
48
    public function beforeAutogiroFile(): void
49
    {
50
        $this->payeeBg = '';
51
        $this->payeeBgcNr = '';
52
    }
53
54
    /**
55
     * Validate payee bankgiro number
56
     */
57
    public function beforePayeeBankgiro(Node $node): void
58
    {
59
        if (!$this->payeeBg) {
60
            $this->payeeBg = (string)$node->getValueFrom(Node::NUMBER);
61
        }
62
63
        if ($node->getValueFrom(Node::NUMBER) != $this->payeeBg) {
64
            $this->getErrorObject()->addError(
65
                "Non-matching payee bankgiro numbers (expecting: %s, found: %s) on line %s",
66
                $this->payeeBg,
67
                (string)$node->getValueFrom(Node::NUMBER),
68
                (string)$node->getLineNr()
69
            );
70
        }
71
    }
72
73
    /**
74
     * Validate payee BGC customer number
75
     */
76
    public function beforePayeeBgcNumber(Node $node): void
77
    {
78
        if (!$this->payeeBgcNr) {
79
            $this->payeeBgcNr = (string)$node->getValue();
80
        }
81
82
        if ($node->getValue() != $this->payeeBgcNr) {
83
            $this->getErrorObject()->addError(
84
                "Non-matching payee BGC customer numbers (expecting: %s, found: %s) on line %s",
85
                $this->payeeBgcNr,
86
                (string)$node->getValue(),
87
                (string)$node->getLineNr()
88
            );
89
        }
90
    }
91
}
92