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

ShowVariableTranslation::translate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 1
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