Passed
Pull Request — master (#13)
by Sebastian
04:02
created

renderDecryptionKey()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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