Passed
Push — master ( 8ec95d...ba9743 )
by Sebastian
04:36
created

Mailcode_Factory_Instantiator   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 91
dl 0
loc 249
rs 10
c 3
b 0
f 0
wmc 26

20 Methods

Rating   Name   Duplication   Size   Complexity  
A buildIfVar() 0 15 2
A buildIf() 0 29 3
A buildIfEmpty() 0 3 1
A buildIfNotEmpty() 0 3 1
A buildIfContains() 0 17 2
A buildIfNotContains() 0 3 1
A buildIfEquals() 0 7 1
A buildIfListNotContains() 0 3 1
A buildIfBiggerThan() 0 7 1
A buildIfSmallerThan() 0 7 1
A buildIfEndsWith() 0 3 1
A exceptionUnexpectedType() 0 8 1
A buildIfBeginsWith() 0 3 1
A buildIfListContains() 0 3 1
A buildIfSearch() 0 17 2
A filterLiteral() 0 3 1
A checkCommand() 0 13 2
A buildIfNumeric() 0 12 1
A quoteString() 0 3 1
A filterVariableName() 0 5 1
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Factory_Instantiator} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Utilities
7
 * @see Mailcode_Factory_Instantiator
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Factory utility used to create commands.
16
 *
17
 * @package Mailcode
18
 * @subpackage Utilities
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Factory_Instantiator
22
{
23
    public function buildIf(string $ifType, string $params, string $type='') : Mailcode_Commands_IfBase
24
    {
25
        $stringType = $type;
26
        
27
        if(!empty($type))
28
        {
29
            $stringType = ' '.$type;
30
        }
31
        
32
        $command = Mailcode::create()->getCommands()->createCommand(
33
            $ifType,
34
            $type,
35
            $params,
36
            sprintf(
37
                '{%s%s: %s}',
38
                strtolower($ifType),
39
                $stringType,
40
                $params
41
            )
42
        );
43
        
44
        $this->checkCommand($command);
45
        
46
        if($command instanceof Mailcode_Commands_IfBase)
47
        {
48
            return $command;
49
        }
50
        
51
        throw $this->exceptionUnexpectedType('IfBase', $command);
52
    }
53
    
54
    public function buildIfVar(string $ifType, string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_IfBase
55
    {
56
        if($quoteValue)
57
        {
58
            $value = $this->quoteString($value);
59
        }
60
        
61
        $condition = sprintf(
62
            "%s %s %s",
63
            $this->filterVariableName($variable),
64
            $operand,
65
            $value
66
        );
67
        
68
        return $this->buildIf($ifType, $condition, 'variable');
69
    }
70
    
71
    public function buildIfEmpty(string $ifType, string $variable) : Mailcode_Commands_IfBase
72
    {
73
        return $this->buildIf($ifType, $this->filterVariableName($variable), 'empty');
74
    }
75
    
76
    public function buildIfNotEmpty(string $ifType, string $variable) : Mailcode_Commands_IfBase
77
    {
78
        return $this->buildIf($ifType, $this->filterVariableName($variable), 'not-empty');
79
    }
80
81
    /**
82
     * @param string $ifType
83
     * @param string $variable
84
     * @param string[] $searchTerms
85
     * @param bool $caseInsensitive
86
     * @return Mailcode_Commands_IfBase
87
     * @throws Mailcode_Factory_Exception
88
     */
89
    public function buildIfContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, string $containsType='contains') : Mailcode_Commands_IfBase
90
    {
91
        $keyword = ' ';
92
        
93
        if($caseInsensitive)
94
        {
95
            $keyword = ' '.Mailcode_Commands_Keywords::TYPE_INSENSITIVE;
96
        }
97
        
98
        $condition = sprintf(
99
            '%s%s"%s"',
100
            $this->filterVariableName($variable),
101
            $keyword,
102
            implode('" "', array_map(array($this, 'filterLiteral'), $searchTerms))
103
        );
104
        
105
        return $this->buildIf($ifType, $condition, $containsType);
106
    }
107
108
    /**
109
     * @param string $ifType
110
     * @param string $variable
111
     * @param string[] $searchTerms
112
     * @param bool $caseInsensitive
113
     * @return Mailcode_Commands_IfBase
114
     * @throws Mailcode_Factory_Exception
115
     */
116
    public function buildIfNotContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false) : Mailcode_Commands_IfBase
117
    {
118
        return $this->buildIfContains($ifType, $variable, $searchTerms, $caseInsensitive, 'not-contains');
119
    }
120
121
    /**
122
     * @param string $ifType
123
     * @param string $variable
124
     * @param array $searchTerms
125
     * @param bool $caseInsensitive
126
     * @param string $containsType
127
     * @return Mailcode_Commands_IfBase
128
     * @throws Mailcode_Factory_Exception
129
     */
130
    public function buildIfListContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, string $containsType='list-contains') : Mailcode_Commands_IfBase
0 ignored issues
show
Unused Code introduced by
The parameter $containsType is not used and could be removed. ( Ignorable by Annotation )

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

