Passed
Push — master ( 742ef2...7822c6 )
by Sebastian
04:11
created

ShowURL::renderURLTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * File containing the class {@see \Mailcode\Translator\Syntax\ApacheVelocity\ShowURL}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Translator
7
 * @see \Mailcode\Translator\Syntax\ApacheVelocity\ShowURL
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Translator\Syntax\ApacheVelocity;
13
14
use AppUtils\ConvertHelper;
15
use Mailcode\Mailcode;
16
use Mailcode\Mailcode_Commands_Command_ShowURL;
17
use Mailcode\Mailcode_Translator_Syntax_ApacheVelocity;
18
use Mailcode\Translator\Command\ShowURLInterface;
19
20
/**
21
 * Translates the `showurl` command to ApacheVelocity.
22
 *
23
 * @package Mailcode
24
 * @subpackage Translator
25
 * @author Sebastian Mordziol <[email protected]>
26
 */
27
class ShowURL extends Mailcode_Translator_Syntax_ApacheVelocity implements ShowURLInterface
28
{
29
    public const URL_VAR_TEMPLATE = 'url_tpl%03d';
30
31
    private static int $urlCounter = 0;
32
33
34
    public static function resetURLCounter() : void
35
    {
36
        self::$urlCounter = 0;
37
    }
38
39
    public function translate(Mailcode_Commands_Command_ShowURL $command) : string
40
    {
41
        self::$urlCounter++;
42
43
        $urlVar = sprintf(
44
            self::URL_VAR_TEMPLATE,
45
            self::$urlCounter
46
        );
47
48
        $statements = array();
49
        $statements[] = $this->renderURL($urlVar);
50
51
        if($command->isTrackingEnabled())
52
        {
53
            $statements[] = $this->renderTracking($command);
54
        }
55
56
        if($command->hasQueryParams())
57
        {
58
            $params = $command->getQueryParams();
59
60
            foreach($params as $name => $value)
61
            {
62
                $statements[] = $this->renderQueryParam($name, $value);
63
            }
64
        }
65
66
        return sprintf(
67
            '%s$tracking.%s',
68
            $this->renderURLTemplate($command, $urlVar),
69
            implode('.', $statements)
70
        );
71
    }
72
73
    private function renderURLTemplate(Mailcode_Commands_Command_ShowURL $command, string $urlVar) : string
74
    {
75
        return sprintf(
76
            '#define($%s)%s#end',
77
            $urlVar,
78
            $this->resolveURL($command)
79
        );
80
    }
81
82
    private function renderQueryParam(string $name, string $value) : string
83
    {
84
        return sprintf(
85
            'query(%s, %s)',
86
            $this->renderQuotedValue($name),
87
            $this->renderQuotedValue($value)
88
        );
89
    }
90
91
    private function renderTracking(Mailcode_Commands_Command_ShowURL $command) : string
92
    {
93
        return sprintf(
94
            "lt(\${tracking_host}, \${envelope_hash}, %s)",
95
            $this->renderQuotedValue($command->getTrackingID())
96
        );
97
    }
98
99
    private function renderURL(string $urlVar) : string
100
    {
101
        return sprintf(
102
            'url(${%s})',
103
            $urlVar
104
        );
105
    }
106
107
    private function resolveURL(Mailcode_Commands_Command_ShowURL $command) : string
108
    {
109
        // Remove newlines in the content.
110
        $content = trim(str_replace(array("\r", "\n"), '', $command->getContent()));
111
112
        $safeguard = Mailcode::create()->createSafeguard($content);
113
114
        return Mailcode::create()->createTranslator()
115
            ->createSyntax($this->getSyntaxName())
116
            ->translateSafeguard($safeguard);
117
    }
118
}
119