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

beforeMandateResponseClosing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
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\LayoutNode;
26
use byrokrat\autogiro\Tree\Response\ResponseOpening;
27
use byrokrat\autogiro\Tree\Response\MandateResponseClosing;
28
29
/**
30
 * Validate dates and record count in mandate response layouts
31
 */
32
class MandateResponseVisitor extends ErrorAwareVisitor
33
{
34
    /**
35
     * @var string Current date from opening record
36
     */
37
    private $date;
38
39
    /**
40
     * @var int The number of records in layout
41
     */
42
    private $recordCount;
43
44
    /**
45
     * Collect date from opening record
46
     */
47
    public function beforeResponseOpening(ResponseOpening $node): void
48
    {
49
        $this->date = $node->getChild('date')->getValue();
50
    }
51
52
    /**
53
     * Validate that date in closing record matches date in opening record
54
     */
55
    public function beforeMandateResponseClosing(MandateResponseClosing $node): void
56
    {
57
        if ($node->getChild('date')->getValue() != $this->date) {
58
            $this->getErrorObject()->addError(
59
                "Non-matching dates in opening and closing nodes (opening: %s, closing: %s) on line %s",
60
                $this->date,
61
                $node->getChild('date')->getValue(),
62
                (string)$node->getLineNr()
63
            );
64
        }
65
    }
66
67
    /**
68
     * Collect the number of expected records in layout
69
     */
70
    public function afterMandateResponseClosing(MandateResponseClosing $node): void
71
    {
72
        $this->recordCount = (int)$node->getChild('nr_of_posts')->getValue();
73
    }
74
75
    /**
76
    * Validate that the number of records in layout matches the expected value
77
     */
78
    public function afterLayoutNode(LayoutNode $node): void
79
    {
80
        if ($this->recordCount && $this->recordCount != count($node->getChildren()) - 2) {
81
            $this->getErrorObject()->addError(
82
                "Invalid record count (found: %s, expecting: %s) on line %s",
83
                (string)(count($node->getChildren()) - 2),
84
                (string)($this->recordCount),
85
                (string)$node->getLineNr()
86
            );
87
        }
88
    }
89
}
90