Completed
Push — master ( 7dbffa...714da8 )
by Hannes
05:26
created

AccountVisitor::writeAccountAttr()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 3
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-17 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Visitor;
24
25
use byrokrat\autogiro\Tree\AccountNode;
26
use byrokrat\autogiro\Tree\ReferredAccountNode;
27
use byrokrat\autogiro\Tree\PayeeBankgiroNode;
28
use byrokrat\banking\AccountFactory;
29
use byrokrat\banking\Exception as BankingException;
30
31
/**
32
 * Validates the structure of account numbers in tree
33
 */
34
class AccountVisitor extends ErrorAwareVisitor
35
{
36
    /**
37
     * @var AccountFactory
38
     */
39
    private $accountFactory;
40
41
    /**
42
     * @var AccountFactory
43
     */
44
    private $bankgiroFactory;
45
46
    public function __construct(ErrorObject $errorObj, AccountFactory $accountFactory, AccountFactory $bankgiroFactory)
47
    {
48
        parent::__construct($errorObj);
49
        $this->accountFactory = $accountFactory;
50
        $this->bankgiroFactory = $bankgiroFactory;
51
    }
52
53
    public function beforeAccountNode(AccountNode $node)
54
    {
55
        $this->writeAccountAttr($node->getValue(), $node, $this->accountFactory);
56
    }
57
58
    public function beforeReferredAccountNode(ReferredAccountNode $node)
59
    {
60
        if (!$node->hasAttribute('referred_value')) {
61
            return;
62
        }
63
64
        $this->writeAccountAttr($node->getAttribute('referred_value'), $node, $this->accountFactory);
65
    }
66
67
    public function beforePayeeBankgiroNode(PayeeBankgiroNode $node)
68
    {
69
        $this->writeAccountAttr($node->getValue(), $node, $this->bankgiroFactory);
70
    }
71
72
    private function writeAccountAttr(string $number, AccountNode $node, AccountFactory $factory)
73
    {
74
        if ($node->hasAttribute('account')) {
75
            return;
76
        }
77
78
        try {
79
            $node->setAttribute(
80
                'account',
81
                $factory->createAccount($number)
82
            );
83
        } catch (BankingException $e) {
84
            $this->getErrorObject()->addError(
85
                "Invalid account number %s on line %s",
86
                $number,
87
                (string)$node->getLineNr()
88
            );
89
        }
90
    }
91
}
92