Passed
Push — master ( 77f2a3...0c35c3 )
by Sebastian
03:02
created

buildIfNotContains()   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 5
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    public function buildIfNotContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, string $containsType='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

108
    public function buildIfNotContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, /** @scrutinizer ignore-unused */ string $containsType='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...
109
    {
110
        return $this->buildIfContains($ifType, $variable, $searchTerms, $caseInsensitive, 'not-contains');
111
    }
112
    
113
    public function buildIfBeginsWith(string $ifType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase
114
    {
115
        return $this->buildIfSearch($ifType, 'begins-with', $variable, $search, $caseInsensitive);
116
    }
117
    
118
    public function buildIfEndsWith(string $ifType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase
119
    {
120
        return $this->buildIfSearch($ifType, 'ends-with', $variable, $search, $caseInsensitive);
121
    }
122
123
    private function buildIfNumeric(string $ifType, string $variable, string $value, string $type) : Mailcode_Commands_IfBase
124
    {
125
        $params = sprintf(
126
            '%1$s "%2$s"',
127
            '$'.ltrim($variable, '$'),
128
            $value
129
        );
130
131
        return $this->buildIf(
132
            $ifType,
133
            $params,
134
            $type
135
        );
136
    }
137
138
    public function buildIfBiggerThan(string $ifType, string $variable, string $value) : Mailcode_Commands_IfBase
139
    {
140
        return $this->buildIfNumeric(
141
            $ifType,
142
            $variable,
143
            $value,
144
            'bigger-than'
145
        );
146
    }
147
148
    public function buildIfSmallerThan(string $ifType, string $variable, string $value) : Mailcode_Commands_IfBase
149
    {
150
        return $this->buildIfNumeric(
151
            $ifType,
152
            $variable,
153
            $value,
154
            'smaller-than'
155
        );
156
    }
157
158
    public function buildIfEquals(string $ifType, string $variable, string $value) : Mailcode_Commands_IfBase
159
    {
160
        return $this->buildIfNumeric(
161
            $ifType,
162
            $variable,
163
            $value,
164
            'equals-number'
165
        );
166
    }
167
168
    private function buildIfSearch(string $ifType, string $subType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase
169
    {
170
        $keyword = ' ';
171
        
172
        if($caseInsensitive)
173
        {
174
            $keyword = ' '.Mailcode_Commands_Keywords::TYPE_INSENSITIVE;
175
        }
176
        
177
        $condition = sprintf(
178
            '%s%s"%s"',
179
            $this->filterVariableName($variable),
180
            $keyword,
181
            $this->filterLiteral($search)
182
        );
183
        
184
        return $this->buildIf($ifType, $condition, $subType);
185
    }
186
    
187
    public function filterLiteral(string $term) : string
188
    {
189
        return str_replace('"', '\"', $term);
190
    }
191
    
192
    public function filterVariableName(string $name) : string
193
    {
194
        $name = preg_replace('/\s/', '', $name);
195
        
196
        return '$'.ltrim($name, '$');
197
    }
198
    
199
    public function checkCommand(Mailcode_Commands_Command $command) : void
200
    {
201
        if($command->isValid())
202
        {
203
            return;
204
        }
205
        
206
        throw new Mailcode_Factory_Exception(
207
            'Invalid command created.',
208
            'Validation message: '.$command->getValidationResult()->getErrorMessage(),
209
            Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED,
210
            null,
211
            $command
212
        );
213
    }
214
    
215
    /**
216
     * Quotes a string literal: adds the quotes, and escapes any quotes already present in it.
217
     *
218
     * @param string $string
219
     * @return string
220
     */
221
    public function quoteString(string $string) : string
222
    {
223
        return '"'.str_replace('"', '\"', $string).'"';
224
    }
225
    
226
    public function exceptionUnexpectedType(string $type, Mailcode_Commands_Command $command) : Mailcode_Factory_Exception
227
    {
228
        return new Mailcode_Factory_Exception(
229
            'Invalid command class type created.',
230
            sprintf('Excepted type [%s], but created class [%s].', $type, get_class($command)),
231
            Mailcode_Factory::ERROR_UNEXPECTED_COMMAND_TYPE,
232
            null,
233
            $command
234
        );
235
    }
236
}
237