130
    public function buildIfListContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, /** @scrutinizer ignore-unused */ string $containsType='list-contains') : Mailcode_Commands_IfBase

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        return $this->buildIfContains($ifType, $variable, $searchTerms, $caseInsensitive, 'list-contains');
133
    }
134
135
    /**
136
     * @param string $ifType
137
     * @param string $variable
138
     * @param array $searchTerms
139
     * @param bool $caseInsensitive
140
     * @param string $containsType
141
     * @return Mailcode_Commands_IfBase
142
     * @throws Mailcode_Factory_Exception
143
     */
144
    public function buildIfListNotContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, string $containsType='list-contains') : Mailcode_Commands_IfBase
0 ignored issues
show
Unused Code introduced by
The parameter $containsType is not used and could be removed. ( Ignorable by Annotation )

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

144
    public function buildIfListNotContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, /** @scrutinizer ignore-unused */ string $containsType='list-contains') : Mailcode_Commands_IfBase

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
    {
146
        return $this->buildIfContains($ifType, $variable, $searchTerms, $caseInsensitive, 'list-not-contains');
147
    }
148
149
    public function buildIfBeginsWith(string $ifType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase
150
    {
151
        return $this->buildIfSearch($ifType, 'begins-with', $variable, $search, $caseInsensitive);
152
    }
153
    
154
    public function buildIfEndsWith(string $ifType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase
155
    {
156
        return $this->buildIfSearch($ifType, 'ends-with', $variable, $search, $caseInsensitive);
157
    }
158
159
    private function buildIfNumeric(string $ifType, string $variable, string $value, string $type) : Mailcode_Commands_IfBase
160
    {
161
        $params = sprintf(
162
            '%1$s "%2$s"',
163
            '$'.ltrim($variable, '$'),
164
            $value
165
        );
166
167
        return $this->buildIf(
168
            $ifType,
169
            $params,
170
            $type
171
        );
172
    }
173
174
    public function buildIfBiggerThan(string $ifType, string $variable, string $value) : Mailcode_Commands_IfBase
175
    {
176
        return $this->buildIfNumeric(
177
            $ifType,
178
            $variable,
179
            $value,
180
            'bigger-than'
181
        );
182
    }
183
184
    public function buildIfSmallerThan(string $ifType, string $variable, string $value) : Mailcode_Commands_IfBase
185
    {
186
        return $this->buildIfNumeric(
187
            $ifType,
188
            $variable,
189
            $value,
190
            'smaller-than'
191
        );
192
    }
193
194
    public function buildIfEquals(string $ifType, string $variable, string $value) : Mailcode_Commands_IfBase
195
    {
196
        return $this->buildIfNumeric(
197
            $ifType,
198
            $variable,
199
            $value,
200
            'equals-number'
201
        );
202
    }
203
204
    private function buildIfSearch(string $ifType, string $subType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase
205
    {
206
        $keyword = ' ';
207
        
208
        if($caseInsensitive)
209
        {
210
            $keyword = ' '.Mailcode_Commands_Keywords::TYPE_INSENSITIVE;
211
        }
212
        
213
        $condition = sprintf(
214
            '%s%s"%s"',
215
            $this->filterVariableName($variable),
216
            $keyword,
217
            $this->filterLiteral($search)
218
        );
219
        
220
        return $this->buildIf($ifType, $condition, $subType);
221
    }
222
    
223
    public function filterLiteral(string $term) : string
224
    {
225
        return str_replace('"', '\"', $term);
226
    }
227
    
228
    public function filterVariableName(string $name) : string
229
    {
230
        $name = preg_replace('/\s/', '', $name);
231
        
232
        return '$'.ltrim($name, '$');
233
    }
234
    
235
    public function checkCommand(Mailcode_Commands_Command $command) : void
236
    {
237
        if($command->isValid())
238
        {
239
            return;
240
        }
241
        
242
        throw new Mailcode_Factory_Exception(
243
            'Invalid command created.',
244
            'Validation message: '.$command->getValidationResult()->getErrorMessage(),
245
            Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED,
246
            null,
247
            $command
248
        );
249
    }
250
    
251
    /**
252
     * Quotes a string literal: adds the quotes, and escapes any quotes already present in it.
253
     *
254
     * @param string $string
255
     * @return string
256
     */
257
    public function quoteString(string $string) : string
258
    {
259
        return '"'.str_replace('"', '\"', $string).'"';
260
    }
261
    
262
    public function exceptionUnexpectedType(string $type, Mailcode_Commands_Command $command) : Mailcode_Factory_Exception
263
    {
264
        return new Mailcode_Factory_Exception(
265
            'Invalid command class type created.',
266
            sprintf('Excepted type [%s], but created class [%s].', $type, get_class($command)),
267
            Mailcode_Factory::ERROR_UNEXPECTED_COMMAND_TYPE,
268
            null,
269
            $command
270
        );
271
    }
272
}
273