| Total Complexity | 64 |
| Total Lines | 555 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| 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 |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Creates a ShowDate command, used to display date variables and |
||
| 61 | * format the date using date format strings. |
||
| 62 | * |
||
| 63 | * @param string $variableName A variable name, with or without the $ sign prepended. |
||
| 64 | * @param string $formatString A date format string, or empty string for default. |
||
| 65 | * @return Mailcode_Commands_Command_ShowDate |
||
| 66 | */ |
||
| 67 | public static function showDate(string $variableName, string $formatString="") : Mailcode_Commands_Command_ShowDate |
||
| 68 | { |
||
| 69 | $variableName = self::_filterVariableName($variableName); |
||
| 70 | |||
| 71 | $format = ''; |
||
| 72 | if(!empty($formatString)) |
||
| 73 | { |
||
| 74 | $format = sprintf( |
||
| 75 | ' "%s"', |
||
| 76 | $formatString |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
| 81 | 'ShowDate', |
||
| 82 | '', |
||
| 83 | $variableName.$format, |
||
| 84 | sprintf( |
||
| 85 | '{showdate: %s%s}', |
||
| 86 | $variableName, |
||
| 87 | $format |
||
| 88 | ) |
||
| 89 | ); |
||
| 90 | |||
| 91 | self::_checkCommand($cmd); |
||
| 92 | |||
| 93 | if($cmd instanceof Mailcode_Commands_Command_ShowDate) |
||
| 94 | { |
||
| 95 | return $cmd; |
||
| 96 | } |
||
| 97 | |||
| 98 | throw self::_exceptionUnexpectedType('ShowDate', $cmd); |
||
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Creates a ShowSnippet command. |
||
| 104 | * |
||
| 105 | * @param string $snippetName A snippet name, with or without the $ sign prepended. |
||
| 106 | * @return Mailcode_Commands_Command_ShowSnippet |
||
| 107 | */ |
||
| 108 | public static function showSnippet(string $snippetName) : Mailcode_Commands_Command_ShowSnippet |
||
| 109 | { |
||
| 110 | $snippetName = self::_filterVariableName($snippetName); |
||
| 111 | |||
| 112 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
| 113 | 'ShowSnippet', |
||
| 114 | '', |
||
| 115 | $snippetName, |
||
| 116 | '{showsnippet:'.$snippetName.'}' |
||
| 117 | ); |
||
| 118 | |||
| 119 | self::_checkCommand($cmd); |
||
| 120 | |||
| 121 | if($cmd instanceof Mailcode_Commands_Command_ShowSnippet) |
||
| 122 | { |
||
| 123 | return $cmd; |
||
| 124 | } |
||
| 125 | |||
| 126 | throw self::_exceptionUnexpectedType('ShowSnippet', $cmd); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Creates a SetVariable command. |
||
| 131 | * |
||
| 132 | * @param string $variableName A variable name, with or without the $ sign prepended. |
||
| 133 | * @param string $value |
||
| 134 | * @param bool $quoteValue Whether to treat the value as a string literal, and add quotes to it. |
||
| 135 | * @return Mailcode_Commands_Command_SetVariable |
||
| 136 | * @throws Mailcode_Factory_Exception |
||
| 137 | * |
||
| 138 | * @see Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED |
||
| 139 | */ |
||
| 140 | public static function setVar(string $variableName, string $value, bool $quoteValue=true) : Mailcode_Commands_Command_SetVariable |
||
| 141 | { |
||
| 142 | $variableName = self::_filterVariableName($variableName); |
||
| 143 | |||
| 144 | if($quoteValue) |
||
| 145 | { |
||
| 146 | $value = self::_quoteString($value); |
||
| 147 | } |
||
| 148 | |||
| 149 | $params = $variableName.' = '.$value; |
||
| 150 | |||
| 151 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
| 152 | 'SetVariable', |
||
| 153 | '', // type |
||
| 154 | $params, |
||
| 155 | '{setvar: '.$params.'}' |
||
| 156 | ); |
||
| 157 | |||
| 158 | self::_checkCommand($cmd); |
||
| 159 | |||
| 160 | if($cmd instanceof Mailcode_Commands_Command_SetVariable) |
||
| 161 | { |
||
| 162 | return $cmd; |
||
| 163 | } |
||
| 164 | |||
| 165 | throw self::_exceptionUnexpectedType('SetVariable', $cmd); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Like setVar(), but treats the value as a string literal |
||
| 170 | * and automatically adds quotes to it. |
||
| 171 | * |
||
| 172 | * @param string $variableName |
||
| 173 | * @param string $value |
||
| 174 | * @return Mailcode_Commands_Command_SetVariable |
||
| 175 | */ |
||
| 176 | public static function setVarString(string $variableName, string $value) : Mailcode_Commands_Command_SetVariable |
||
| 177 | { |
||
| 178 | return self::setVar($variableName, $value, true); |
||
| 179 | } |
||
| 180 | |||
| 181 | public static function comment(string $comments) : Mailcode_Commands_Command_Comment |
||
| 182 | { |
||
| 183 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
| 184 | 'Comment', |
||
| 185 | '', // type |
||
| 186 | $comments, // params |
||
| 187 | sprintf( |
||
| 188 | '{comment: %s}', |
||
| 189 | $comments |
||
| 190 | ) |
||
| 191 | ); |
||
| 192 | |||
| 193 | self::_checkCommand($cmd); |
||
| 194 | |||
| 195 | if($cmd instanceof Mailcode_Commands_Command_Comment) |
||
| 196 | { |
||
| 197 | return $cmd; |
||
| 198 | } |
||
| 199 | |||
| 200 | throw self::_exceptionUnexpectedType('Comment', $cmd); |
||
| 201 | } |
||
| 202 | |||
| 203 | public static function else() : Mailcode_Commands_Command_Else |
||
| 204 | { |
||
| 205 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
| 206 | 'Else', |
||
| 207 | '', |
||
| 208 | '', |
||
| 209 | '{else}' |
||
| 210 | ); |
||
| 211 | |||
| 212 | self::_checkCommand($cmd); |
||
| 213 | |||
| 214 | if($cmd instanceof Mailcode_Commands_Command_Else) |
||
| 215 | { |
||
| 216 | return $cmd; |
||
| 217 | } |
||
| 218 | |||
| 219 | throw self::_exceptionUnexpectedType('Else', $cmd); |
||
| 220 | } |
||
| 221 | |||
| 222 | public static function end() : Mailcode_Commands_Command_End |
||
| 223 | { |
||
| 224 | $cmd = Mailcode::create()->getCommands()->createCommand( |
||
| 225 | 'End', |
||
| 226 | '', |
||
| 227 | '', |
||
| 228 | '{end}' |
||
| 229 | ); |
||
| 230 | |||
| 231 | self::_checkCommand($cmd); |
||
| 232 | |||
| 233 | if($cmd instanceof Mailcode_Commands_Command_End) |
||
| 234 | { |
||
| 235 | return $cmd; |
||
| 236 | } |
||
| 237 | |||
| 238 | throw self::_exceptionUnexpectedType('End', $cmd); |
||
| 239 | } |
||
| 240 | |||
| 241 | protected static function _buildIf(string $ifType, string $params, string $type='') : Mailcode_Commands_IfBase |
||
| 242 | { |
||
| 243 | $stringType = $type; |
||
| 244 | |||
| 245 | if(!empty($type)) |
||
| 246 | { |
||
| 247 | $stringType = ' '.$type; |
||
| 248 | } |
||
| 249 | |||
| 250 | $command = Mailcode::create()->getCommands()->createCommand( |
||
| 251 | $ifType, |
||
| 252 | $type, |
||
| 253 | $params, |
||
| 254 | sprintf( |
||
| 255 | '{%s%s: %s}', |
||
| 256 | strtolower($ifType), |
||
| 257 | $stringType, |
||
| 258 | $params |
||
| 259 | ) |
||
| 260 | ); |
||
| 261 | |||
| 262 | self::_checkCommand($command); |
||
| 263 | |||
| 264 | if($command instanceof Mailcode_Commands_IfBase) |
||
| 265 | { |
||
| 266 | return $command; |
||
| 267 | } |
||
| 268 | |||
| 269 | throw self::_exceptionUnexpectedType('IfBase', $command); |
||
| 270 | } |
||
| 271 | |||
| 272 | protected static function _buildIfVar(string $ifType, string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_IfBase |
||
| 273 | { |
||
| 274 | if($quoteValue) |
||
| 275 | { |
||
| 276 | $value = self::_quoteString($value); |
||
| 277 | } |
||
| 278 | |||
| 279 | $condition = sprintf( |
||
| 280 | "%s %s %s", |
||
| 281 | self::_filterVariableName($variable), |
||
| 282 | $operand, |
||
| 283 | $value |
||
| 284 | ); |
||
| 285 | |||
| 286 | return self::_buildIf($ifType, $condition, 'variable'); |
||
| 287 | } |
||
| 288 | |||
| 289 | public static function if(string $condition, string $type='') : Mailcode_Commands_Command_If |
||
| 290 | { |
||
| 291 | $command = self::_buildIf('If', $condition, $type); |
||
| 292 | |||
| 293 | if($command instanceof Mailcode_Commands_Command_If) |
||
| 294 | { |
||
| 295 | return $command; |
||
| 296 | } |
||
| 297 | |||
| 298 | throw self::_exceptionUnexpectedType('If', $command); |
||
| 299 | } |
||
| 300 | |||
| 301 | public static function ifVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
| 302 | { |
||
| 303 | $command = self::_buildIfVar('If', $variable, $operand, $value, $quoteValue); |
||
| 304 | |||
| 305 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
| 306 | { |
||
| 307 | return $command; |
||
| 308 | } |
||
| 309 | |||
| 310 | throw self::_exceptionUnexpectedType('IfVar', $command); |
||
| 311 | } |
||
| 312 | |||
| 313 | public static function ifVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_If_Variable |
||
| 314 | { |
||
| 315 | $command = self::_buildIfVar('If', $variable, $operand, $value, true); |
||
| 316 | |||
| 317 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
| 318 | { |
||
| 319 | return $command; |
||
| 320 | } |
||
| 321 | |||
| 322 | throw self::_exceptionUnexpectedType('IfVarString', $command); |
||
| 323 | } |
||
| 324 | |||
| 325 | public static function ifVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
| 326 | { |
||
| 327 | $command = self::_buildIfVar('If', $variable, '==', $value, $quoteValue); |
||
| 328 | |||
| 329 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
| 330 | { |
||
| 331 | return $command; |
||
| 332 | } |
||
| 333 | |||
| 334 | throw self::_exceptionUnexpectedType('IfVarEquals', $command); |
||
| 335 | } |
||
| 336 | |||
| 337 | public static function ifVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If |
||
| 338 | { |
||
| 339 | $command = self::_buildIfVar('If', $variable, '==', $value, true); |
||
| 340 | |||
| 341 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
| 342 | { |
||
| 343 | return $command; |
||
| 344 | } |
||
| 345 | |||
| 346 | throw self::_exceptionUnexpectedType('IfarEqualsString', $command); |
||
| 347 | } |
||
| 348 | |||
| 349 | public static function ifVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
| 350 | { |
||
| 351 | $command = self::_buildIfVar('If', $variable, '!=', $value, $quoteValue); |
||
| 352 | |||
| 353 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
| 354 | { |
||
| 355 | return $command; |
||
| 356 | } |
||
| 357 | |||
| 358 | throw self::_exceptionUnexpectedType('IfVarNotEquals', $command); |
||
| 359 | } |
||
| 360 | |||
| 361 | public static function ifVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If_Variable |
||
| 362 | { |
||
| 363 | $command = self::_buildIfVar('If', $variable, '!=', $value, true); |
||
| 364 | |||
| 365 | if($command instanceof Mailcode_Commands_Command_If_Variable) |
||
| 366 | { |
||
| 367 | return $command; |
||
| 368 | } |
||
| 369 | |||
| 370 | throw self::_exceptionUnexpectedType('IfVarNotEqualsString', $command); |
||
| 371 | } |
||
| 372 | |||
| 373 | public static function elseIf(string $condition, string $type='') : Mailcode_Commands_Command_ElseIf |
||
| 374 | { |
||
| 375 | $command = self::_buildIf('ElseIf', $condition, $type); |
||
| 376 | |||
| 377 | if($command instanceof Mailcode_Commands_Command_ElseIf) |
||
| 378 | { |
||
| 379 | return $command; |
||
| 380 | } |
||
| 381 | |||
| 382 | throw self::_exceptionUnexpectedType('ElseIf', $command); |
||
| 383 | } |
||
| 384 | |||
| 385 | public static function elseIfVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 386 | { |
||
| 387 | $command = self::_buildIfVar('ElseIf', $variable, $operand, $value, $quoteValue); |
||
| 388 | |||
| 389 | if($command instanceof Mailcode_Commands_Command_ElseIf_Variable) |
||
| 390 | { |
||
| 391 | return $command; |
||
| 392 | } |
||
| 393 | |||
| 394 | throw self::_exceptionUnexpectedType('ElseIfVariable', $command); |
||
| 395 | } |
||
| 396 | |||
| 397 | public static function elseIfVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 398 | { |
||
| 399 | $command = self::_buildIfVar('ElseIf', $variable, $operand, $value, true); |
||
| 400 | |||
| 401 | if($command instanceof Mailcode_Commands_Command_ElseIf_Variable) |
||
| 402 | { |
||
| 403 | return $command; |
||
| 404 | } |
||
| 405 | |||
| 406 | throw self::_exceptionUnexpectedType('ElseIfVarString', $command); |
||
| 407 | } |
||
| 408 | |||
| 409 | public static function elseIfVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 410 | { |
||
| 411 | $command = self::_buildIfVar('ElseIf', $variable, '==', $value, $quoteValue); |
||
| 412 | |||
| 413 | if($command instanceof Mailcode_Commands_Command_ElseIf_Variable) |
||
| 414 | { |
||
| 415 | return $command; |
||
| 416 | } |
||
| 417 | |||
| 418 | throw self::_exceptionUnexpectedType('ElseIfVarEquals', $command); |
||
| 419 | } |
||
| 420 | |||
| 421 | public static function elseIfVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 431 | } |
||
| 432 | |||
| 433 | public static function elseIfVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 434 | { |
||
| 435 | $command = self::_buildIfVar('ElseIf', $variable, '!=', $value, $quoteValue); |
||
| 436 | |||
| 437 | if($command instanceof Mailcode_Commands_Command_ElseIf_Variable) |
||
| 438 | { |
||
| 439 | return $command; |
||
| 440 | } |
||
| 441 | |||
| 442 | throw self::_exceptionUnexpectedType('ElseIfVarNotEquals', $command); |
||
| 443 | } |
||
| 444 | |||
| 445 | public static function elseIfVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 446 | { |
||
| 447 | $command = self::_buildIfVar('ElseIf', $variable, '!=', $value, true); |
||
| 448 | |||
| 449 | if($command instanceof Mailcode_Commands_Command_ElseIf_Variable) |
||
| 450 | { |
||
| 451 | return $command; |
||
| 452 | } |
||
| 453 | |||
| 454 | throw self::_exceptionUnexpectedType('ElseIfVarNotEqualsString', $command); |
||
| 455 | } |
||
| 456 | |||
| 457 | public static function ifContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_Contains |
||
| 458 | { |
||
| 459 | $command = self::_buildIfContains('If', $variable, $search, $caseInsensitive); |
||
| 460 | |||
| 461 | if($command instanceof Mailcode_Commands_Command_If_Contains) |
||
| 462 | { |
||
| 463 | return $command; |
||
| 464 | } |
||
| 465 | |||
| 466 | throw self::_exceptionUnexpectedType('ElseIfContains', $command); |
||
| 467 | } |
||
| 468 | |||
| 469 | public static function elseIfContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_Contains |
||
| 470 | { |
||
| 471 | $command = self::_buildIfContains('ElseIf', $variable, $search, $caseInsensitive); |
||
| 472 | |||
| 473 | if($command instanceof Mailcode_Commands_Command_ElseIf_Contains) |
||
| 474 | { |
||
| 475 | return $command; |
||
| 476 | } |
||
| 477 | |||
| 478 | throw self::_exceptionUnexpectedType('ElseIfContains', $command); |
||
| 479 | } |
||
| 480 | |||
| 481 | protected static function _buildIfContains(string $ifType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase |
||
| 498 | } |
||
| 499 | |||
| 500 | protected static function _filterVariableName(string $name) : string |
||
| 501 | { |
||
| 502 | $name = preg_replace('/\s/', '', $name); |
||
| 503 | |||
| 504 | return '$'.ltrim($name, '$'); |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Quotes a string literal: adds the quotes, and escapes any quotes already present in it. |
||
| 509 | * |
||
| 510 | * @param string $string |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | protected static function _quoteString(string $string) : string |
||
| 514 | { |
||
| 515 | return '"'.str_replace('"', '\"', $string).'"'; |
||
| 516 | } |
||
| 517 | |||
| 518 | protected static function _checkCommand(Mailcode_Commands_Command $command) : void |
||
| 519 | { |
||
| 520 | if($command->isValid()) |
||
| 521 | { |
||
| 522 | return; |
||
| 523 | } |
||
| 524 | |||
| 525 | throw new Mailcode_Factory_Exception( |
||
| 526 | 'Invalid command created.', |
||
| 527 | 'Validation message: '.$command->getValidationResult()->getErrorMessage(), |
||
| 528 | self::ERROR_INVALID_COMMAND_CREATED, |
||
| 529 | null, |
||
| 530 | $command |
||
| 531 | ); |
||
| 532 | } |
||
| 533 | |||
| 534 | protected static function _exceptionUnexpectedType(string $type, Mailcode_Commands_Command $command) : Mailcode_Factory_Exception |
||
| 542 | ); |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Creates a renderer instance, which can be used to easily |
||
| 547 | * create and convert commands to strings. |
||
| 548 | * |
||
| 549 | * @return Mailcode_Renderer |
||
| 550 | */ |
||
| 551 | public static function createRenderer() : Mailcode_Renderer |
||
| 552 | { |
||
| 553 | return new Mailcode_Renderer(); |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Creates a printer instance, which works like the renderer, |
||
| 558 | * but outputs the generated strings to standard output. |
||
| 559 | * |
||
| 560 | * @return Mailcode_Printer |
||
| 561 | */ |
||
| 562 | public static function createPrinter() : Mailcode_Printer |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Gets/creates the global instance of the date format info |
||
| 569 | * class, used to handle date formatting aspects. |
||
| 570 | * |
||
| 571 | * @return Mailcode_Date_FormatInfo |
||
| 572 | */ |
||
| 573 | public static function createDateInfo() : Mailcode_Date_FormatInfo |
||
| 574 | { |
||
| 575 | return Mailcode_Date_FormatInfo::getInstance(); |
||
| 576 | } |
||
| 577 | } |
||
| 578 |