You are here

public function MailhandlerComment::invoke in Mailhandler 8

File

mailhandler_comment/src/Plugin/inmail/Handler/MailhandlerComment.php, line 73

Class

MailhandlerComment
Message handler that supports posting comments via email.

Namespace

Drupal\mailhandler_comment\Plugin\inmail\Handler

Code

public function invoke(MimeMessageInterface $message, ProcessorResultInterface $processor_result) {
  try {
    $result = $processor_result
      ->getAnalyzerResult();
    if (!$result
      ->hasContext('entity_type') || $result
      ->getContext('entity_type')
      ->getContextValue()['entity_type'] != 'comment') {

      // Do not run this handler in case we are not dealing with comments.
      return;
    }

    // Create a comment.
    $comment = $this
      ->createComment($message, $result);
    $processor_result
      ->log('CommentHandler', '@comment has been created by @user.', [
      '@comment' => $comment
        ->label(),
      '@user' => $comment
        ->getAuthorName(),
    ]);
  } catch (\Exception $e) {

    // Log error in case verification, authentication or authorization fails.
    $processor_result
      ->log('CommentHandler', $e
      ->getMessage());
  }
}