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

TransactionProcessor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeIncomingTransactionRequestNode() 0 5 1
A beforeOutgoingTransactionRequestNode() 0 4 1
A validateImmediateDateWithInterval() 0 9 3
A validateRepetitionsWithoutInterval() 0 10 3
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro\Processor.
4
 *
5
 * byrokrat\autogiro\Processor 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\Processor 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\Processor. 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\Record\Request\IncomingTransactionRequestNode;
26
use byrokrat\autogiro\Tree\Record\Request\OutgoingTransactionRequestNode;
27
use byrokrat\autogiro\Tree\Date\ImmediateDateNode;
28
29
/**
30
 * Validate intervals, repetitions and dates of transaction requests
31
 */
32
class TransactionProcessor extends Processor
33
{
34
    public function beforeIncomingTransactionRequestNode(IncomingTransactionRequestNode $node)
35
    {
36
        $this->validateImmediateDateWithInterval($node);
37
        $this->validateRepetitionsWithoutInterval($node);
38
    }
39
40
    public function beforeOutgoingTransactionRequestNode(OutgoingTransactionRequestNode $node)
41
    {
42
        $this->validateImmediateDateWithInterval($node);
43
    }
44
45
    private function validateImmediateDateWithInterval(IncomingTransactionRequestNode $node)
46
    {
47
        if ($node->getChild('date') instanceof ImmediateDateNode && $node->getChild('interval')->getValue() != '0') {
48
            $this->addError(
49
                "Immediate dates and intervals can not be mixed in transaction on line %s",
50
                (string)$node->getLineNr()
51
            );
52
        }
53
    }
54
55
    private function validateRepetitionsWithoutInterval(IncomingTransactionRequestNode $node)
56
    {
57
        if ($node->getChild('interval')->getValue() == '0' && trim($node->getChild('repetitions')->getValue()) != '') {
58
            $this->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