Test Failed
Pull Request — master (#13)
by Sebastian
05:15
created

validateSyntax_no_other_tokens()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 10
rs 10
c 3
b 0
f 0
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Command_ShowVariable} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Command_ShowVariable
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use Mailcode\Interfaces\Commands\Validation\DecryptInterface;
15
use Mailcode\Interfaces\Commands\Validation\IDNEncodingInterface;
16
use Mailcode\Traits\Commands\Validation\DecryptTrait;
17
use Mailcode\Traits\Commands\Validation\IDNDecodeTrait;
18
use Mailcode\Traits\Commands\Validation\IDNEncodeTrait;
19
20
/**
21
 * Mailcode command: show a variable value.
22
 *
23
 * @package Mailcode
24
 * @subpackage Commands
25
 * @author Sebastian Mordziol <[email protected]>
26
 */
27
class Mailcode_Commands_Command_ShowVariable
28
    extends Mailcode_Commands_ShowBase
29
    implements
30
    IDNEncodingInterface, DecryptInterface
31
{
32
    public const VALIDATION_TOO_MANY_PARAMETERS = 69701;
33
34
    use IDNEncodeTrait;
35
    use IDNDecodeTrait;
36
    use DecryptTrait;
37
38
    /**
39
     * @var string|NULL
40
     */
41
    private static ?string $defaultDecryptionKey = null;
42
43
    public function getName(): string
44
    {
45
        return 'showvar';
46
    }
47
48
    public function getLabel(): string
49
    {
50
        return t('Show variable');
51
    }
52
53
    protected function getValidations(): array
54
    {
55
        return array(
56
            Mailcode_Interfaces_Commands_Validation_Variable::VALIDATION_NAME_VARIABLE,
57
            'no_other_tokens',
58
            DecryptInterface::VALIDATION_DECRYPT_NAME
59
        );
60
    }
61
62
    protected function validateSyntax_no_other_tokens(): void
63
    {
64
        $tokens = $this->requireParams()->getInfo()->getTokens();
65
        $allowed = $this->resolveActiveTokens();
66
67
        if (count($tokens) > count($allowed)) {
68
            $this->validationResult->makeError(
69
                t('Unknown parameters found:') . ' ' .
70
                t('Only the variable name and keywords should be specified.'),
71
                self::VALIDATION_TOO_MANY_PARAMETERS
72
            );
73
        }
74
    }
75
76
    /**
77
     * Gets all validated tokens that the command supports
78
     * (namely the variable, and keywords).
79
     *
80
     * @return Mailcode_Parser_Statement_Tokenizer_Token[]
81
     * @throws Mailcode_Exception
82
     */
83
    protected function resolveActiveTokens(): array
84
    {
85
        $allowed = array($this->getVariableToken());
86
87
        $encodings = $this->getSupportedEncodings();
88
89
        foreach ($encodings as $keyword) {
90
            $token = $this->getEncodingToken($keyword);
91
            if ($token) {
92
                $allowed[] = $token;
93
94
                $parameter = $this->requireParams()->getInfo()->getTokenForKeyWord($token->getKeyword());
95
                if ($parameter) {
96
                    $allowed[] = $parameter;
97
                }
98
            }
99
        }
100
101
        return $allowed;
102
    }
103
104
    /**
105
     * @param string|NULL $decryptionKey A decryption key
106
     * @return void
107
     */
108
    public static function setDefaultDecryptionKey(?string $decryptionKey): void
109
    {
110
        self::$defaultDecryptionKey = $decryptionKey;
111
    }
112
113
    /**
114
     * Gets the default decryption key for decryption. If not set via
115
     * {@see self::setDefaultDecryptionKey()}, this defaults to "default".
116
     *
117
     * @return string
118
     */
119
    public static function getDefaultDecryptionKey(): string
120
    {
121
        return self::$defaultDecryptionKey ?? "default";
122
    }
123
}
124