Passed
Pull Request — master (#13)
by Sebastian
04:18
created

DecryptTrait::validateSyntax_check_decrypt()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
nc 3
nop 0
dl 0
loc 19
rs 9.8666
c 1
b 0
f 0
1
<?php
2
/**
3
 * File containing the trait {@see \Mailcode\Traits\Commands\Validation\DecryptTrait}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see \Mailcode\Traits\Commands\Validation\DecryptTrait
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Traits\Commands\Validation;
13
14
use Mailcode\Interfaces\Commands\Validation\DecryptInterface;
15
use Mailcode\Mailcode_Commands_Command_ShowVariable;
16
use Mailcode\Mailcode_Commands_Keywords;
17
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral;
18
use function Mailcode\t;
19
20
/**
21
 * @package Mailcode
22
 * @subpackage Validation
23
 * @author Olaf Böcker <[email protected]>
24
 *
25
 * @see DecryptInterface
26
 */
27
trait DecryptTrait
28
{
29
    private ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $decryptionKeyToken = null;
30
31
    protected function validateSyntax_check_decrypt(): void
32
    {
33
        $token = $this
34
            ->requireParams()
0 ignored issues
show
Bug introduced by
It seems like requireParams() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

34
            ->/** @scrutinizer ignore-call */ requireParams()
Loading history...
35
            ->getInfo()
36
            ->getTokenByParamName(DecryptInterface::PARAMETER_NAME);
37
38
        if($token === null) {
39
            return;
40
        }
41
42
        if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
43
            $this->decryptionKeyToken = $token;
44
            return;
45
        }
46
47
        $this->validationResult->makeError(
48
            t('Invalid decryption key token:') . ' ' . t('Expected a string.'),
49
            DecryptInterface::VALIDATION_DECRYPT_CODE_WRONG_TYPE
50
        );
51
    }
52
53
    public function isDecryptionEnabled() : bool
54
    {
55
        return $this->getDecryptionKeyToken() !== null;
56
    }
57
58
    public function getDecryptionKey() : string
59
    {
60
        $key = $this->getDecryptionKeyToken();
61
        if($key !== null) {
62
            return $key->getText();
63
        }
64
65
        return '';
66
    }
67
68
    public function enableDecryption(string $key=DecryptInterface::DEFAULT_DECRYPTION_KEY) : self
69
    {
70
        $this->decryptionKeyToken = $this
71
            ->requireParams()
72
            ->getInfo()
73
            ->addParamString(DecryptInterface::PARAMETER_NAME, $key);
74
75
        return $this;
76
    }
77
78
    public function disableDecryption() : self
79
    {
80
        if(isset($this->decryptionKeyToken)) {
81
            $this
82
                ->requireParams()
83
                ->getInfo()
84
                ->removeToken($this->decryptionKeyToken);
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * Gets the decryption key to use for the command. If none has
92
     * been specified in the original command, the default
93
     * decryption key is used as defined via {@see Mailcode_Commands_Command_ShowVariable::setDefaultDecryptionKey()}.
94
     *
95
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL
96
     */
97
    public function getDecryptionKeyToken(): ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
98
    {
99
        return $this->decryptionKeyToken;
100
    }
101
}
102