Completed
Push — master ( 2917fa...796bfc )
by Hannes
02:44
created

MessageVisitor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A beforeAutogiroFile() 0 5 1
A beforeRecord() 0 4 1
A beforeMessage() 0 25 2
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\MessageRetriever;
26
use byrokrat\autogiro\Tree\Node;
27
use byrokrat\autogiro\Tree\Text;
28
29
/**
30
 * Visitor of message nodes in tree
31
 *
32
 * Creates string message as child 'Text'
33
 */
34
class MessageVisitor extends ErrorAwareVisitor
35
{
36
    /**
37
     * @var MessageRetriever
38
     */
39
    private $messages;
40
41
    /**
42
     * @var string
43
     */
44
    private $layoutName = '';
45
46
    /**
47
     * @var string
48
     */
49
    private $recordName = '';
50
51
    public function __construct(ErrorObject $errorObj, MessageRetriever $messages = null)
52
    {
53
        parent::__construct($errorObj);
54
        $this->messages = $messages ?: new MessageRetriever;
55
    }
56
57
    public function beforeAutogiroFile(Node $node): void
58
    {
59
        $this->layoutName = $node->getName();
60
        $this->recordName = '';
61
    }
62
63
    public function beforeRecord(Node $node): void
64
    {
65
        $this->recordName = $node->getName();
66
    }
67
68
    public function beforeMessage(Node $node): void
69
    {
70
        $code = (string)$node->getValueFrom('Number');
71
72
        $message = $this->messages->readMessage(
73
            $this->layoutName,
74
            $this->recordName,
75
            $node->getName(),
76
            $code
77
        );
78
79
        if (!$message) {
80
            $this->getErrorObject()->addError(
81
                "Invalid message id '%s:%s:%s:%s' on line %s",
82
                $this->layoutName,
83
                $this->recordName,
84
                $node->getName(),
85
                $code,
86
                (string)$node->getLineNr()
87
            );
88
            return;
89
        }
90
91
        $node->addChild(new Text($node->getLineNr(), $message));
92
    }
93
}
94