Mailcode_Commands_Command_Code   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 18
eloc 56
dl 0
loc 162
rs 10
c 3
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A requiresParameters() 0 3 1
A getName() 0 3 1
A supportsType() 0 3 1
A getLabel() 0 3 1
A supportsURLEncoding() 0 3 1
A supportsLogicKeywords() 0 3 1
A getDefaultType() 0 3 1
A generatesContent() 0 3 1
A getSyntaxName() 0 11 2
A getSupportedSyntaxes() 0 5 1
A getValidations() 0 6 1
A validateSyntax_token() 0 17 2
A isMailcodeEnabled() 0 3 1
A validateSyntax_language() 0 22 3
1
<?php
2
/**
3
 * File containing the {@see \Mailcode\Mailcode_Commands_Command_Code} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see \Mailcode\Mailcode_Commands_Command_Code
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode command: opening CODE statement.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Commands_Command_Code
22
    extends Mailcode_Commands_Command
23
    implements
24
        Mailcode_Commands_Command_Type_Standalone,
25
        Mailcode_Interfaces_Commands_ProtectedContent
26
{
27
    use Mailcode_Traits_Commands_ProtectedContent;
28
29
    public const ERROR_LANG_TOKEN_MISSING = 73101;
30
31
    public const VALIDATION_LANGUAGE_NOT_SPECIFIED = 72901;
32
    public const VALIDATION_UNKNOWN_LANGUAGE = 72902;
33
34
    /**
35
     * @var Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL
36
     */
37
    private ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $langToken = null;
38
39
    public function getName() : string
40
    {
41
        return 'code';
42
    }
43
    
44
    public function getLabel() : string
45
    {
46
        return t('Raw backend code');
47
    }
48
    
49
    public function supportsType(): bool
50
    {
51
        return false;
52
    }
53
54
    public function supportsURLEncoding() : bool
55
    {
56
        return false;
57
    }
58
59
    public function getDefaultType() : string
60
    {
61
        return '';
62
    }
63
    
64
    public function requiresParameters(): bool
65
    {
66
        return true;
67
    }
68
    
69
    public function supportsLogicKeywords() : bool
70
    {
71
        return false;
72
    }
73
    
74
    protected function getValidations() : array
75
    {
76
        return array(
77
            Mailcode_Interfaces_Commands_ProtectedContent::VALIDATION_NAME_CONTENT_ID,
78
            'token',
79
            'language'
80
        );
81
    }
82
    
83
    public function generatesContent() : bool
84
    {
85
        return true;
86
    }
87
88
    // region: Supported syntaxes
89
90
    public const SYNTAX_APACHE_VELOCITY = 'ApacheVelocity';
91
    public const SYNTAX_MAILCODE = 'Mailcode';
92
93
    /**
94
     * @return string[]
95
     */
96
    public static function getSupportedSyntaxes() : array
97
    {
98
        return array(
99
            self::SYNTAX_APACHE_VELOCITY,
100
            self::SYNTAX_MAILCODE
101
        );
102
    }
103
104
    /**
105
     * Retrieves the name of the syntax that the code is written in.
106
     *
107
     * @return string
108
     * @throws Mailcode_Exception If the command has no language parameter.
109
     *
110
     * @see Mailcode_Commands_Command_Code::ERROR_LANG_TOKEN_MISSING
111
     */
112
    public function getSyntaxName() : string
113
    {
114
        if(isset($this->langToken))
115
        {
116
            return $this->langToken->getText();
0 ignored issues
show
Bug introduced by
The method getText() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
            return $this->langToken->/** @scrutinizer ignore-call */ getText();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
        }
118
119
        throw new Mailcode_Exception(
120
            'No language name available',
121
            'The command has no lang token.',
122
            self::ERROR_LANG_TOKEN_MISSING
123
        );
124
    }
125
126
    // endregion
127
128
    /**
129
     * Ensures that the language token is present.
130
     */
131
    protected function validateSyntax_token() : void
132
    {
133
        $lang = $this->requireParams()
134
            ->getInfo()
135
            ->getStringLiteralByIndex(1);
136
137
        if($lang)
138
        {
139
            $this->langToken = $lang;
140
            return;
141
        }
142
143
        $this->validationResult->makeError(
144
            t('The target language has to be specified in the first parameter of the command.').' '.
145
            t('Possible values are:').' '.
146
            '<code>'.implode('</code>, <code>', Mailcode_Translator::create()->getSyntaxNames()).'</code>',
147
            self::VALIDATION_LANGUAGE_NOT_SPECIFIED
148
        );
149
    }
150
151
    /**
152
     * Ensures that the syntax specified in the language token
153
     * is a valid translator syntax name.
154
     */
155
    protected function validateSyntax_language() : void
156
    {
157
        // To keep PHPStan happy. If no token has been found, this
158
        // method will not be called.
159
        if(!isset($this->langToken))
160
        {
161
            return;
162
        }
163
164
        $name = $this->langToken->getText();
165
        $supported = self::getSupportedSyntaxes();
166
167
        if(in_array($name, $supported))
168
        {
169
            return;
170
        }
171
172
        $this->validationResult->makeError(
173
            t('The language %1$s does not exist.', '<code>'.$name.'</code>').' '.
174
            t('Possible values are:').' '.
175
            '<code>'.implode('</code>, <code>', $supported).'</code>',
176
            self::VALIDATION_UNKNOWN_LANGUAGE
177
        );
178
    }
179
180
    public function isMailcodeEnabled() : bool
181
    {
182
        return false;
183
    }
184
}
185