You are here

class Incident in Radioactivity 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Incident.php \Drupal\radioactivity\Incident
  2. 8.2 src/Incident.php \Drupal\radioactivity\Incident

Data class for Radioactivity Incident.

@package Drupal\radioactivity

Hierarchy

Expanded class hierarchy of Incident

4 files declare their use of Incident
EmitController.php in src/Controller/EmitController.php
IncidentTest.php in tests/src/Unit/IncidentTest.php
RadioactivityEmitter.php in src/Plugin/Field/FieldFormatter/RadioactivityEmitter.php
RadioactivityReferenceEmitter.php in src/Plugin/Field/FieldFormatter/RadioactivityReferenceEmitter.php

File

src/Incident.php, line 13

Namespace

Drupal\radioactivity
View source
class Incident implements IncidentInterface {

  /**
   * The incident field name.
   *
   * @var string
   */
  private $fieldName;

  /**
   * The incident entity type.
   *
   * @var string
   */
  private $entityType;

  /**
   * The incident entity id.
   *
   * @var string|int
   */
  private $entityId;

  /**
   * The id of the referenced radioactivity entity.
   *
   * @var string|int
   */
  private $targetId;

  /**
   * The incident energy.
   *
   * @var int|float
   */
  private $energy;

  /**
   * The incident hash.
   *
   * @var string
   */
  private $hash;

  /**
   * Constructor.
   *
   * @param string $field_name
   *   The field name from the incident.
   * @param string $entity_type
   *   The entity type from the incident.
   * @param string|int $entity_id
   *   The entity id from the incident.
   * @param string|int $target_id
   *   The id from the referenced radioactivity entity.
   * @param int|float $energy
   *   The energy from the incident.
   * @param string $hash
   *   The hash from the incident.
   */
  public function __construct($field_name, $entity_type, $entity_id, $target_id, $energy, $hash = NULL) {
    $this->fieldName = $field_name;
    $this->entityType = $entity_type;
    $this->entityId = $entity_id;
    $this->targetId = $target_id;
    $this->energy = $energy;
    $this->hash = $hash;
  }

  /**
   * {@inheritdoc}
   */
  public function isValid() {
    return strcmp($this->hash, $this
      ->calculateHash()) === 0;
  }

  /**
   * Calculate hash for this incident.
   *
   * @return string
   *   The calculated hash of this incident.
   */
  private function calculateHash() {
    return sha1(implode('##', [
      $this->fieldName,
      $this->entityType,
      $this->entityId,
      $this->targetId,
      $this->energy,
      Settings::getHashSalt(),
    ]));
  }

  /**
   * {@inheritdoc}
   */
  public function toJson() {
    return Json::encode([
      'fn' => $this->fieldName,
      'et' => $this->entityType,
      'id' => $this->entityId,
      'ti' => $this->targetId,
      'e' => $this->energy,
      'h' => $this
        ->calculateHash(),
    ]);
  }

  /**
   * Create an Incident from data received in an http request.
   *
   * @param array $data
   *   Associative array of incident data.
   *
   * @return \Drupal\radioactivity\IncidentInterface
   *   An Incident object.
   */
  public static function createFromPostData(array $data) {
    $data += [
      'fn' => '',
      'et' => '',
      'id' => '',
      'ti' => 0,
      'e' => 0,
      'h' => '',
    ];
    return new Incident($data['fn'], $data['et'], $data['id'], $data['ti'], $data['e'], $data['h']);
  }

  /**
   * Create an Incident from field items, an item within it and a formatter.
   *
   * @param object $items
   *   The items containing item.
   * @param object $item
   *   The item in question.
   * @param object $formatter
   *   The formatter in use.
   *
   * @return \Drupal\radioactivity\IncidentInterface
   *   The incident object.
   */
  public static function createFromFieldItemsAndFormatter($items, $item, $formatter) {
    return new Incident($items
      ->getName(), $item
      ->getEntity()
      ->getEntityTypeId(), $item
      ->getEntity()
      ->id(), isset($item->target_id) ? $item->target_id : 0, $formatter
      ->getSetting('energy'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFieldName() {
    return $this->fieldName;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityTypeId() {
    return $this->entityType;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityId() {
    return $this->entityId;
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetId() {
    return $this->targetId;
  }

  /**
   * {@inheritdoc}
   */
  public function getEnergy() {
    return $this->energy;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Incident::$energy private property The incident energy.
Incident::$entityId private property The incident entity id.
Incident::$entityType private property The incident entity type.
Incident::$fieldName private property The incident field name.
Incident::$hash private property The incident hash.
Incident::$targetId private property The id of the referenced radioactivity entity.
Incident::calculateHash private function Calculate hash for this incident.
Incident::createFromFieldItemsAndFormatter public static function Create an Incident from field items, an item within it and a formatter.
Incident::createFromPostData public static function Create an Incident from data received in an http request.
Incident::getEnergy public function Returns the incident energy. Overrides IncidentInterface::getEnergy
Incident::getEntityId public function Returns the incident entity id. Overrides IncidentInterface::getEntityId
Incident::getEntityTypeId public function Returns the incident entity type. Overrides IncidentInterface::getEntityTypeId
Incident::getFieldName public function Returns the incident field name. Overrides IncidentInterface::getFieldName
Incident::getTargetId public function Returns the id of the referenced radioactivity entity. Overrides IncidentInterface::getTargetId
Incident::isValid public function Test validity of the Incident. Overrides IncidentInterface::isValid
Incident::toJson public function Convert to JSON format. Overrides IncidentInterface::toJson
Incident::__construct public function Constructor.