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

ShowURLTranslation::renderTracking()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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