Test Failed
Push — master ( 36c5b6...af11e6 )
by Sebastian
04:51
created

removeContentID()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 21
rs 9.8333
1
<?php
2
/**
3
 * File containing the class {@see Mailcode_Commands_Normalizer_ProtectedContent}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Normalizer
7
 * @see Mailcode_Commands_Normalizer_ProtectedContent
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Specialized normalizer for protected content commands.
16
 * Ensures that the content ID parameter is removed before
17
 * normalizing, and converts the inline command to the
18
 * original command including its content.
19
 *
20
 * The flow looks like this:
21
 *
22
 * 1) Original command in the source text, e.g. <code>{code: "Mailcode"}Some text here{code}</code>
23
 * 2) The pre-parser converts this to an inline command, e.g. <code>{code: 1 "Mailcode"}</code>
24
 * 3) The parser instantiates the command from the inline version.
25
 * 4) The normalizer reverts it back to the original command string.
26
 *
27
 * @package Mailcode
28
 * @subpackage Normalizer
29
 * @author Sebastian Mordziol <[email protected]>
30
 *
31
 * @see Mailcode_Traits_Commands_ProtectedContent::getNormalized()
32
 *
33
 * @property Mailcode_Interfaces_Commands_ProtectedContent $command
34
 */
35
class Mailcode_Commands_Normalizer_ProtectedContent extends Mailcode_Commands_Normalizer
36
{
37
    public const ERROR_CONTENT_ID_TOKEN_MISSING = 101601;
38
    public const ERROR_INVALID_COMMAND_INSTANCE = 101602;
39
40
    public function __construct(Mailcode_Commands_Command $command)
41
    {
42
        if(!$command instanceof Mailcode_Interfaces_Commands_ProtectedContent)
43
        {
44
            throw new Mailcode_Exception(
45
                'Invalid command',
46
                sprintf(
47
                    'The specified command is not a protected content command: [%s].',
48
                    get_class($command)
49
                ),
50
                self::ERROR_INVALID_COMMAND_INSTANCE
51
            );
52
        }
53
54
        parent::__construct($command);
55
    }
56
57
    public function normalize() : string
58
    {
59
        return
60
            parent::normalize().
61
            $this->command->getContent().
62
            '{'.$this->command->getName().'}';
63
    }
64
65
    protected function getParamsStatement(Mailcode_Commands_Command $command) : ?Mailcode_Parser_Statement
66
    {
67
        $params = $command->getParams();
68
69
        if($params === null)
70
        {
71
            return null;
72
        }
73
74
        return $this->removeContentID($params);
75
    }
76
77
    /**
78
     * Removes the content ID parameter from the command's
79
     * parameters string, since this must not be included
80
     * in the command's normalized version.
81
     *
82
     * @param Mailcode_Parser_Statement $params
83
     * @return Mailcode_Parser_Statement
84
     * @throws Mailcode_Exception
85
     */
86
    private function removeContentID(Mailcode_Parser_Statement $params) : Mailcode_Parser_Statement
87
    {
88
        $copy = $params->copy();
89
        $info = $copy->getInfo();
90
91
        $contentID = $info->getTokenByIndex(0);
92
93
        if($contentID instanceof Mailcode_Parser_Statement_Tokenizer_Token_Number)
94
        {
95
            $info->removeToken($contentID);
96
            return $copy;
97
        }
98
99
        throw new Mailcode_Exception(
100
            'Invalid content ID token',
101
            sprintf(
102
                'The command [%s] is missing its content ID token in the statement string: [%s].',
103
                $this->command->getName(),
104
                $copy->getStatementString()
105
            ),
106
            self::ERROR_CONTENT_ID_TOKEN_MISSING
107
        );
108
    }
109
}
110