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

DecryptTrait::isDecryptionEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
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\Decrypt\DecryptSettings;
15
use Mailcode\Interfaces\Commands\Validation\DecryptInterface;
16
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral;
17
use function Mailcode\t;
18
19
/**
20
 * @package Mailcode
21
 * @subpackage Validation
22
 * @author Olaf Böcker <[email protected]>
23
 *
24
 * @see DecryptInterface
25
 */
26
trait DecryptTrait
27
{
28
    private ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $decryptionKeyToken = null;
29
30
    protected function validateSyntax_check_decrypt(): void
31
    {
32
        $token = $this
33
            ->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

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