AmountVisitor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 20
c 3
b 0
f 0
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A beforeAmount() 0 24 4
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 Money\Money;
29
use Money\MoneyParser;
30
31
/**
32
 * Create amount object under child Node::OBJ
33
 */
34
final class AmountVisitor extends Visitor
35
{
36
    use ErrorAwareTrait;
37
38
    /**
39
     * @var MoneyParser
40
     */
41
    private $moneyParser;
42
43
    public function __construct(ErrorObject $errorObj, MoneyParser $moneyParser)
44
    {
45
        $this->setErrorObject($errorObj);
46
        $this->moneyParser = $moneyParser;
47
    }
48
49
    public function beforeAmount(Node $node): void
50
    {
51
        if ($node->hasChild(Node::OBJ)) {
52
            return;
53
        }
54
55
        $signalStr = (string)$node->getValueFrom('Text');
56
57
        if (trim($signalStr) == '') {
58
            return;
59
        }
60
61
        try {
62
            $node->addChild(
63
                new Obj(
64
                    $node->getLineNr(),
65
                    $this->moneyParser->parse($signalStr)
66
                )
67
            );
68
        } catch (\Exception $e) {
69
            $this->getErrorObject()->addError(
70
                "Invalid signaled amount %s on line %s",
71
                (string)$node->getValueFrom('Text'),
72
                (string)$node->getLineNr()
73
            );
74
        }
75
    }
76
}
77