You are here

RavenCommands.php in Raven: Sentry Integration 3.x

Same filename and directory in other branches
  1. 8.2 src/Commands/RavenCommands.php

File

src/Commands/RavenCommands.php
View source
<?php

namespace Drupal\raven\Commands;

use Drupal\raven\Logger\Raven;
use Drush\Commands\DrushCommands;
use Sentry\Severity;

/**
 * Provides Drush commands for Raven module.
 */
class RavenCommands extends DrushCommands {

  /**
   * The @logger.raven service.
   *
   * @var \Drupal\raven\Logger\Raven|null
   */
  protected $ravenLogger;

  /**
   * Injects Raven logger service.
   */
  public function setRavenLogger(Raven $raven_logger) {
    $this->ravenLogger = $raven_logger;
  }

  /**
   * Send a test message to Sentry.
   *
   * Because messages are sent to Sentry asynchronously, there is no guarantee
   * that the message was actually delivered successfully.
   *
   * @param string $message
   *   The message text.
   * @param array $options
   *   An associative array of options.
   *
   * @option level
   *   The message level (debug, info, warning, error, fatal).
   *
   * @command raven:captureMessage
   * @usage drush raven:captureMessage
   *   Send test message to Sentry.
   * @usage drush raven:captureMessage --level=error
   *   Send error message to Sentry.
   * @usage drush raven:captureMessage 'Mic check.'
   *   Send "Mic check" message to Sentry.
   */
  public function captureMessage($message = 'Test message from Drush.', array $options = [
    'level' => 'info',
  ]) {
    if (!$this->ravenLogger) {
      throw new \Exception('Sentry logger service not available.');
    }
    if (!$this->ravenLogger
      ->getClient()) {
      throw new \Exception('Sentry client not available.');
    }
    $id = \Sentry\captureMessage($message, new Severity($options['level']));
    if (!$id) {
      throw new \Exception('Send failed.');
    }

    // Static analysis fix: LoggerInterface does not define a success method.
    if (method_exists($this
      ->logger(), 'success')) {
      $this
        ->logger()
        ->success(dt('Message sent as event %id.', [
        '%id' => $id,
      ]));
    }
  }

}

Classes

Namesort descending Description
RavenCommands Provides Drush commands for Raven module.