Passed
Push — master ( 1515ff...7c0b30 )
by Sebastian
04:21
created

translateBody()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 38
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 15
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 38
rs 8.4444
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Translator_Syntax_ApacheVelocity_If} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Translator
7
 * @see Mailcode_Translator_Syntax_ApacheVelocity_If
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Translates the "If" command to Apache Velocity.
16
 *
17
 * @package Mailcode
18
 * @subpackage Translator
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Translator_Syntax_ApacheVelocity_If extends Mailcode_Translator_Syntax_ApacheVelocity_Base_AbstractIf implements Mailcode_Translator_Command_If
22
{
23
    protected function getCommandTemplate() : string
24
    {
25
        return '#if(%s)';
26
    }
27
    
28
    public function translate(Mailcode_Commands_Command_If $command): string
29
    {
30
        return $this->_translate($command);
31
    }
32
    
33
    protected function translateBody(Mailcode_Commands_IfBase $command) : string
34
    {
35
        if($command instanceof Mailcode_Commands_Command_If_Command)
36
        {
37
            return $this->translateCommand($command);
38
        }
39
        
40
        if($command instanceof Mailcode_Commands_Command_If_Variable)
41
        {
42
            return $this->translateVariable($command);
43
        }
44
        
45
        if($command instanceof Mailcode_Commands_Command_If_Contains)
46
        {
47
            return $this->translateContains($command);
48
        }
49
        
50
        if($command instanceof Mailcode_Commands_Command_If_Empty)
51
        {
52
            return $this->translateEmpty($command);
53
        }
54
        
55
        if($command instanceof Mailcode_Commands_Command_If_NotEmpty)
56
        {
57
            return $this->translateNotEmpty($command);
58
        }
59
        
60
        if($command instanceof Mailcode_Commands_Command_If_BeginsWith)
61
        {
62
            return $this->translateBeginsWith($command);
63
        }
64
        
65
        if($command instanceof Mailcode_Commands_Command_If_EndsWith)
66
        {
67
            return $this->translateEndsWith($command);
68
        }
69
        
70
        return '';
71
    }
72
73
    protected function translateCommand(Mailcode_Commands_Command_If_Command $command) : string
74
    {
75
        return $this->_translateGeneric($command);
76
    }
77
    
78
    protected function translateBeginsWith(Mailcode_Commands_Command_If_BeginsWith $command) : string
79
    {
80
        return $this->_translateSearch(
81
            'starts',
82
            $command->getVariable(),
83
            $command->isCaseInsensitive(),
84
            $command->getSearchTerm()
85
        );
86
    }
87
    
88
    protected function translateEndsWith(Mailcode_Commands_Command_If_EndsWith $command) : string
89
    {
90
        return $this->_translateSearch(
91
            'ends',
92
            $command->getVariable(),
93
            $command->isCaseInsensitive(),
94
            $command->getSearchTerm()
95
        );
96
    }
97
    
98
    protected function translateVariable(Mailcode_Commands_Command_If_Variable $command) : string
99
    {
100
        return $this->_translateVariable(
101
            $command->getVariable(), 
102
            $command->getSign(), 
103
            $command->getValue()
104
        );
105
    }
106
    
107
    protected function translateContains(Mailcode_Commands_Command_If_Contains $command) : string
108
    {
109
        return $this->_translateContains(
110
            $command->getVariable(), 
111
            $command->isCaseInsensitive(), 
112
            $command->getSearchTerm()
113
        );
114
    }
115
    
116
    protected function translateEmpty(Mailcode_Commands_Command_If_Empty $command) : string
117
    {
118
        return $this->_translateEmpty($command->getVariable(), false);
119
    }
120
    
121
    protected function translateNotEmpty(Mailcode_Commands_Command_If_NotEmpty $command) : string
122
    {
123
        return $this->_translateEmpty($command->getVariable(), true);
124
    }
125
}
126