You are here

DefaultIncidentStorage.php in Radioactivity 4.0.x

Same filename and directory in other branches
  1. 8.3 src/DefaultIncidentStorage.php

File

src/DefaultIncidentStorage.php
View source
<?php

namespace Drupal\radioactivity;

use Drupal\Core\State\StateInterface;

/**
 * Defines a default incident storage.
 */
class DefaultIncidentStorage implements IncidentStorageInterface {

  /**
   * The state key-value storage.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * DefaultIncidentStorage constructor.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key-value storage.
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public function addIncident(IncidentInterface $incident) {
    $incidents = $this->state
      ->get(self::STORAGE_KEY, []);
    $incidents[] = $incident;
    $this->state
      ->set(self::STORAGE_KEY, $incidents);
  }

  /**
   * {@inheritdoc}
   */
  public function getIncidents() {
    return $this->state
      ->get(self::STORAGE_KEY, []);
  }

  /**
   * {@inheritdoc}
   */
  public function getIncidentsByType($entity_type = '') {
    $incidents = [];

    /** @var \Drupal\radioactivity\IncidentInterface[] $stored_incidents */
    $stored_incidents = $this->state
      ->get(self::STORAGE_KEY, []);
    foreach ($stored_incidents as $incident) {
      $incidents[$incident
        ->getEntityTypeId()][$incident
        ->getEntityId()][] = $incident;
    }
    if (isset($incidents[$entity_type])) {
      return [
        $entity_type => $incidents[$entity_type],
      ];
    }
    return $incidents ?: [
      [],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function clearIncidents() {
    $this->state
      ->set(self::STORAGE_KEY, []);
  }

  /**
   * {@inheritdoc}
   */
  public function injectSettings(array &$page) {
    global $base_url;
    $page['#attached']['drupalSettings']['radioactivity']['type'] = 'default';
    $page['#attached']['drupalSettings']['radioactivity']['endpoint'] = $base_url . '/radioactivity/emit';
  }

}

Classes

Namesort descending Description
DefaultIncidentStorage Defines a default incident storage.