Completed
Push — master ( 7e81c7...bd5f3a )
by Hannes
01:39
created

PaymentVisitor::beforeOutgoingPaymentRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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-18 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Visitor;
24
25
use byrokrat\autogiro\Tree\Request\IncomingPaymentRequest;
26
use byrokrat\autogiro\Tree\Request\OutgoingPaymentRequest;
27
use byrokrat\autogiro\Tree\ImmediateDateNode;
28
29
/**
30
 * Validate intervals, repetitions and dates of payment requests
31
 */
32
class PaymentVisitor extends ErrorAwareVisitor
33
{
34
    public function beforeIncomingPaymentRequest(IncomingPaymentRequest $node): void
35
    {
36
        $this->validateImmediateDateWithInterval($node);
37
        $this->validateRepetitionsWithoutInterval($node);
38
    }
39
40
    public function beforeOutgoingPaymentRequest(OutgoingPaymentRequest $node): void
41
    {
42
        $this->validateImmediateDateWithInterval($node);
43
    }
44
45
    private function validateImmediateDateWithInterval(IncomingPaymentRequest $node): void
46
    {
47
        if ($node->getChild('date') instanceof ImmediateDateNode && $node->getChild('interval')->getValue() != '0') {
48
            $this->getErrorObject()->addError(
49
                "Immediate dates and intervals can not be mixed in payment on line %s",
50
                (string)$node->getLineNr()
51
            );
52
        }
53
    }
54
55
    private function validateRepetitionsWithoutInterval(IncomingPaymentRequest $node): void
56
    {
57
        if ($node->getChild('interval')->getValue() == '0' && trim($node->getChild('repetitions')->getValue()) != '') {
58
            $this->getErrorObject()->addError(
59
                "Repetitions set (%s) but no interval is definied on line %s",
60
                $node->getChild('repetitions')->getValue(),
61
                (string)$node->getLineNr()
62
            );
63
        }
64
    }
65
}
66