Completed
Push — master ( 03230b...27b37b )
by Hannes
02:11
created

AccountProcessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 50
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A beforeAccountNode() 0 15 2
A beforePayeeBankgiroNode() 0 15 2
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\AccountNode;
26
use byrokrat\autogiro\Tree\PayeeBankgiroNode;
27
use byrokrat\banking\AccountFactory;
28
use byrokrat\banking\Exception as BankingException;
29
30
/**
31
 * Validates the structure of account numbers in tree
32
 */
33
class AccountProcessor extends Processor
34
{
35
    /**
36
     * @var AccountFactory
37
     */
38
    private $accountFactory;
39
40
    /**
41
     * @var AccountFactory
42
     */
43
    private $bankgiroFactory;
44
45
    public function __construct(AccountFactory $accountFactory, AccountFactory $bankgiroFactory)
46
    {
47
        $this->accountFactory = $accountFactory;
48
        $this->bankgiroFactory = $bankgiroFactory;
49
    }
50
51
    public function beforeAccountNode(AccountNode $node)
52
    {
53
        try {
54
            $node->setAttribute(
55
                'account',
56
                $this->accountFactory->createAccount($node->getValue())
57
            );
58
        } catch (BankingException $e) {
59
            $this->addError(
60
                "Invalid account number %s on line %s",
61
                $node->getValue(),
62
                (string)$node->getLineNr()
63
            );
64
        }
65
    }
66
67
    public function beforePayeeBankgiroNode(PayeeBankgiroNode $node)
68
    {
69
        try {
70
            $node->setAttribute(
71
                'account',
72
                $this->bankgiroFactory->createAccount($node->getValue())
73
            );
74
        } catch (BankingException $e) {
75
            $this->addError(
76
                "Invalid bankgiro number %s on line %s",
77
                $node->getValue(),
78
                (string)$node->getLineNr()
79
            );
80
        }
81
    }
82
}
83