Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SectionEvent 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 SectionEvent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class SectionEvent extends Event |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * An associative array of results from the filters that have run |
||
| 18 | * on this event. |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | public $filter_results = array(); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * An associative array of errors from the filters that have run |
||
| 25 | * on this event. |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public $filter_errors = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * This method will construct XML that represents the result of |
||
| 32 | * an Event filter. |
||
| 33 | * |
||
| 34 | * @param string $name |
||
| 35 | * The name of the filter |
||
| 36 | * @param string $status |
||
| 37 | * The status of the filter, either passed or failed. |
||
| 38 | * @param XMLElement|string $message |
||
| 39 | * Optionally, an XMLElement or string to be appended to this |
||
| 40 | * `<filter>` element. XMLElement allows for more complex return |
||
| 41 | * types. |
||
| 42 | * @param array $attributes |
||
| 43 | * An associative array of additional attributes to add to this |
||
| 44 | * `<filter>` element |
||
| 45 | * @return XMLElement |
||
| 46 | */ |
||
| 47 | public static function buildFilterElement($name, $status, $message = null, array $attributes = null) |
||
| 48 | { |
||
| 49 | $filter = new XMLElement('filter', (!$message || is_object($message) ? null : $message), array('name' => $name, 'status' => $status)); |
||
| 50 | |||
| 51 | if ($message instanceof XMLElement) { |
||
| 52 | $filter->appendChild($message); |
||
| 53 | } |
||
| 54 | |||
| 55 | if (is_array($attributes)) { |
||
| 56 | $filter->setAttributeArray($attributes); |
||
| 57 | } |
||
| 58 | |||
| 59 | return $filter; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Appends errors generated from fields during the execution of an Event |
||
| 64 | * |
||
| 65 | * @param XMLElement $result |
||
| 66 | * @param array $fields |
||
| 67 | * @param array $errors |
||
| 68 | * @param object $post_values |
||
| 69 | * @throws Exception |
||
| 70 | * @return XMLElement |
||
| 71 | */ |
||
| 72 | public static function appendErrors(XMLElement $result, array $fields, $errors, $post_values) |
||
| 73 | { |
||
| 74 | $result->setAttribute('result', 'error'); |
||
| 75 | $result->appendChild(new XMLElement('message', __('Entry encountered errors when saving.'), array( |
||
| 76 | 'message-id' => EventMessages::ENTRY_ERRORS |
||
| 77 | ))); |
||
| 78 | |||
| 79 | foreach ($errors as $field_id => $message) { |
||
| 80 | $field = (new FieldManager)->select()->field($field_id)->execute()->next(); |
||
| 81 | |||
| 82 | // Do a little bit of a check for files so that we can correctly show |
||
| 83 | // whether they are 'missing' or 'invalid'. If it's missing, then we |
||
| 84 | // want to remove the data so `__reduceType` will correctly resolve to |
||
| 85 | // missing instead of invalid. |
||
| 86 | // @see https://github.com/symphonists/s3upload_field/issues/17 |
||
| 87 | if (isset($_FILES['fields']['error'][$field->get('element_name')])) { |
||
| 88 | $upload = $_FILES['fields']['error'][$field->get('element_name')]; |
||
| 89 | |||
| 90 | if ($upload === UPLOAD_ERR_NO_FILE) { |
||
| 91 | unset($fields[$field->get('element_name')]); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | if (is_array($fields[$field->get('element_name')])) { |
||
| 96 | $type = array_reduce($fields[$field->get('element_name')], array('SectionEvent', '__reduceType')); |
||
| 97 | } else { |
||
| 98 | $type = ($fields[$field->get('element_name')] == '') ? 'missing' : 'invalid'; |
||
| 99 | } |
||
| 100 | |||
| 101 | $error = self::createError($field, $type, $message); |
||
| 102 | $result->appendChild($error); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (isset($post_values) && is_object($post_values)) { |
||
| 106 | $result->appendChild($post_values); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $result; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Given a Field instance, the type of error, and the message, this function |
||
| 114 | * creates an XMLElement node so that it can be added to the `?debug` for the |
||
| 115 | * Event |
||
| 116 | * |
||
| 117 | * @since Symphony 2.5.0 |
||
| 118 | * @param Field $field |
||
| 119 | * @param string $type |
||
| 120 | * At the moment 'missing' or 'invalid' accepted |
||
| 121 | * @param string $message |
||
| 122 | * @return XMLElement |
||
| 123 | */ |
||
| 124 | public static function createError(Field $field, $type, $message = null) |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * This function searches the `$haystack` for the given `$needle`, |
||
| 138 | * where the needle is a string representation of where the desired |
||
| 139 | * value exists in the `$haystack` array. For example `fields[name]` |
||
| 140 | * would look in the `$haystack` for the key of `fields` that has the |
||
| 141 | * key `name` and return the value. |
||
| 142 | * |
||
| 143 | * @param string $needle |
||
| 144 | * The needle, ie. `fields[name]`. |
||
| 145 | * @param array $haystack |
||
| 146 | * Associative array to find the needle, ie. |
||
| 147 | * `array('fields' => array( |
||
| 148 | * 'name' => 'Bob', |
||
| 149 | * 'age' => '10' |
||
| 150 | * ))` |
||
| 151 | * @param string $default |
||
| 152 | * If the `$needle` is not found, return this value. Defaults to null. |
||
| 153 | * @param boolean $discard_field_name |
||
| 154 | * When matches are found in the `$haystack`, they are added to results |
||
| 155 | * array. This parameter defines if this should be an associative array |
||
| 156 | * or just an array of the matches. Used in conjunction with `$collapse` |
||
| 157 | * @param boolean $collapse |
||
| 158 | * If multiple values are found, this will cause them to be reduced |
||
| 159 | * to single string with ' ' as the separator. Defaults to true. |
||
| 160 | * @return string|array |
||
| 161 | */ |
||
| 162 | public static function replaceFieldToken($needle, $haystack, $default = null, $discard_field_name = true, $collapse = true) |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Helper method to determine if a field is missing, or if the data |
||
| 193 | * provided was invalid. Used in conjunction with `array_reduce`. |
||
| 194 | * |
||
| 195 | * @param array $a, |
||
| 196 | * @param array $b |
||
| 197 | * @return string |
||
| 198 | * 'missing' or 'invalid' |
||
| 199 | */ |
||
| 200 | public static function __reduceType($a, $b) |
||
| 201 | { |
||
| 202 | if (is_array($b)) { |
||
| 203 | return array_reduce($b, array('SectionEvent', '__reduceType')); |
||
| 204 | } |
||
| 205 | |||
| 206 | return (strlen(trim($b)) === 0) ? 'missing' : 'invalid'; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * This function will process the core Filters, Admin Only and Expect |
||
| 211 | * Multiple, before invoking the `__doit` function, which actually |
||
| 212 | * processes the Event. Once the Event has executed, this function will |
||
| 213 | * determine if the user should be redirected to a URL, or to just return |
||
| 214 | * the XML. |
||
| 215 | * |
||
| 216 | * @throws Exception |
||
| 217 | * @return XMLElement|void |
||
| 218 | * If `$_REQUEST{'redirect']` is set, and the Event executed successfully, |
||
| 219 | * the user will be redirected to the given location. If `$_REQUEST['redirect']` |
||
| 220 | * is not set, or the Event encountered errors, an XMLElement of the Event |
||
| 221 | * result will be returned. |
||
| 222 | */ |
||
| 223 | public function execute() |
||
| 224 | { |
||
| 225 | if (!isset($this->eParamFILTERS) || !is_array($this->eParamFILTERS)) { |
||
| 226 | $this->eParamFILTERS = array(); |
||
| 227 | } |
||
| 228 | |||
| 229 | $result = new XMLElement($this->ROOTELEMENT); |
||
| 230 | |||
| 231 | if (in_array('admin-only', $this->eParamFILTERS) && !Symphony::Engine()->isLoggedIn()) { |
||
| 232 | $result->setAttribute('result', 'error'); |
||
| 233 | $result->appendChild(new XMLElement('message', __('Entry encountered errors when saving.'), array( |
||
| 234 | 'message-id' => EventMessages::ENTRY_ERRORS |
||
| 235 | ))); |
||
| 236 | $result->appendChild(self::buildFilterElement('admin-only', 'failed')); |
||
| 237 | return $result; |
||
| 238 | } |
||
| 239 | |||
| 240 | $entry_id = $position = $fields = null; |
||
| 241 | $post = General::getPostData(); |
||
| 242 | $success = true; |
||
| 243 | if (!is_array($post['fields'])) { |
||
| 244 | $post['fields'] = array(); |
||
| 245 | } |
||
| 246 | |||
| 247 | if (in_array('expect-multiple', $this->eParamFILTERS)) { |
||
| 248 | foreach ($post['fields'] as $position => $fields) { |
||
| 249 | if (isset($post['id'][$position]) && is_numeric($post['id'][$position])) { |
||
| 250 | $entry_id = $post['id'][$position]; |
||
| 251 | } else { |
||
| 252 | $entry_id = null; |
||
| 253 | } |
||
| 254 | |||
| 255 | $entry = new XMLElement('entry', null, array('position' => $position)); |
||
| 256 | |||
| 257 | // Reset errors for each entry execution |
||
| 258 | $this->filter_results = $this->filter_errors = array(); |
||
| 259 | |||
| 260 | // Ensure that we are always dealing with an array. |
||
| 261 | if (!is_array($fields)) { |
||
| 262 | $fields = array(); |
||
| 263 | } |
||
| 264 | |||
| 265 | // Execute the event for this entry |
||
| 266 | if (!$this->__doit($fields, $entry, $position, $entry_id)) { |
||
| 267 | $success = false; |
||
| 268 | } |
||
| 269 | |||
| 270 | $result->appendChild($entry); |
||
| 271 | } |
||
| 272 | } else { |
||
| 273 | $fields = $post['fields']; |
||
| 274 | |||
| 275 | if (isset($post['id']) && is_numeric($post['id'])) { |
||
| 276 | $entry_id = $post['id']; |
||
| 277 | } |
||
| 278 | |||
| 279 | $success = $this->__doit($fields, $result, null, $entry_id); |
||
| 280 | } |
||
| 281 | |||
| 282 | if ($success && isset($_REQUEST['redirect'])) { |
||
| 283 | redirect($_REQUEST['redirect']); |
||
| 284 | } |
||
| 285 | |||
| 286 | return $result; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * This function does the bulk of processing the Event, from running the delegates |
||
| 291 | * to validating the data and eventually saving the data into Symphony. The result |
||
| 292 | * of the Event is returned via the `$result` parameter. |
||
| 293 | * |
||
| 294 | * @param array $fields |
||
| 295 | * An array of $_POST data, to process and add/edit an entry. |
||
| 296 | * @param XMLElement $result |
||
| 297 | * The XMLElement contains the result of the Event, it is passed by |
||
| 298 | * reference. |
||
| 299 | * @param integer $position |
||
| 300 | * When the Expect Multiple filter is added, this event should expect |
||
| 301 | * to deal with adding (or editing) multiple entries at once. |
||
| 302 | * @param integer $entry_id |
||
| 303 | * If this Event is editing an existing entry, that Entry ID will |
||
| 304 | * be passed to this function. |
||
| 305 | * @throws Exception |
||
| 306 | * @return XMLElement |
||
| 307 | * The result of the Event |
||
| 308 | */ |
||
| 309 | public function __doit(array $fields = array(), XMLElement &$result, $position = null, $entry_id = null) |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Processes all extensions attached to the `EventPreSaveFilter` delegate |
||
| 428 | * |
||
| 429 | * @uses EventPreSaveFilter |
||
| 430 | * |
||
| 431 | * @param XMLElement $result |
||
| 432 | * @param array $fields |
||
| 433 | * @param XMLElement $post_values |
||
| 434 | * @param integer $entry_id |
||
| 435 | * @return boolean |
||
| 436 | */ |
||
| 437 | protected function processPreSaveFilters(XMLElement $result, array &$fields, XMLElement &$post_values, $entry_id = null) |
||
| 438 | { |
||
| 439 | $can_proceed = true; |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Prior to saving entry from the front-end. This delegate will |
||
| 443 | * force the Event to terminate if it populates the `$filter_results` |
||
| 444 | * array. All parameters are passed by reference. |
||
| 445 | * |
||
| 446 | * @delegate EventPreSaveFilter |
||
| 447 | * @param string $context |
||
| 448 | * '/frontend/' |
||
| 449 | * @param array $fields |
||
| 450 | * @param Event $this |
||
| 451 | * @param array $messages |
||
| 452 | * An associative array of array's which contain 4 values, |
||
| 453 | * the name of the filter (string), the status (boolean), |
||
| 454 | * the message (string) an optionally an associative array |
||
| 455 | * of additional attributes to add to the filter element. |
||
| 456 | * @param XMLElement $post_values |
||
| 457 | * @param integer $entry_id |
||
| 458 | * If editing an entry, this parameter will be an integer, |
||
| 459 | * otherwise null. |
||
| 460 | */ |
||
| 461 | Symphony::ExtensionManager()->notifyMembers( |
||
| 462 | 'EventPreSaveFilter', |
||
| 463 | '/frontend/', |
||
| 464 | array( |
||
| 465 | 'fields' => &$fields, |
||
| 466 | 'event' => &$this, |
||
| 467 | 'messages' => &$this->filter_results, |
||
| 468 | 'post_values' => &$post_values, |
||
| 469 | 'entry_id' => $entry_id |
||
| 470 | ) |
||
| 471 | ); |
||
| 472 | |||
| 473 | // Logic taken from `event.section.php` to fail should any `$this->filter_results` |
||
| 474 | // be returned. This delegate can cause the event to exit early. |
||
| 475 | if (is_array($this->filter_results) && !empty($this->filter_results)) { |
||
| 476 | $can_proceed = true; |
||
| 477 | |||
| 478 | foreach ($this->filter_results as $fr) { |
||
| 479 | list($name, $status, $message, $attributes) = array_pad($fr, 4, null); |
||
| 480 | |||
| 481 | $result->appendChild( |
||
| 482 | self::buildFilterElement($name, ($status ? 'passed' : 'failed'), $message, $attributes) |
||
| 483 | ); |
||
| 484 | |||
| 485 | if ($status === false) { |
||
| 486 | $can_proceed = false; |
||
| 487 | } |
||
| 488 | } |
||
| 489 | |||
| 490 | if ($can_proceed !== true) { |
||
| 491 | $result->appendChild($post_values); |
||
| 492 | $result->setAttribute('result', 'error'); |
||
| 493 | $result->appendChild(new XMLElement('message', __('Entry encountered errors when saving.'), array( |
||
| 494 | 'message-id' => EventMessages::FILTER_FAILED |
||
| 495 | ))); |
||
| 496 | } |
||
| 497 | } |
||
| 498 | |||
| 499 | // Reset the filter results to prevent duplicates. RE: #2179 |
||
| 500 | $this->filter_results = array(); |
||
| 501 | return $can_proceed; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Processes all extensions attached to the `EventPostSaveFilter` delegate |
||
| 506 | * |
||
| 507 | * @uses EventPostSaveFilter |
||
| 508 | * |
||
| 509 | * @param XMLElement $result |
||
| 510 | * @param array $fields |
||
| 511 | * @param Entry $entry |
||
| 512 | * @return XMLElement |
||
| 513 | */ |
||
| 514 | protected function processPostSaveFilters(XMLElement $result, array $fields, Entry $entry = null) |
||
| 515 | { |
||
| 516 | /** |
||
| 517 | * After saving entry from the front-end. This delegate will not force |
||
| 518 | * the Events to terminate if it populates the `$filter_results` array. |
||
| 519 | * Provided with references to this object, the `$_POST` data and also |
||
| 520 | * the error array |
||
| 521 | * |
||
| 522 | * @delegate EventPostSaveFilter |
||
| 523 | * @param string $context |
||
| 524 | * '/frontend/' |
||
| 525 | * @param integer $entry_id |
||
| 526 | * @param array $fields |
||
| 527 | * @param Entry $entry |
||
| 528 | * @param Event $this |
||
| 529 | * @param array $messages |
||
| 530 | * An associative array of array's which contain 4 values, |
||
| 531 | * the name of the filter (string), the status (boolean), |
||
| 532 | * the message (string) an optionally an associative array |
||
| 533 | * of additional attributes to add to the filter element. |
||
| 534 | */ |
||
| 535 | Symphony::ExtensionManager()->notifyMembers('EventPostSaveFilter', '/frontend/', array( |
||
| 536 | 'entry_id' => $entry->get('id'), |
||
| 537 | 'fields' => $fields, |
||
| 538 | 'entry' => $entry, |
||
| 539 | 'event' => &$this, |
||
| 540 | 'messages' => &$this->filter_results |
||
| 541 | )); |
||
| 542 | |||
| 543 | if (is_array($this->filter_results) && !empty($this->filter_results)) { |
||
| 544 | foreach ($this->filter_results as $fr) { |
||
| 545 | list($name, $status, $message, $attributes) = $fr; |
||
| 546 | |||
| 547 | $result->appendChild( |
||
| 548 | self::buildFilterElement($name, ($status ? 'passed' : 'failed'), $message, $attributes) |
||
| 549 | ); |
||
| 550 | } |
||
| 551 | } |
||
| 552 | |||
| 553 | // Reset the filter results to prevent duplicates. RE: #2179 |
||
| 554 | $this->filter_results = array(); |
||
| 555 | return $result; |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Processes all extensions attached to the `EventFinalSaveFilter` delegate |
||
| 560 | * |
||
| 561 | * @uses EventFinalSaveFilter |
||
| 562 | * |
||
| 563 | * @param XMLElement $result |
||
| 564 | * @param array $fields |
||
| 565 | * @param Entry $entry |
||
| 566 | * @return XMLElement |
||
| 567 | */ |
||
| 568 | protected function processFinalSaveFilters(XMLElement $result, array $fields, Entry $entry = null) |
||
| 617 | } |
||
| 618 | |||
| 619 | /** |
||
| 620 | * This function handles the Send Mail filter which will send an email |
||
| 621 | * to each specified recipient informing them that an Entry has been |
||
| 622 | * created. |
||
| 623 | * |
||
| 624 | * @param XMLElement $result |
||
| 625 | * The XMLElement of the XML that is going to be returned as part |
||
| 626 | * of this event to the page. |
||
| 627 | * @param array $send_email |
||
| 628 | * Associative array of `send-mail` parameters.* Associative array of `send-mail` parameters. |
||
| 629 | * @param array $fields |
||
| 630 | * Array of post data to extract the values from |
||
| 631 | * @param Section $section |
||
| 632 | * This current Entry that has just been updated or created |
||
| 633 | * @param Entry $entry |
||
| 634 | * @throws Exception |
||
| 635 | * @return XMLElement |
||
| 636 | * The modified `$result` with the results of the filter. |
||
| 637 | */ |
||
| 638 | public function processSendMailFilter(XMLElement $result, array $send_email, array &$fields, Section $section, Entry $entry) |
||
| 639 | { |
||
| 640 | $fields['recipient'] = self::replaceFieldToken($send_email['recipient'], $fields); |
||
| 641 | $fields['recipient'] = preg_split('/\,/i', $fields['recipient'], -1, PREG_SPLIT_NO_EMPTY); |
||
| 642 | $fields['recipient'] = array_map('trim', $fields['recipient']); |
||
| 643 | |||
| 644 | $fields['subject'] = self::replaceFieldToken($send_email['subject'], $fields, __('[Symphony] A new entry was created on %s', array(Symphony::Configuration()->get('sitename', 'general')))); |
||
| 645 | $fields['body'] = self::replaceFieldToken($send_email['body'], $fields, null, false, false); |
||
| 646 | $fields['sender-email'] = self::replaceFieldToken($send_email['sender-email'], $fields); |
||
| 647 | $fields['sender-name'] = self::replaceFieldToken($send_email['sender-name'], $fields); |
||
| 648 | |||
| 649 | $fields['reply-to-name'] = self::replaceFieldToken($send_email['reply-to-name'], $fields); |
||
| 650 | $fields['reply-to-email'] = self::replaceFieldToken($send_email['reply-to-email'], $fields); |
||
| 651 | |||
| 652 | $edit_link = SYMPHONY_URL . '/publish/' . $section->get('handle') . '/edit/' . $entry->get('id').'/'; |
||
| 653 | $language = Symphony::Configuration()->get('lang', 'symphony'); |
||
| 654 | $template_path = Event::getNotificationTemplate($language); |
||
| 655 | $body = sprintf(file_get_contents($template_path), $section->get('name'), $edit_link); |
||
| 656 | |||
| 657 | if (is_array($fields['body'])) { |
||
| 658 | foreach ($fields['body'] as $field_handle => $value) { |
||
| 659 | $body .= "// $field_handle" . PHP_EOL . $value . PHP_EOL . PHP_EOL; |
||
| 660 | } |
||
| 661 | } else { |
||
| 662 | $body .= $fields['body']; |
||
| 663 | } |
||
| 664 | |||
| 665 | // Loop over all the recipients and attempt to send them an email |
||
| 666 | // Errors will be appended to the Event XML |
||
| 667 | $errors = array(); |
||
| 668 | |||
| 669 | foreach ($fields['recipient'] as $recipient) { |
||
| 670 | $author = AuthorManager::fetchByUsername($recipient); |
||
| 671 | |||
| 672 | if (empty($author)) { |
||
| 673 | $errors['recipient'][$recipient] = __('Recipient not found'); |
||
| 674 | continue; |
||
| 675 | } |
||
| 676 | |||
| 677 | $email = Email::create(); |
||
| 678 | |||
| 679 | // Exceptions are also thrown in the settings functions, not only in the send function. |
||
| 680 | // Those Exceptions should be caught too. |
||
| 681 | try { |
||
| 682 | $email->recipients = array( |
||
| 683 | $author->get('first_name') => $author->get('email') |
||
| 684 | ); |
||
| 685 | |||
| 686 | if ($fields['sender-name'] != null) { |
||
| 687 | $email->sender_name = $fields['sender-name']; |
||
| 688 | } |
||
| 689 | |||
| 690 | if ($fields['sender-email'] != null) { |
||
| 691 | $email->sender_email_address = $fields['sender-email']; |
||
| 692 | } |
||
| 693 | |||
| 694 | if ($fields['reply-to-name'] != null) { |
||
| 695 | $email->reply_to_name = $fields['reply-to-name']; |
||
| 696 | } |
||
| 697 | |||
| 698 | if ($fields['reply-to-email'] != null) { |
||
| 699 | $email->reply_to_email_address = $fields['reply-to-email']; |
||
| 700 | } |
||
| 701 | |||
| 702 | $email->text_plain = str_replace('<!-- RECIPIENT NAME -->', $author->get('first_name'), $body); |
||
| 703 | $email->subject = $fields['subject']; |
||
| 704 | $email->send(); |
||
| 705 | } catch (EmailValidationException $e) { |
||
| 706 | $errors['address'][$author->get('email')] = $e->getMessage(); |
||
| 707 | |||
| 708 | // The current error array does not permit custom tags. |
||
| 709 | // Therefore, it is impossible to set a "proper" error message. |
||
| 710 | // Will return the failed email address instead. |
||
| 711 | } catch (EmailGatewayException $e) { |
||
| 712 | $errors['gateway'][$author->get('email')] = $e->getMessage(); |
||
| 713 | |||
| 714 | // Because we don't want symphony to break because it can not send emails, |
||
| 715 | // all exceptions are logged silently. |
||
| 716 | // Any custom event can change this behaviour. |
||
| 717 | } catch (EmailException $e) { |
||
| 718 | $errors['email'][$author->get('email')] = $e->getMessage(); |
||
| 719 | } |
||
| 720 | } |
||
| 721 | |||
| 722 | // If there were errors, output them to the event |
||
| 723 | if (!empty($errors)) { |
||
| 724 | $xml = self::buildFilterElement('send-email', 'failed'); |
||
| 725 | |||
| 726 | foreach ($errors as $type => $messages) { |
||
| 727 | $xType = new XMLElement('error'); |
||
| 728 | $xType->setAttribute('error-type', $type); |
||
| 729 | |||
| 730 | foreach ($messages as $recipient => $message) { |
||
| 731 | $xType->appendChild( |
||
| 732 | new XMLElement('message', General::wrapInCDATA($message), array( |
||
| 733 | 'recipient' => $recipient |
||
| 734 | )) |
||
| 735 | ); |
||
| 736 | } |
||
| 737 | |||
| 738 | $xml->appendChild($xType); |
||
| 739 | } |
||
| 740 | |||
| 741 | $result->appendChild($xml); |
||
| 742 | } else { |
||
| 743 | $result->appendChild( |
||
| 744 | self::buildFilterElement('send-email', 'passed') |
||
| 745 | ); |
||
| 746 | } |
||
| 779 |