Mailcode_Factory_CommandSets_Set_Misc   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 81
c 4
b 0
f 0
dl 0
loc 177
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A for() 0 37 4
A mono() 0 32 4
A code() 0 26 2
A break() 0 16 2
A comment() 0 19 2
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Factory_CommandSets_Set_Misc} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Factory
7
 * @see Mailcode_Factory_CommandSets_Set_Misc
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use Mailcode\Interfaces\Commands\Validation\BreakAtInterface;
15
use Mailcode\Parser\PreParser;
16
17
/**
18
 * Factory utility used to create commands.
19
 *
20
 * @package Mailcode
21
 * @subpackage Factory
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class Mailcode_Factory_CommandSets_Set_Misc extends Mailcode_Factory_CommandSets_Set
25
{
26
    /**
27
     * @param string $comments Quoted or unquoted string.
28
     * @return Mailcode_Commands_Command_Comment
29
     * @throws Mailcode_Exception
30
     * @throws Mailcode_Factory_Exception
31
     */
32
    public function comment(string $comments): Mailcode_Commands_Command_Comment
33
    {
34
        $cmd = $this->commands->createCommand(
35
            'Comment',
36
            '', // type
37
            $this->instantiator->quoteString($comments),
38
            sprintf(
39
                '{comment: "%s"}',
40
                $this->instantiator->quoteString($comments)
41
            )
42
        );
43
44
        $this->instantiator->checkCommand($cmd);
45
46
        if ($cmd instanceof Mailcode_Commands_Command_Comment) {
47
            return $cmd;
48
        }
49
50
        throw $this->instantiator->exceptionUnexpectedType('Comment', $cmd);
51
    }
52
53
    /**
54
     * Creates a for loop command.
55
     *
56
     * @param string $sourceVariable
57
     * @param string $loopVariable
58
     * @param Mailcode_Variables_Variable|string|int|NULL $breakAtValue
59
     * @return Mailcode_Commands_Command_For
60
     * @throws Mailcode_Exception
61
     * @throws Mailcode_Factory_Exception
62
     */
63
    public function for(string $sourceVariable, string $loopVariable, $breakAtValue = ''): Mailcode_Commands_Command_For
64
    {
65
        $sourceVariable = dollarize($sourceVariable);
66
        $loopVariable = dollarize($loopVariable);
67
68
        if($breakAtValue instanceof Mailcode_Variables_Variable) {
69
            $breakAtValue = $breakAtValue->getFullName();
70
        }
71
72
        if (!empty($breakAtValue)) {
73
            $breakAtValue = sprintf(' %s=%s', BreakAtInterface::PARAMETER_NAME, $breakAtValue);
0 ignored issues
show
Bug introduced by
It seems like $breakAtValue can also be of type Mailcode\Mailcode_Variables_Variable; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

73
            $breakAtValue = sprintf(' %s=%s', BreakAtInterface::PARAMETER_NAME, /** @scrutinizer ignore-type */ $breakAtValue);
Loading history...
74
        }
75
76
        $cmd = $this->commands->createCommand(
77
            'For',
78
            '',
79
            sprintf(
80
                '%s in: %s%s',
81
                $loopVariable,
82
                $sourceVariable,
83
                $breakAtValue
84
            ),
85
            sprintf(
86
                '{for: %s in: %s%s}',
87
                $loopVariable,
88
                $sourceVariable,
89
                $breakAtValue
90
            )
91
        );
92
93
        $this->instantiator->checkCommand($cmd);
94
95
        if ($cmd instanceof Mailcode_Commands_Command_For) {
96
            return $cmd;
97
        }
98
99
        throw $this->instantiator->exceptionUnexpectedType('For', $cmd);
100
    }
101
102
    /**
103
     * Creates a break command, which can be used to break out of
104
     * a loop command. Using this outside of a loop will trigger
105
     * a validation error.
106
     *
107
     * @return Mailcode_Commands_Command_Break
108
     * @throws Mailcode_Exception
109
     * @throws Mailcode_Factory_Exception
110
     */
111
    public function break(): Mailcode_Commands_Command_Break
112
    {
113
        $cmd = $this->commands->createCommand(
114
            'Break',
115
            '',
116
            '',
117
            '{break}'
118
        );
119
120
        $this->instantiator->checkCommand($cmd);
121
122
        if ($cmd instanceof Mailcode_Commands_Command_Break) {
123
            return $cmd;
124
        }
125
126
        throw $this->instantiator->exceptionUnexpectedType('Break', $cmd);
127
    }
128
129
    /**
130
     * Creates a preprocessor command to format text as preformatted.
131
     *
132
     * NOTE: Requires the Mailcode text to be preprocessed using the
133
     * preprocessor. See the documentation on how to use it.
134
     *
135
     * @param bool $multiline
136
     * @param string[] $classes
137
     * @return Mailcode_Commands_Command_Mono
138
     * @throws Mailcode_Exception
139
     * @throws Mailcode_Factory_Exception
140
     */
141
    public function mono(bool $multiline = false, array $classes = array()): Mailcode_Commands_Command_Mono
142
    {
143
        $params = '';
144
        $source = '{code';
145
146
        if ($multiline) {
147
            $params = 'multiline:';
148
            $source = '{code: multiline:';
149
        }
150
151
        if (!empty($classes)) {
152
            $classString = sprintf('"%s"', implode(' ', $classes));
153
            $params .= $classString;
154
            $source .= $classString;
155
        }
156
157
        $source .= '}';
158
159
        $cmd = $this->commands->createCommand(
160
            'Mono',
161
            '',
162
            $params,
163
            $source
164
        );
165
166
        $this->instantiator->checkCommand($cmd);
167
168
        if ($cmd instanceof Mailcode_Commands_Command_Mono) {
169
            return $cmd;
170
        }
171
172
        throw $this->instantiator->exceptionUnexpectedType('Mono', $cmd);
173
    }
174
175
    public function code(string $language, string $content): Mailcode_Commands_Command_Code
176
    {
177
        $contentID = PreParser::storeContent($content);
178
179
        $cmd = $this->commands->createCommand(
180
            'Code',
181
            '',
182
            sprintf(
183
                '%s "%s"',
184
                $contentID,
185
                $language
186
            ),
187
            sprintf(
188
                '{code: %s "%s"}',
189
                $contentID,
190
                $language
191
            )
192
        );
193
194
        $this->instantiator->checkCommand($cmd);
195
196
        if ($cmd instanceof Mailcode_Commands_Command_Code) {
197
            return $cmd;
198
        }
199
200
        throw $this->instantiator->exceptionUnexpectedType('Code', $cmd);
201
    }
202
}
203