PaymentVisitor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 20
c 1
b 0
f 0
dl 0
loc 40
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeIncomingPaymentRequest() 0 4 1
A beforeOutgoingPaymentRequest() 0 4 1
A validateImmediateDateWithInterval() 0 9 3
A validateRepetitionsWithoutInterval() 0 11 3
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\Intervals;
27
use byrokrat\autogiro\Tree\Node;
28
use byrokrat\autogiro\Tree\ImmediateDate;
29
30
/**
31
 * Validate intervals, repetitions and dates of payment requests
32
 */
33
final class PaymentVisitor extends Visitor
34
{
35
    use ErrorAwareTrait;
36
37
    public function beforeIncomingPaymentRequest(Node $node): void
38
    {
39
        $this->validateImmediateDateWithInterval($node);
40
        $this->validateRepetitionsWithoutInterval($node);
41
    }
42
43
    public function beforeOutgoingPaymentRequest(Node $node): void
44
    {
45
        $this->validateImmediateDateWithInterval($node);
46
        $this->validateRepetitionsWithoutInterval($node);
47
    }
48
49
    private function validateImmediateDateWithInterval(Node $node): void
50
    {
51
        if (
52
            $node->getChild('date') instanceof ImmediateDate
53
            && $node->getChild('interval')->getValueFrom(Node::NUMBER) != Intervals::INTERVAL_ONCE
54
        ) {
55
            $this->getErrorObject()->addError(
56
                "Immediate dates and intervals can not be mixed in payment on line %s",
57
                (string)$node->getLineNr()
58
            );
59
        }
60
    }
61
62
    private function validateRepetitionsWithoutInterval(Node $node): void
63
    {
64
        if (
65
            $node->getChild('interval')->getValueFrom(Node::NUMBER) == Intervals::INTERVAL_ONCE
66
            && trim($node->getValueFrom('repetitions')) != ''
67
        ) {
68
            $this->getErrorObject()->addError(
69
                "Repetitions set (%s) but interval is once (%s) on line %s",
70
                $node->getValueFrom('repetitions'),
71
                $node->getChild('interval')->getValueFrom(Node::NUMBER),
72
                (string)$node->getLineNr()
73
            );
74
        }
75
    }
76
}
77