ShowURLTranslation   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 42
c 0
b 0
f 0
dl 0
loc 119
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A resetURLCounter() 0 3 1
A renderTracking() 0 5 1
A renderQueryParam() 0 6 1
A renderURLTemplate() 0 6 1
A translate() 0 36 5
A renderURL() 0 5 1
A renderShorten() 0 3 1
A resolveURL() 0 10 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\BaseApacheVelocityCommandTranslation;
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 BaseApacheVelocityCommandTranslation 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->isShortenEnabled())
61
        {
62
            $statements[] = $this->renderShorten();
63
        }
64
65
        if($command->hasQueryParams())
66
        {
67
            $params = $command->getQueryParams();
68
69
            foreach($params as $name => $value)
70
            {
71
                $statements[] = $this->renderQueryParam($name, $value);
72
            }
73
        }
74
75
        return sprintf(
76
            '%s${tracking.%s}',
77
            $this->renderURLTemplate($command, $urlVar),
78
            implode('.', $statements)
79
        );
80
    }
81
82
    /**
83
     * @param Mailcode_Commands_Command_ShowURL $command
84
     * @param string $urlVar
85
     * @return string
86
     * @throws Mailcode_Exception
87
     * @throws Mailcode_Translator_Exception
88
     */
89
    private function renderURLTemplate(Mailcode_Commands_Command_ShowURL $command, string $urlVar) : string
90
    {
91
        return sprintf(
92
            '#{define}($%s)%s#{end}',
93
            $urlVar,
94
            $this->resolveURL($command)
95
        );
96
    }
97
98
    private function renderQueryParam(string $name, string $value) : string
99
    {
100
        return sprintf(
101
            'query(%s, %s)',
102
            $this->renderQuotedValue($name),
103
            $this->renderQuotedValue($value)
104
        );
105
    }
106
107
    private function renderTracking(Mailcode_Commands_Command_ShowURL $command) : string
108
    {
109
        return sprintf(
110
            "lt(\${tracking_host}, \${envelope.hash}, %s)",
111
            $this->renderQuotedValue($command->getTrackingID())
112
        );
113
    }
114
115
    private function renderShorten() : string
116
    {
117
        return 'shorten()';
118
    }
119
120
    private function renderURL(string $urlVar) : string
121
    {
122
        return sprintf(
123
            'url(${%s})',
124
            $urlVar
125
        );
126
    }
127
128
    /**
129
     * @param Mailcode_Commands_Command_ShowURL $command
130
     * @return string
131
     * @throws Mailcode_Exception
132
     * @throws Mailcode_Translator_Exception
133
     */
134
    private function resolveURL(Mailcode_Commands_Command_ShowURL $command) : string
135
    {
136
        // Remove newlines in the content.
137
        $content = trim(str_replace(array("\r", "\n"), '', $command->getContent()));
138
139
        $safeguard = Mailcode::create()->createSafeguard($content);
140
141
        return Mailcode::create()->createTranslator()
142
            ->createSyntax($this->getSyntaxName())
143
            ->translateSafeguard($safeguard);
144
    }
145
}
146