Passed
Push — master ( d8dc61...dfdb64 )
by Sebastian
09:05
created

ShowVariableTranslation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 19
c 0
b 0
f 0
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 12 2
A renderDecryptionKey() 0 27 3
1
<?php
2
/**
3
 * @package Mailcode
4
 * @subpackage Translator
5
 */
6
7
declare(strict_types=1);
8
9
namespace Mailcode\Translator\Syntax\ApacheVelocity;
10
11
use Mailcode\Mailcode_Commands_Command_ShowVariable;
12
use Mailcode\Mailcode_Translator_Command_ShowVariable;
13
use Mailcode\Translator\Syntax\ApacheVelocity;
14
use function Mailcode\dollarize;
15
use function Mailcode\undollarize;
16
17
/**
18
 * Translates the {@see Mailcode_Translator_Command_ShowVariable} command to Apache Velocity.
19
 *
20
 * @package Mailcode
21
 * @subpackage Translator
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class ShowVariableTranslation extends ApacheVelocity implements Mailcode_Translator_Command_ShowVariable
25
{
26
    public function translate(Mailcode_Commands_Command_ShowVariable $command): string
27
    {
28
        if($command->isDecryptionEnabled())
29
        {
30
            $varName = $this->renderDecryptionKey($command);
31
        }
32
        else
33
        {
34
            $varName = undollarize($command->getVariableName());
35
        }
36
37
        return $this->renderVariableEncodings($command, $varName);
38
    }
39
40
    private function renderDecryptionKey(Mailcode_Commands_Command_ShowVariable $command) : string
41
    {
42
        $keyName = $command->getDecryptionKeyName();
43
44
        if(!empty($keyName))
45
        {
46
            $varName = sprintf(
47
                'text.decrypt(%s, "%s")',
48
                dollarize($command->getVariableName()),
49
                $keyName
50
            );
51
        }
52
        else
53
        {
54
            // This will make the decryption system use the system's
55
            // default key name.
56
            $varName = sprintf(
57
                'text.decrypt(%s)',
58
                dollarize($command->getVariableName())
59
            );
60
        }
61
62
        if($this->hasVariableEncodings($command)) {
63
            $varName = '${'.$varName.'}';
64
        }
65
66
        return $varName;
67
    }
68
}
69