You are here

class RestIncidentStorage in Radioactivity 8.3

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

Defines a REST incident storage.

Hierarchy

Expanded class hierarchy of RestIncidentStorage

2 files declare their use of RestIncidentStorage
RestIncidentStorageTest.php in tests/src/Unit/RestIncidentStorageTest.php
StorageFactoryTest.php in tests/src/Unit/StorageFactoryTest.php
1 string reference to 'RestIncidentStorage'
radioactivity.services.yml in ./radioactivity.services.yml
radioactivity.services.yml
1 service uses RestIncidentStorage
radioactivity.rest_incident_storage in ./radioactivity.services.yml
Drupal\radioactivity\RestIncidentStorage

File

src/RestIncidentStorage.php, line 10

Namespace

Drupal\radioactivity
View source
class RestIncidentStorage implements RestIncidentStorageInterface {

  /**
   * REST endpoint URL for incidents.
   *
   * @var string
   */
  protected $endpoint = NULL;

  /**
   * {@inheritdoc}
   */
  public function addIncident(IncidentInterface $incident) {
    throw new \Exception("The Radioactivity rest endpoint expects incidents to be added somewhere else.");
  }

  /**
   * {@inheritdoc}
   */
  public function getIncidents() {
    $data = $this
      ->getIncidentsFromStorage();
    $result = [];
    foreach ($data as $rows) {
      foreach ($rows as $row) {
        $incident = Incident::createFromPostData($row);
        if ($incident
          ->isValid()) {
          $result[] = $incident;
        }
      }
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getIncidentsByType($entity_type = '') {
    $incidents = [];
    $stored_incidents = $this
      ->getIncidents();
    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
      ->clearIncidentStorage();
  }

  /**
   * {@inheritdoc}
   */
  public function injectSettings(array &$page) {
    $page['#attached']['drupalSettings']['radioactivity']['type'] = 'rest';
    $page['#attached']['drupalSettings']['radioactivity']['endpoint'] = $this
      ->getEndpoint();
  }

  /**
   * {@inheritdoc}
   */
  public function setEndpoint($endpoint = NULL) {
    $this->endpoint = $endpoint;
  }

  /**
   * Returns the endpoint URL.
   *
   * @return string
   *   The endpoint URL.
   */
  protected function getEndpoint() {
    if (is_null($this->endpoint)) {
      $this->endpoint = $this
        ->getDefaultEndpoint();
    }
    return $this->endpoint;
  }

  /**
   * Returns the default storage endpoint.
   *
   * @return string
   *   The storage endpoint.
   */
  protected function getDefaultEndpoint() {
    global $base_url;
    return $base_url . '/' . drupal_get_path('module', 'radioactivity') . '/endpoints/file/rest.php';
  }

  /**
   * Returns all incidents from the storage.
   *
   * @return array
   *   The incidents.
   */
  protected function getIncidentsFromStorage() {
    return Json::decode(file_get_contents("{$this->getEndpoint()}?get"));
  }

  /**
   * Deletes all incidents from the storage.
   */
  protected function clearIncidentStorage() {
    file_get_contents("{$this->getEndpoint()}?clear");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IncidentStorageInterface::STORAGE_KEY constant The key to identify the radioactivity incident storage.
RestIncidentStorage::$endpoint protected property REST endpoint URL for incidents.
RestIncidentStorage::addIncident public function Adds an incident to the storage. Overrides IncidentStorageInterface::addIncident
RestIncidentStorage::clearIncidents public function Clears the incident storage. Overrides IncidentStorageInterface::clearIncidents
RestIncidentStorage::clearIncidentStorage protected function Deletes all incidents from the storage.
RestIncidentStorage::getDefaultEndpoint protected function Returns the default storage endpoint.
RestIncidentStorage::getEndpoint protected function Returns the endpoint URL.
RestIncidentStorage::getIncidents public function Gets all incidents from the storage. Overrides IncidentStorageInterface::getIncidents
RestIncidentStorage::getIncidentsByType public function Gets all incidents from the storage per entity type. Overrides IncidentStorageInterface::getIncidentsByType
RestIncidentStorage::getIncidentsFromStorage protected function Returns all incidents from the storage.
RestIncidentStorage::injectSettings public function Add endpoint settings to the page. Overrides IncidentStorageInterface::injectSettings
RestIncidentStorage::setEndpoint public function Sets the REST endpoint URL. Overrides RestIncidentStorageInterface::setEndpoint