Passed
Push — master ( 742ef2...7822c6 )
by Sebastian
04:11
created

EncodableTrait::hasActiveEncodings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * File containing the trait {@see \Mailcode\Traits\Commands\EncodableTrait}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see \Mailcode\Traits\Commands\EncodableTrait
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Traits\Commands;
13
14
use Mailcode\Interfaces\Commands\EncodableInterface;
15
use Mailcode\Interfaces\Commands\Validation\IDNDecodeInterface;
16
use Mailcode\Interfaces\Commands\Validation\IDNEncodeInterface;
17
use Mailcode\Mailcode_Commands_Keywords;
18
use Mailcode\Mailcode_Exception;
19
use Mailcode\Mailcode_Interfaces_Commands_Validation_URLDecode;
20
use Mailcode\Mailcode_Interfaces_Commands_Validation_URLEncode;
21
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token;
22
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_Keyword;
23
24
/**
25
 * Trait used to implement the encodable interface.
26
 *
27
 * @package Mailcode
28
 * @subpackage Commands
29
 * @author Sebastian Mordziol <[email protected]>
30
 *
31
 * @see EncodableInterface
32
 */
33
trait EncodableTrait
34
{
35
    public function supportsEncoding(string $keyword) : bool
36
    {
37
        return in_array($keyword, $this->getSupportedEncodings(), true);
38
    }
39
40
    public function isEncodingEnabled(string $keyword) : bool
41
    {
42
        return $this->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

42
        return $this->/** @scrutinizer ignore-call */ requireParams()
Loading history...
43
            ->getInfo()
44
            ->hasKeyword($keyword);
45
    }
46
47
    /**
48
     * @param string $keyword
49
     * @param bool $enabled
50
     * @return $this
51
     * @throws Mailcode_Exception
52
     */
53
    public function setEncodingEnabled(string $keyword, bool $enabled) : self
54
    {
55
        if(!$this->supportsEncoding($keyword))
56
        {
57
            throw new Mailcode_Exception(
58
                'Cannot set encoding status, command does not support target encoding.',
59
                sprintf(
60
                    'Tried setting the encoding [%s], but the command only supports the following encodings: [%s].',
61
                    $keyword,
62
                    implode(', ', $this->getSupportedEncodings())
63
                ),
64
                EncodableInterface::ERROR_UNSUPPORTED_ENCODING
65
            );
66
        }
67
68
        $this->requireParams()
69
            ->getInfo()
70
            ->setKeywordEnabled($keyword, $enabled);
71
72
        return $this;
73
    }
74
75
    public function hasActiveEncodings() : bool
76
    {
77
        $list = $this->getActiveEncodings();
78
79
        return !empty($list);
80
    }
81
82
    public function getActiveEncodings() : array
83
    {
84
        $keywords = $this->requireParams()
85
            ->getInfo()
86
            ->getKeywords();
87
88
        $result = array();
89
90
        // We are using the keywords list from the command's parameters
91
        // here, so they are in the exact order in which they were specified.
92
        foreach ($keywords as $keyword)
93
        {
94
            $name = $keyword->getKeyword();
95
96
            if($this->supportsEncoding($name) && $this->isEncodingEnabled($name))
97
            {
98
                $result[] = $name;
99
            }
100
        }
101
102
        return $result;
103
    }
104
105
    public function getSupportedEncodings() : array
106
    {
107
        $encodings = array();
108
109
        if($this instanceof Mailcode_Interfaces_Commands_Validation_URLEncode)
110
        {
111
            $encodings[] = Mailcode_Commands_Keywords::TYPE_URLENCODE;
112
        }
113
114
        if($this instanceof Mailcode_Interfaces_Commands_Validation_URLDecode)
115
        {
116
            $encodings[] = Mailcode_Commands_Keywords::TYPE_URLDECODE;
117
        }
118
119
        if($this instanceof IDNEncodeInterface)
120
        {
121
            $encodings[] = Mailcode_Commands_Keywords::TYPE_IDN_ENCODE;
122
        }
123
124
        if($this instanceof IDNDecodeInterface)
125
        {
126
            $encodings[] = Mailcode_Commands_Keywords::TYPE_IDN_DECODE;
127
        }
128
129
        sort($encodings);
130
131
        return $encodings;
132
    }
133
134
    public function getEncodingToken(string $keyword) : ?Mailcode_Parser_Statement_Tokenizer_Token_Keyword
135
    {
136
        return $this->requireParams()
137
            ->getInfo()
138
            ->getKeywordsCollection()
139
            ->getByName($keyword);
140
    }
141
}
142