Total Complexity | 60 |
Total Lines | 501 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 1 |
Complex classes like Mailcode_Factory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mailcode_Factory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Mailcode_Factory |
||
22 | { |
||
23 | const ERROR_INVALID_COMMAND_CREATED = 50001; |
||
24 | |||
25 | const ERROR_UNEXPECTED_COMMAND_TYPE = 50002; |
||
26 | |||
27 | /** |
||
28 | * @var Mailcode_Renderer |
||
29 | */ |
||
30 | protected static $renderer; |
||
31 | |||
32 | /** |
||
33 | * Creates a ShowVariable command. |
||
34 | * |
||
35 | * @param string $variableName A variable name, with or without the $ sign prepended. |
||
36 | * @return Mailcode_Commands_Command_ShowVariable |
||
37 | */ |
||
38 | public static function showVar(string $variableName) : Mailcode_Commands_Command_ShowVariable |
||
39 | { |
||
40 | $variableName = self::_filterVariableName($variableName); |
||
41 | |||
42 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
43 | 'ShowVariable', |
||
44 | '', |
||
45 | $variableName, |
||
46 | '{showvar:'.$variableName.'}' |
||
47 | ); |
||
48 | |||
49 | self::_checkCommand($cmd); |
||
50 | |||
51 | if($cmd instanceof Mailcode_Commands_Command_ShowVariable) |
||
52 | { |
||
53 | return $cmd; |
||
54 | } |
||
55 | |||
56 | throw self::_exceptionUnexpectedType('ShowVariable', $cmd); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Creates a ShowSnippet command. |
||
61 | * |
||
62 | * @param string $snippetName A snippet name, with or without the $ sign prepended. |
||
63 | * @return Mailcode_Commands_Command_ShowSnippet |
||
64 | */ |
||
65 | public static function showSnippet(string $snippetName) : Mailcode_Commands_Command_ShowSnippet |
||
66 | { |
||
67 | $snippetName = self::_filterVariableName($snippetName); |
||
68 | |||
69 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
70 | 'ShowSnippet', |
||
71 | '', |
||
72 | $snippetName, |
||
73 | '{showsnippet:'.$snippetName.'}' |
||
74 | ); |
||
75 | |||
76 | self::_checkCommand($cmd); |
||
77 | |||
78 | if($cmd instanceof Mailcode_Commands_Command_ShowSnippet) |
||
79 | { |
||
80 | return $cmd; |
||
81 | } |
||
82 | |||
83 | throw self::_exceptionUnexpectedType('ShowSnippet', $cmd); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Creates a SetVariable command. |
||
88 | * |
||
89 | * @param string $variableName A variable name, with or without the $ sign prepended. |
||
90 | * @param string $value |
||
91 | * @param bool $quoteValue Whether to treat the value as a string literal, and add quotes to it. |
||
92 | * @return Mailcode_Commands_Command_SetVariable |
||
93 | * @throws Mailcode_Factory_Exception |
||
94 | * |
||
95 | * @see Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED |
||
96 | */ |
||
97 | public static function setVar(string $variableName, string $value, bool $quoteValue=true) : Mailcode_Commands_Command_SetVariable |
||
98 | { |
||
99 | $variableName = self::_filterVariableName($variableName); |
||
100 | |||
101 | if($quoteValue) |
||
102 | { |
||
103 | $value = self::_quoteString($value); |
||
104 | } |
||
105 | |||
106 | $params = $variableName.' = '.$value; |
||
107 | |||
108 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
109 | 'SetVariable', |
||
110 | '', // type |
||
111 | $params, |
||
112 | '{setvar: '.$params.'}' |
||
113 | ); |
||
114 | |||
115 | self::_checkCommand($cmd); |
||
116 | |||
117 | if($cmd instanceof Mailcode_Commands_Command_SetVariable) |
||
118 | { |
||
119 | return $cmd; |
||
120 | } |
||
121 | |||
122 | throw self::_exceptionUnexpectedType('SetVariable', $cmd); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Like setVar(), but treats the value as a string literal |
||
127 | * and automatically adds quotes to it. |
||
128 | * |
||
129 | * @param string $variableName |
||
130 | * @param string $value |
||
131 | * @return Mailcode_Commands_Command_SetVariable |
||
132 | */ |
||
133 | public static function setVarString(string $variableName, string $value) : Mailcode_Commands_Command_SetVariable |
||
134 | { |
||
135 | return self::setVar($variableName, $value, true); |
||
136 | } |
||
137 | |||
138 | public static function comment(string $comments) : Mailcode_Commands_Command_Comment |
||
139 | { |
||
140 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
141 | 'Comment', |
||
142 | '', // type |
||
143 | $comments, // params |
||
144 | sprintf( |
||
145 | '{comment: %s}', |
||
146 | $comments |
||
147 | ) |
||
148 | ); |
||
149 | |||
150 | self::_checkCommand($cmd); |
||
151 | |||
152 | if($cmd instanceof Mailcode_Commands_Command_Comment) |
||
153 | { |
||
154 | return $cmd; |
||
155 | } |
||
156 | |||
157 | throw self::_exceptionUnexpectedType('Comment', $cmd); |
||
158 | } |
||
159 | |||
160 | public static function else() : Mailcode_Commands_Command_Else |
||
161 | { |
||
162 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
163 | 'Else', |
||
164 | '', |
||
165 | '', |
||
166 | '{else}' |
||
167 | ); |
||
168 | |||
169 | self::_checkCommand($cmd); |
||
170 | |||
171 | if($cmd instanceof Mailcode_Commands_Command_Else) |
||
172 | { |
||
173 | return $cmd; |
||
174 | } |
||
175 | |||
176 | throw self::_exceptionUnexpectedType('Else', $cmd); |
||
177 | } |
||
178 | |||
179 | public static function end() : Mailcode_Commands_Command_End |
||
180 | { |
||
181 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
182 | 'End', |
||
183 | '', |
||
184 | '', |
||
185 | '{end}' |
||
186 | ); |
||
187 | |||
188 | self::_checkCommand($cmd); |
||
189 | |||
190 | if($cmd instanceof Mailcode_Commands_Command_End) |
||
191 | { |
||
192 | return $cmd; |
||
193 | } |
||
194 | |||
195 | throw self::_exceptionUnexpectedType('End', $cmd); |
||
196 | } |
||
197 | |||
198 | protected static function _buildIf(string $ifType, string $params, string $type='') : Mailcode_Commands_IfBase |
||
199 | { |
||
200 | $stringType = $type; |
||
201 | |||
202 | if(!empty($type)) |
||
203 | { |
||
204 | $stringType = ' '.$type; |
||
205 | } |
||
206 | |||
207 | $command = Mailcode::create()->getCommands()->createCommand( |
||
208 | $ifType, |
||
209 | $type, |
||
210 | $params, |
||
211 | sprintf( |
||
212 | '{%s%s: %s}', |
||
213 | strtolower($ifType), |
||
214 | $stringType, |
||
215 | $params |
||
216 | ) |
||
217 | ); |
||
218 | |||
219 | self::_checkCommand($command); |
||
220 | |||
221 | if($command instanceof Mailcode_Commands_IfBase) |
||
222 | { |
||
223 | return $command; |
||
224 | } |
||
225 | |||
226 | throw self::_exceptionUnexpectedType('IfBase', $command); |
||
227 | } |
||
228 | |||
229 | protected static function _buildIfVar(string $ifType, string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_IfBase |
||
230 | { |
||
231 | if($quoteValue) |
||
232 | { |
||
233 | $value = self::_quoteString($value); |
||
234 | } |
||
235 | |||
236 | $condition = sprintf( |
||
237 | "%s %s %s", |
||
238 | self::_filterVariableName($variable), |
||
239 | $operand, |
||
240 | $value |
||
241 | ); |
||
242 | |||
243 | return self::_buildIf($ifType, $condition, 'variable'); |
||
244 | } |
||
245 | |||
246 | public static function if(string $condition, string $type='') : Mailcode_Commands_Command_If |
||
247 | { |
||
248 | $command = self::_buildIf('If', $condition, $type); |
||
249 | |||
250 | if($command instanceof Mailcode_Commands_Command_If) |
||
251 | { |
||
252 | return $command; |
||
253 | } |
||
254 | |||
255 | throw self::_exceptionUnexpectedType('If', $command); |
||
256 | } |
||
257 | |||
258 | public static function ifVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
259 | { |
||
260 | $command = self::_buildIfVar('If', $variable, $operand, $value, $quoteValue); |
||
261 | |||
262 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
263 | { |
||
264 | return $command; |
||
265 | } |
||
266 | |||
267 | throw self::_exceptionUnexpectedType('IfVar', $command); |
||
268 | } |
||
269 | |||
270 | public static function ifVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_If_Variable |
||
271 | { |
||
272 | $command = self::_buildIfVar('If', $variable, $operand, $value, true); |
||
273 | |||
274 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
275 | { |
||
276 | return $command; |
||
277 | } |
||
278 | |||
279 | throw self::_exceptionUnexpectedType('IfVarString', $command); |
||
280 | } |
||
281 | |||
282 | public static function ifVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
283 | { |
||
284 | $command = self::_buildIfVar('If', $variable, '==', $value, $quoteValue); |
||
285 | |||
286 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
287 | { |
||
288 | return $command; |
||
289 | } |
||
290 | |||
291 | throw self::_exceptionUnexpectedType('IfVarEquals', $command); |
||
292 | } |
||
293 | |||
294 | public static function ifVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If |
||
295 | { |
||
296 | $command = self::_buildIfVar('If', $variable, '==', $value, true); |
||
297 | |||
298 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
299 | { |
||
300 | return $command; |
||
301 | } |
||
302 | |||
303 | throw self::_exceptionUnexpectedType('IfarEqualsString', $command); |
||
304 | } |
||
305 | |||
306 | public static function ifVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
307 | { |
||
308 | $command = self::_buildIfVar('If', $variable, '!=', $value, $quoteValue); |
||
309 | |||
310 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
311 | { |
||
312 | return $command; |
||
313 | } |
||
314 | |||
315 | throw self::_exceptionUnexpectedType('IfVarNotEquals', $command); |
||
316 | } |
||
317 | |||
318 | public static function ifVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If_Variable |
||
319 | { |
||
320 | $command = self::_buildIfVar('If', $variable, '!=', $value, true); |
||
321 | |||
322 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
323 | { |
||
324 | return $command; |
||
325 | } |
||
326 | |||
327 | throw self::_exceptionUnexpectedType('IfVarNotEqualsString', $command); |
||
328 | } |
||
329 | |||
330 | public static function elseIf(string $condition, string $type='') : Mailcode_Commands_Command_ElseIf |
||
331 | { |
||
332 | $command = self::_buildIf('ElseIf', $condition, $type); |
||
333 | |||
334 | if($command instanceof Mailcode_Commands_Command_ElseIf) |
||
335 | { |
||
336 | return $command; |
||
337 | } |
||
338 | |||
339 | throw self::_exceptionUnexpectedType('ElseIf', $command); |
||
340 | } |
||
341 | |||
342 | public static function elseIfVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
343 | { |
||
344 | $command = self::_buildIfVar('ElseIf', $variable, $operand, $value, $quoteValue); |
||
345 | |||
346 | if($command instanceof Mailcode_Commands_Command_ElseIf_Variable) |
||
347 | { |
||
348 | return $command; |
||
349 | } |
||
350 | |||
351 | throw self::_exceptionUnexpectedType('ElseIfVariable', $command); |
||
352 | } |
||
353 | |||
354 | public static function elseIfVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
355 | { |
||
356 | $command = self::_buildIfVar('ElseIf', $variable, $operand, $value, true); |
||
357 | |||
358 | if($command instanceof Mailcode_Commands_Command_ElseIf_Variable) |
||
359 | { |
||
360 | return $command; |
||
361 | } |
||
362 | |||
363 | throw self::_exceptionUnexpectedType('ElseIfVarString', $command); |
||
364 | } |
||
365 | |||
366 | public static function elseIfVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
367 | { |
||
368 | $command = self::_buildIfVar('ElseIf', $variable, '==', $value, $quoteValue); |
||
369 | |||
370 | if($command instanceof Mailcode_Commands_Command_ElseIf_Variable) |
||
371 | { |
||
372 | return $command; |
||
373 | } |
||
374 | |||
375 | throw self::_exceptionUnexpectedType('ElseIfVarEquals', $command); |
||
376 | } |
||
377 | |||
378 | public static function elseIfVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
388 | } |
||
389 | |||
390 | public static function elseIfVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
391 | { |
||
392 | $command = self::_buildIfVar('ElseIf', $variable, '!=', $value, $quoteValue); |
||
393 | |||
394 | if($command instanceof Mailcode_Commands_Command_ElseIf_Variable) |
||
395 | { |
||
396 | return $command; |
||
397 | } |
||
398 | |||
399 | throw self::_exceptionUnexpectedType('ElseIfVarNotEquals', $command); |
||
400 | } |
||
401 | |||
402 | public static function elseIfVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
403 | { |
||
404 | $command = self::_buildIfVar('ElseIf', $variable, '!=', $value, true); |
||
405 | |||
406 | if($command instanceof Mailcode_Commands_Command_ElseIf_Variable) |
||
407 | { |
||
408 | return $command; |
||
409 | } |
||
410 | |||
411 | throw self::_exceptionUnexpectedType('ElseIfVarNotEqualsString', $command); |
||
412 | } |
||
413 | |||
414 | public static function ifContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_Contains |
||
415 | { |
||
416 | $command = self::_buildIfContains('If', $variable, $search, $caseInsensitive); |
||
417 | |||
418 | if($command instanceof Mailcode_Commands_Command_If_Contains) |
||
419 | { |
||
420 | return $command; |
||
421 | } |
||
422 | |||
423 | throw self::_exceptionUnexpectedType('ElseIfContains', $command); |
||
424 | } |
||
425 | |||
426 | public static function elseIfContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_Contains |
||
427 | { |
||
428 | $command = self::_buildIfContains('ElseIf', $variable, $search, $caseInsensitive); |
||
429 | |||
430 | if($command instanceof Mailcode_Commands_Command_ElseIf_Contains) |
||
431 | { |
||
432 | return $command; |
||
433 | } |
||
434 | |||
435 | throw self::_exceptionUnexpectedType('ElseIfContains', $command); |
||
436 | } |
||
437 | |||
438 | protected static function _buildIfContains(string $ifType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase |
||
455 | } |
||
456 | |||
457 | protected static function _filterVariableName(string $name) : string |
||
458 | { |
||
459 | $name = preg_replace('/\s/', '', $name); |
||
460 | |||
461 | return '$'.ltrim($name, '$'); |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Quotes a string literal: adds the quotes, and escapes any quotes already present in it. |
||
466 | * |
||
467 | * @param string $string |
||
468 | * @return string |
||
469 | */ |
||
470 | protected static function _quoteString(string $string) : string |
||
471 | { |
||
472 | return '"'.str_replace('"', '\"', $string).'"'; |
||
473 | } |
||
474 | |||
475 | protected static function _checkCommand(Mailcode_Commands_Command $command) : void |
||
476 | { |
||
477 | if($command->isValid()) |
||
478 | { |
||
479 | return; |
||
480 | } |
||
481 | |||
482 | throw new Mailcode_Factory_Exception( |
||
483 | 'Invalid command created.', |
||
484 | 'Validation message: '.$command->getValidationResult()->getErrorMessage(), |
||
485 | self::ERROR_INVALID_COMMAND_CREATED, |
||
486 | null, |
||
487 | $command |
||
488 | ); |
||
489 | } |
||
490 | |||
491 | protected static function _exceptionUnexpectedType(string $type, Mailcode_Commands_Command $command) : Mailcode_Factory_Exception |
||
499 | ); |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * Creates a renderer instance, which can be used to easily |
||
504 | * create and convert commands to strings. |
||
505 | * |
||
506 | * @return Mailcode_Renderer |
||
507 | */ |
||
508 | public static function createRenderer() : Mailcode_Renderer |
||
509 | { |
||
510 | return new Mailcode_Renderer(); |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Creates a printer instance, which works like the renderer, |
||
515 | * but outputs the generated strings to standard output. |
||
516 | * |
||
517 | * @return Mailcode_Printer |
||
518 | */ |
||
519 | public static function createPrinter() : Mailcode_Printer |
||
522 | } |
||
523 | } |
||
524 |