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

SetVariableTranslation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 16
c 1
b 0
f 0
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 16 3
A buildCountAssignment() 0 11 2
1
<?php
2
/**
3
 * @package Mailcode
4
 * @subpackage Translator
5
 */
6
7
declare(strict_types=1);
8
9
namespace Mailcode\Translator\Syntax\HubL;
10
11
use Mailcode\Mailcode_Commands_Command_SetVariable;
12
use Mailcode\Mailcode_Translator_Command_SetVariable;
13
use Mailcode\Translator\Syntax\HubL;
14
15
/**
16
 * Translates the {@see Mailcode_Commands_Command_SetVariable} command to HubL.
17
 *
18
 * @package Mailcode
19
 * @subpackage Translator
20
 * @author Sebastian Mordziol <[email protected]>
21
 */
22
class SetVariableTranslation extends HubL implements Mailcode_Translator_Command_SetVariable
23
{
24
    public function translate(Mailcode_Commands_Command_SetVariable $command): string
25
    {
26
        $assignmentString = $command->getAssignmentString();
27
28
        if ($command->isCountEnabled())
29
        {
30
            $result = $this->buildCountAssignment($command);
31
            if($result !== null) {
32
                $assignmentString = $result;
33
            }
34
        }
35
36
        return sprintf(
37
            '{%% set %s = %s %%}',
38
            $this->formatVariableName($command->getVariable()->getFullName()),
39
            $this->formatVariablesInString($assignmentString)
40
        );
41
    }
42
43
    private function buildCountAssignment(Mailcode_Commands_Command_SetVariable $command) : ?string
44
    {
45
        $variable = $command->getCountVariable();
46
47
        if ($variable === null) {
48
            return null;
49
        }
50
51
        return sprintf(
52
            '%s|length',
53
            $this->formatVariableName($variable->getFullName())
54
        );
55
    }
56
}
57