You are here

Incident.php in Radioactivity 8.3

Same filename and directory in other branches
  1. 8.2 src/Incident.php
  2. 4.0.x src/Incident.php

File

src/Incident.php
View source
<?php

namespace Drupal\radioactivity;

use Drupal\Core\Site\Settings;
use Drupal\Component\Serialization\Json;

/**
 * Data class for Radioactivity Incident.
 *
 * @package Drupal\radioactivity
 */
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 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 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, $energy, $hash = NULL) {
    $this->fieldName = $field_name;
    $this->entityType = $entity_type;
    $this->entityId = $entity_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->energy,
      Settings::getHashSalt(),
    ]));
  }

  /**
   * {@inheritdoc}
   */
  public function toJson() {
    return Json::encode([
      'fn' => $this->fieldName,
      'et' => $this->entityType,
      'id' => $this->entityId,
      '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' => '',
      'e' => 0,
      'h' => '',
    ];
    return new Incident($data['fn'], $data['et'], $data['id'], $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(), $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 getEnergy() {
    return $this->energy;
  }

}

Classes

Namesort descending Description
Incident Data class for Radioactivity Incident.