AccountVisitor::writeAccount()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 20
rs 9.8333
cc 4
nc 4
nop 2
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
use byrokrat\autogiro\Tree\Obj;
28
use byrokrat\banking\AccountFactoryInterface;
29
use byrokrat\banking\Exception as BankingException;
30
31
/**
32
 * Validates the structure of account numbers in tree
33
 *
34
 * Creates account object as child Node::OBJ
35
 */
36
final class AccountVisitor extends Visitor
37
{
38
    use ErrorAwareTrait;
39
40
    /**
41
     * @var AccountFactoryInterface
42
     */
43
    private $accountFactory;
44
45
    /**
46
     * @var AccountFactoryInterface
47
     */
48
    private $bankgiroFactory;
49
50
    public function __construct(
51
        ErrorObject $errorObj,
52
        AccountFactoryInterface $accountFactory,
53
        AccountFactoryInterface $bankgiroFactory
54
    ) {
55
        $this->setErrorObject($errorObj);
56
        $this->accountFactory = $accountFactory;
57
        $this->bankgiroFactory = $bankgiroFactory;
58
    }
59
60
    public function beforeAccount(Node $node): void
61
    {
62
        $this->writeAccount($node, $this->accountFactory);
63
    }
64
65
    public function beforePayeeBankgiro(Node $node): void
66
    {
67
        $this->writeAccount($node, $this->bankgiroFactory);
68
    }
69
70
    private function writeAccount(Node $node, AccountFactoryInterface $factory): void
71
    {
72
        if ($node->hasChild(Node::OBJ)) {
73
            return;
74
        }
75
76
        $number = ltrim((string)$node->getValueFrom(Node::NUMBER), '0');
77
78
        if (!$number) {
79
            return;
80
        }
81
82
        try {
83
            $node->addChild(new Obj($node->getLineNr(), $factory->createAccount($number)));
84
        } catch (BankingException $e) {
85
            $this->getErrorObject()->addError(
86
                "Invalid account number %s (%s) on line %s",
87
                $number,
88
                $e->getMessage(),
89
                (string)$node->getLineNr()
90
            );
91
        }
92
    }
93
}
94