Test Failed
Push — master ( f51294...9c5f94 )
by Sebastian
03:08
created

Mailcode_Translator_Syntax_ApacheVelocity_Base_AbstractIf   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 94
rs 10
c 1
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _translate() 0 12 2
A _translateContains() 0 13 2
A _translateEmpty() 0 13 2
A _translateGeneric() 0 10 2
A _translateVariable() 0 7 1
A getSign() 0 18 3
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Translator_Syntax_ApacheVelocity_Base_AbstractIf} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Translator
7
 * @see Mailcode_Translator_Syntax_ApacheVelocity_Base_AbstractIf
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Abstract base class for the IF/ELSEIF command translation classes.
16
 *
17
 * @package Mailcode
18
 * @subpackage Translator
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
abstract class Mailcode_Translator_Syntax_ApacheVelocity_Base_AbstractIf extends Mailcode_Translator_Syntax_ApacheVelocity
22
{
23
    const ERROR_CANNOT_GET_KEYWORD_SIGN = 60801;
24
    
25
    abstract protected function getCommandTemplate() : string;
26
27
    abstract protected function translateBody(Mailcode_Commands_IfBase $command) : string;
28
    
29
    protected function _translate(Mailcode_Commands_IfBase $command): string
30
    {
31
        $body = $this->translateBody($command);
32
        
33
        $keywords = $command->getLogicKeywords()->getKeywords();
34
        
35
        foreach($keywords as $keyword)
36
        {
37
            $body .= ' '.$this->getSign($keyword).' '.$this->translateBody($keyword->getCommand());
38
        }
39
        
40
        return sprintf($this->getCommandTemplate(), $body);
41
    }
42
    
43
    protected function getSign(Mailcode_Commands_LogicKeywords_Keyword $keyword) : string
44
    {
45
        switch($keyword->getName())
46
        {
47
            case 'and':
48
                return '&&';
49
                
50
            case 'or':
51
                return '||';
52
        }
53
        
54
        throw new Mailcode_Exception(
55
            'Unknown keyword name',
56
            sprintf(
57
                'The keyword name [%s] is not known and cannot be translated to a velocity sign.',
58
                $keyword->getName()
59
            ),
60
            self::ERROR_CANNOT_GET_KEYWORD_SIGN
61
        );
62
    }
63
    
64
    protected function _translateEmpty(Mailcode_Variables_Variable $variable, bool $notEmpty) : string
65
    {
66
        $sign = '';
67
        
68
        if($notEmpty)
69
        {
70
            $sign = '!';
71
        }
72
        
73
        return sprintf(
74
            '%s$StringUtils.isEmpty(%s)',
75
            $sign,
76
            $variable->getFullName()
77
        );
78
    }
79
    
80
    protected function _translateGeneric(Mailcode_Commands_IfBase $command) : string
81
    {
82
        $params = $command->getParams();
83
        
84
        if(!$params)
0 ignored issues
show
introduced by
$params is of type Mailcode\Mailcode_Parser_Statement, thus it always evaluated to true.
Loading history...
85
        {
86
            return '';
87
        }
88
        
89
        return $params->getNormalized();
90
    }
91
    
92
    protected function _translateVariable(Mailcode_Variables_Variable $variable, string $comparator, string $value) : string
93
    {
94
        return sprintf(
95
            '%s %s %s',
96
            $variable->getFullName(),
97
            $comparator,
98
            $value
99
        );
100
    }
101
    
102
    protected function _translateContains(Mailcode_Variables_Variable $variable, bool $caseSensitive, string $searchTerm) : string
103
    {
104
        $opts = 's';
105
        if($caseSensitive)
106
        {
107
            $opts = 'is';
108
        }
109
        
110
        return sprintf(
111
            '%s.matches("(?%s)%s")',
112
            $variable->getFullName(),
113
            $opts,
114
            $this->filterRegexString(trim($searchTerm, '"'))
115
        );
116
    }
117
}
118