You are here

class AuditLogLogger in Audit Log 8

Same name and namespace in other branches
  1. 8.2 src/AuditLogLogger.php \Drupal\audit_log\AuditLogLogger

Service for responding to audit log events.

@package Drupal\audit_log

Hierarchy

Expanded class hierarchy of AuditLogLogger

1 string reference to 'AuditLogLogger'
audit_log.services.yml in ./audit_log.services.yml
audit_log.services.yml
1 service uses AuditLogLogger
audit_log.logger in ./audit_log.services.yml
Drupal\audit_log\AuditLogLogger

File

src/AuditLogLogger.php, line 13

Namespace

Drupal\audit_log
View source
class AuditLogLogger {

  /**
   * An array of available event subscribers to respond to events.
   *
   * @var array
   */
  protected $entityEventEventSubscribers;

  /**
   * Logs an event to the audit log.
   *
   * @param string $event_type
   *   The type of event being reported such as "insert", "update", or "delete".
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity affected during the event.
   */
  public function log($event_type, EntityInterface $entity) {
    $event = new AuditLogEvent();
    $account = \Drupal::service('current_user')
      ->getAccount();
    $event
      ->setUser($account);
    $event
      ->setEntity($entity);
    $event
      ->setEventType($event_type);
    $event
      ->setRequestTime(REQUEST_TIME);
    foreach ($this
      ->sortEventSubscribers() as $event_subscriber) {
      if ($event_subscriber
        ->reactTo($event)) {
        \Drupal::service('audit_log.storage')
          ->save($event);
        break;
      }
    }
  }

  /**
   * Adds an event subscriber to the processing pipeline.
   *
   * @param \Drupal\audit_log\EventSubscriber\EventSubscriberInterface $event_subscriber
   *   An audit log event event subscriber.
   * @param int $priority
   *   A priority specification for the event subscriber.
   *
   *   Must be a positive integer.
   *
   *   Lower number event subscribers are processed
   *   before higher number event subscribers.
   */
  public function addEventSubscriber(EventSubscriberInterface $event_subscriber, $priority = 0) {
    $this->entityEventEventSubscribers[$priority][] = $event_subscriber;
  }

  /**
   * Sorts the available event subscribers by priority.
   *
   * @return array
   *   The sorted array of event subscribers.
   */
  protected function sortEventSubscribers() {
    $sorted = [];
    krsort($this->entityEventEventSubscribers);
    foreach ($this->entityEventEventSubscribers as $entity_event_event_subscribers) {
      $sorted = array_merge($sorted, $entity_event_event_subscribers);
    }
    return $sorted;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuditLogLogger::$entityEventEventSubscribers protected property An array of available event subscribers to respond to events.
AuditLogLogger::addEventSubscriber public function Adds an event subscriber to the processing pipeline.
AuditLogLogger::log public function Logs an event to the audit log.
AuditLogLogger::sortEventSubscribers protected function Sorts the available event subscribers by priority.