You are here

class DefaultIncidentStorage in Radioactivity 8.3

Same name and namespace in other branches
  1. 4.0.x src/DefaultIncidentStorage.php \Drupal\radioactivity\DefaultIncidentStorage

Defines a default incident storage.

Hierarchy

Expanded class hierarchy of DefaultIncidentStorage

3 files declare their use of DefaultIncidentStorage
DefaultIncidentStorageTest.php in tests/src/Unit/DefaultIncidentStorageTest.php
RadioactivityFunctionTestTrait.php in tests/src/Traits/RadioactivityFunctionTestTrait.php
StorageFactoryTest.php in tests/src/Unit/StorageFactoryTest.php
1 string reference to 'DefaultIncidentStorage'
radioactivity.services.yml in ./radioactivity.services.yml
radioactivity.services.yml
1 service uses DefaultIncidentStorage
radioactivity.default_incident_storage in ./radioactivity.services.yml
Drupal\radioactivity\DefaultIncidentStorage

File

src/DefaultIncidentStorage.php, line 10

Namespace

Drupal\radioactivity
View source
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';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultIncidentStorage::$state protected property The state key-value storage.
DefaultIncidentStorage::addIncident public function Adds an incident to the storage. Overrides IncidentStorageInterface::addIncident
DefaultIncidentStorage::clearIncidents public function Clears the incident storage. Overrides IncidentStorageInterface::clearIncidents
DefaultIncidentStorage::getIncidents public function Gets all incidents from the storage. Overrides IncidentStorageInterface::getIncidents
DefaultIncidentStorage::getIncidentsByType public function Gets all incidents from the storage per entity type. Overrides IncidentStorageInterface::getIncidentsByType
DefaultIncidentStorage::injectSettings public function Add endpoint settings to the page. Overrides IncidentStorageInterface::injectSettings
DefaultIncidentStorage::__construct public function DefaultIncidentStorage constructor.
IncidentStorageInterface::STORAGE_KEY constant The key to identify the radioactivity incident storage.