You are here

class NewRelicConfigSubscriber in New Relic 8

Same name and namespace in other branches
  1. 2.x src/EventSubscriber/NewRelicConfigSubscriber.php \Drupal\new_relic_rpm\EventSubscriber\NewRelicConfigSubscriber
  2. 2.0.x src/EventSubscriber/NewRelicConfigSubscriber.php \Drupal\new_relic_rpm\EventSubscriber\NewRelicConfigSubscriber

Config event listener to mark deployments when a user imports configuration.

Hierarchy

Expanded class hierarchy of NewRelicConfigSubscriber

1 string reference to 'NewRelicConfigSubscriber'
new_relic_rpm.services.yml in ./new_relic_rpm.services.yml
new_relic_rpm.services.yml
1 service uses NewRelicConfigSubscriber
new_relic_rpm.config_subscriber in ./new_relic_rpm.services.yml
Drupal\new_relic_rpm\EventSubscriber\NewRelicConfigSubscriber

File

src/EventSubscriber/NewRelicConfigSubscriber.php, line 16

Namespace

Drupal\new_relic_rpm\EventSubscriber
View source
class NewRelicConfigSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * New Relic adapter.
   *
   * @var \Drupal\new_relic_rpm\ExtensionAdapter\NewRelicAdapterInterface
   */
  protected $adapter;

  /**
   * The configuration for the New Relic module.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The current user account.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Constructs a NewRelicConfigSubscriber.
   *
   * @param \Drupal\new_relic_rpm\ExtensionAdapter\NewRelicAdapterInterface $adapter
   *   The Adapter that we use to talk to the New Relic extension.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The object we use to get our settings.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The currently logged in user.
   */
  public function __construct(NewRelicAdapterInterface $adapter, ConfigFactoryInterface $config_factory, AccountInterface $current_user) {
    $this->adapter = $adapter;
    $this->config = $config_factory
      ->get('new_relic_rpm.settings');
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ConfigEvents::IMPORT][] = [
      'onImport',
    ];
    return $events;
  }

  /**
   * Attempts to create a deployment on New Relic when a config import happens.
   *
   * @param \Drupal\Core\Config\ConfigImporterEvent $event
   *   The current config event that we are responding to.
   */
  public function onImport(ConfigImporterEvent $event) {
    if (!$this->config
      ->get('config_import')) {
      return;
    }
    $changes = $event
      ->getChangelist();
    $name = $this->currentUser
      ->getAccountName();
    $description = $this
      ->t('A configuration import was run on the site.');
    $changelog = '';
    if (!empty($changes['create'])) {
      $changelog .= 'Configurations created:
';
      foreach ($changes['create'] as $config_key) {
        $changelog .= $config_key . '
';
      }
    }
    if (!empty($changes['update'])) {
      $changelog .= 'Configurations updated:
';
      foreach ($changes['update'] as $config_key) {
        $changelog .= $config_key . '
';
      }
    }
    if (!empty($changes['delete'])) {
      $changelog .= 'Configurations deleted:
';
      foreach ($changes['delete'] as $config_key) {
        $changelog .= $config_key . '
';
      }
    }
    if (!empty($changes['rename'])) {
      $changelog .= 'Configurations renamed:
';
      foreach ($changes['rename'] as $config_key) {
        $changelog .= $config_key . '
';
      }
    }

    /** @var \Drupal\new_relic_rpm\Client\NewRelicApiClient $client */
    $client = \Drupal::service('new_relic_rpm.client');
    $client
      ->createDeployment('config_import', $description, $name, $changelog);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NewRelicConfigSubscriber::$adapter protected property New Relic adapter.
NewRelicConfigSubscriber::$config protected property The configuration for the New Relic module.
NewRelicConfigSubscriber::$currentUser protected property The current user account.
NewRelicConfigSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
NewRelicConfigSubscriber::onImport public function Attempts to create a deployment on New Relic when a config import happens.
NewRelicConfigSubscriber::__construct public function Constructs a NewRelicConfigSubscriber.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.