You are here

RestIncidentStorage.php in Radioactivity 4.0.x

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

File

src/RestIncidentStorage.php
View source
<?php

namespace Drupal\radioactivity;

use Drupal\Component\Serialization\Json;

/**
 * Defines a REST incident storage.
 */
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");
  }

}

Classes

Namesort descending Description
RestIncidentStorage Defines a REST incident storage.