| Total Complexity | 47 |
| Total Lines | 418 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| 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 |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Creates a ShowSnippet command. |
||
| 55 | * |
||
| 56 | * @param string $snippetName A snippet name, with or without the $ sign prepended. |
||
| 57 | * @return Mailcode_Commands_Command_ShowSnippet |
||
| 58 | */ |
||
| 59 | public static function showSnippet(string $snippetName) : Mailcode_Commands_Command_ShowSnippet |
||
| 60 | { |
||
| 61 | $snippetName = self::_filterVariableName($snippetName); |
||
| 62 | |||
| 63 | $cmd = new Mailcode_Commands_Command_ShowSnippet( |
||
| 64 | '', |
||
| 65 | $snippetName, |
||
| 66 | '{showsnippet:'.$snippetName.'}' |
||
| 67 | ); |
||
| 68 | |||
| 69 | self::_checkCommand($cmd); |
||
| 70 | |||
| 71 | return $cmd; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Creates a SetVariable command. |
||
| 76 | * |
||
| 77 | * @param string $variableName A variable name, with or without the $ sign prepended. |
||
| 78 | * @param string $value |
||
| 79 | * @param bool $quoteValue Whether to treat the value as a string literal, and add quotes to it. |
||
| 80 | * @return Mailcode_Commands_Command_SetVariable |
||
| 81 | * @throws Mailcode_Factory_Exception |
||
| 82 | * |
||
| 83 | * @see Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED |
||
| 84 | */ |
||
| 85 | public static function setVar(string $variableName, string $value, bool $quoteValue=true) : Mailcode_Commands_Command_SetVariable |
||
| 86 | { |
||
| 87 | $variableName = self::_filterVariableName($variableName); |
||
| 88 | |||
| 89 | if($quoteValue) |
||
| 90 | { |
||
| 91 | $value = self::_quoteString($value); |
||
| 92 | } |
||
| 93 | |||
| 94 | $params = $variableName.' = '.$value; |
||
| 95 | |||
| 96 | $cmd = new Mailcode_Commands_Command_SetVariable( |
||
| 97 | '', // type |
||
| 98 | $params, |
||
| 99 | '{setvar: '.$params.'}' |
||
| 100 | ); |
||
| 101 | |||
| 102 | self::_checkCommand($cmd); |
||
| 103 | |||
| 104 | return $cmd; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Like setVar(), but treats the value as a string literal |
||
| 109 | * and automatically adds quotes to it. |
||
| 110 | * |
||
| 111 | * @param string $variableName |
||
| 112 | * @param string $value |
||
| 113 | * @return Mailcode_Commands_Command_SetVariable |
||
| 114 | */ |
||
| 115 | public static function setVarString(string $variableName, string $value) : Mailcode_Commands_Command_SetVariable |
||
| 116 | { |
||
| 117 | return self::setVar($variableName, $value, true); |
||
| 118 | } |
||
| 119 | |||
| 120 | public static function comment(string $comments) : Mailcode_Commands_Command_Comment |
||
| 121 | { |
||
| 122 | $cmd = new Mailcode_Commands_Command_Comment( |
||
| 123 | '', // type, |
||
| 124 | $comments, // params, |
||
| 125 | sprintf( |
||
| 126 | '{comment: %s}', |
||
| 127 | $comments |
||
| 128 | ) |
||
| 129 | ); |
||
| 130 | |||
| 131 | self::_checkCommand($cmd); |
||
| 132 | |||
| 133 | return $cmd; |
||
| 134 | } |
||
| 135 | |||
| 136 | public static function else() : Mailcode_Commands_Command_Else |
||
| 137 | { |
||
| 138 | $cmd = new Mailcode_Commands_Command_Else( |
||
| 139 | '', // type, |
||
| 140 | '', // params, |
||
| 141 | '{else}' |
||
| 142 | ); |
||
| 143 | |||
| 144 | self::_checkCommand($cmd); |
||
| 145 | |||
| 146 | return $cmd; |
||
| 147 | } |
||
| 148 | |||
| 149 | public static function end() : Mailcode_Commands_Command_End |
||
| 150 | { |
||
| 151 | $cmd = new Mailcode_Commands_Command_End( |
||
| 152 | '', // type, |
||
| 153 | '', // params, |
||
| 154 | '{end}' |
||
| 155 | ); |
||
| 156 | |||
| 157 | self::_checkCommand($cmd); |
||
| 158 | |||
| 159 | return $cmd; |
||
| 160 | } |
||
| 161 | |||
| 162 | protected static function _buildIf(string $cmd, string $condition, string $type='') : Mailcode_Commands_Command |
||
| 163 | { |
||
| 164 | $stringType = $type; |
||
| 165 | |||
| 166 | if(!empty($type)) |
||
| 167 | { |
||
| 168 | $stringType = ' '.$type; |
||
| 169 | } |
||
| 170 | |||
| 171 | $class = '\Mailcode\Mailcode_Commands_Command_'.$cmd; |
||
| 172 | |||
| 173 | $cmd = new $class( |
||
| 174 | $type, // type, |
||
| 175 | $condition, // params, |
||
| 176 | sprintf( |
||
| 177 | '{%s%s: %s}', |
||
| 178 | strtolower($cmd), |
||
| 179 | $stringType, |
||
| 180 | $condition |
||
| 181 | ) |
||
| 182 | ); |
||
| 183 | |||
| 184 | self::_checkCommand($cmd); |
||
| 185 | |||
| 186 | return $cmd; |
||
| 187 | } |
||
| 188 | |||
| 189 | protected static function _buildIfVar(string $cmd, string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command |
||
| 190 | { |
||
| 191 | if($quoteValue) |
||
| 192 | { |
||
| 193 | $value = self::_quoteString($value); |
||
| 194 | } |
||
| 195 | |||
| 196 | $condition = sprintf( |
||
| 197 | "%s %s %s", |
||
| 198 | self::_filterVariableName($variable), |
||
| 199 | $operand, |
||
| 200 | $value |
||
| 201 | ); |
||
| 202 | |||
| 203 | return self::_buildIf($cmd, $condition, 'variable'); |
||
| 204 | } |
||
| 205 | |||
| 206 | public static function if(string $condition, string $type='') : Mailcode_Commands_Command_If |
||
| 207 | { |
||
| 208 | $cmd = self::_buildIf('If', $condition, $type); |
||
| 209 | |||
| 210 | if($cmd instanceof Mailcode_Commands_Command_If) |
||
| 211 | { |
||
| 212 | return $cmd; |
||
| 213 | } |
||
| 214 | |||
| 215 | throw self::_exceptionUnexpectedType($cmd); |
||
| 216 | } |
||
| 217 | |||
| 218 | public static function ifVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If |
||
| 219 | { |
||
| 220 | $cmd = self::_buildIfVar('If', $variable, $operand, $value, $quoteValue); |
||
| 221 | |||
| 222 | if($cmd instanceof Mailcode_Commands_Command_If) |
||
| 223 | { |
||
| 224 | return $cmd; |
||
| 225 | } |
||
| 226 | |||
| 227 | throw self::_exceptionUnexpectedType($cmd); |
||
| 228 | } |
||
| 229 | |||
| 230 | public static function ifVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_If |
||
| 231 | { |
||
| 232 | $cmd = self::_buildIfVar('If', $variable, $operand, $value, true); |
||
| 233 | |||
| 234 | if($cmd instanceof Mailcode_Commands_Command_If) |
||
| 235 | { |
||
| 236 | return $cmd; |
||
| 237 | } |
||
| 238 | |||
| 239 | throw self::_exceptionUnexpectedType($cmd); |
||
| 240 | } |
||
| 241 | |||
| 242 | public static function ifVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If |
||
| 243 | { |
||
| 244 | $cmd = self::_buildIfVar('If', $variable, '==', $value, $quoteValue); |
||
| 245 | |||
| 246 | if($cmd instanceof Mailcode_Commands_Command_If) |
||
| 247 | { |
||
| 248 | return $cmd; |
||
| 249 | } |
||
| 250 | |||
| 251 | throw self::_exceptionUnexpectedType($cmd); |
||
| 252 | } |
||
| 253 | |||
| 254 | public static function ifVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If |
||
| 255 | { |
||
| 256 | $cmd = self::_buildIfVar('If', $variable, '==', $value, true); |
||
| 257 | |||
| 258 | if($cmd instanceof Mailcode_Commands_Command_If) |
||
| 259 | { |
||
| 260 | return $cmd; |
||
| 261 | } |
||
| 262 | |||
| 263 | throw self::_exceptionUnexpectedType($cmd); |
||
| 264 | } |
||
| 265 | |||
| 266 | public static function ifVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If |
||
| 267 | { |
||
| 268 | $cmd = self::_buildIfVar('If', $variable, '!=', $value, $quoteValue); |
||
| 269 | |||
| 270 | if($cmd instanceof Mailcode_Commands_Command_If) |
||
| 271 | { |
||
| 272 | return $cmd; |
||
| 273 | } |
||
| 274 | |||
| 275 | throw self::_exceptionUnexpectedType($cmd); |
||
| 276 | } |
||
| 277 | |||
| 278 | public static function ifVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If |
||
| 279 | { |
||
| 280 | $cmd = self::_buildIfVar('If', $variable, '!=', $value, true); |
||
| 281 | |||
| 282 | if($cmd instanceof Mailcode_Commands_Command_If) |
||
| 283 | { |
||
| 284 | return $cmd; |
||
| 285 | } |
||
| 286 | |||
| 287 | throw self::_exceptionUnexpectedType($cmd); |
||
| 288 | } |
||
| 289 | |||
| 290 | public static function elseIf(string $condition, string $type='') : Mailcode_Commands_Command_ElseIf |
||
| 291 | { |
||
| 292 | $cmd = self::_buildIf('ElseIf', $condition, $type); |
||
| 293 | |||
| 294 | if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
||
| 295 | { |
||
| 296 | return $cmd; |
||
| 297 | } |
||
| 298 | |||
| 299 | throw self::_exceptionUnexpectedType($cmd); |
||
| 300 | } |
||
| 301 | |||
| 302 | public static function elseIfVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf |
||
| 303 | { |
||
| 304 | $cmd = self::_buildIfVar('ElseIf', $variable, $operand, $value, $quoteValue); |
||
| 305 | |||
| 306 | if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
||
| 307 | { |
||
| 308 | return $cmd; |
||
| 309 | } |
||
| 310 | |||
| 311 | throw self::_exceptionUnexpectedType($cmd); |
||
| 312 | } |
||
| 313 | |||
| 314 | public static function elseIfVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_ElseIf |
||
| 315 | { |
||
| 316 | $cmd = self::_buildIfVar('ElseIf', $variable, $operand, $value, true); |
||
| 317 | |||
| 318 | if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
||
| 319 | { |
||
| 320 | return $cmd; |
||
| 321 | } |
||
| 322 | |||
| 323 | throw self::_exceptionUnexpectedType($cmd); |
||
| 324 | } |
||
| 325 | |||
| 326 | public static function elseIfVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf |
||
| 327 | { |
||
| 328 | $cmd = self::_buildIfVar('ElseIf', $variable, '==', $value, $quoteValue); |
||
| 329 | |||
| 330 | if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
||
| 331 | { |
||
| 332 | return $cmd; |
||
| 333 | } |
||
| 334 | |||
| 335 | throw self::_exceptionUnexpectedType($cmd); |
||
| 336 | } |
||
| 337 | |||
| 338 | public static function elseIfVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf |
||
| 348 | } |
||
| 349 | |||
| 350 | public static function elseIfVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf |
||
| 351 | { |
||
| 352 | $cmd = self::_buildIfVar('ElseIf', $variable, '!=', $value, $quoteValue); |
||
| 353 | |||
| 354 | if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
||
| 355 | { |
||
| 356 | return $cmd; |
||
| 357 | } |
||
| 358 | |||
| 359 | throw self::_exceptionUnexpectedType($cmd); |
||
| 360 | } |
||
| 361 | |||
| 362 | public static function elseIfVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf |
||
| 372 | } |
||
| 373 | |||
| 374 | protected static function _filterVariableName(string $name) : string |
||
| 375 | { |
||
| 376 | $name = preg_replace('/\s/', '', $name); |
||
| 377 | |||
| 378 | return '$'.ltrim($name, '$'); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Quotes a string literal: adds the quotes, and escapes any quotes already present in it. |
||
| 383 | * |
||
| 384 | * @param string $string |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | protected static function _quoteString(string $string) : string |
||
| 388 | { |
||
| 389 | return '"'.str_replace('"', '\"', $string).'"'; |
||
| 390 | } |
||
| 391 | |||
| 392 | protected static function _checkCommand(Mailcode_Commands_Command $command) : void |
||
| 393 | { |
||
| 394 | if($command->isValid()) |
||
| 395 | { |
||
| 396 | return; |
||
| 397 | } |
||
| 398 | |||
| 399 | throw new Mailcode_Factory_Exception( |
||
| 400 | 'Invalid command created.', |
||
| 401 | 'Validation message: '.$command->getValidationResult()->getErrorMessage(), |
||
| 402 | self::ERROR_INVALID_COMMAND_CREATED, |
||
| 403 | null, |
||
| 404 | $command |
||
| 405 | ); |
||
| 406 | } |
||
| 407 | |||
| 408 | protected static function _exceptionUnexpectedType(Mailcode_Commands_Command $command) : Mailcode_Factory_Exception |
||
| 416 | ); |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Creates a renderer instance, which can be used to easily |
||
| 421 | * create and convert commands to strings. |
||
| 422 | * |
||
| 423 | * @return Mailcode_Renderer |
||
| 424 | */ |
||
| 425 | public static function createRenderer() : Mailcode_Renderer |
||
| 426 | { |
||
| 427 | return new Mailcode_Renderer(); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Creates a printer instance, which works like the renderer, |
||
| 432 | * but outputs the generated strings to standard output. |
||
| 433 | * |
||
| 434 | * @return Mailcode_Printer |
||
| 435 | */ |
||
| 436 | public static function createPrinter() : Mailcode_Printer |
||
| 439 | } |
||
| 440 | } |
||
| 441 |