You are here

public function RadioactivityProcessor::queueProcessDecay in Radioactivity 4.0.x

Same name and namespace in other branches
  1. 8.3 src/RadioactivityProcessor.php \Drupal\radioactivity\RadioactivityProcessor::queueProcessDecay()

Queue processing of Radioactivity decays.

Parameters

\Drupal\field\FieldStorageConfigInterface $fieldConfig: Configuration of the field to be processed.

array $entityIds: Entity IDs to be processed.

Overrides RadioactivityProcessorInterface::queueProcessDecay

File

src/RadioactivityProcessor.php, line 284

Class

RadioactivityProcessor
Processes Radioactivity incidents and and energy decay.

Namespace

Drupal\radioactivity

Code

public function queueProcessDecay(FieldStorageConfigInterface $fieldConfig, array $entityIds) {
  $entityType = $fieldConfig
    ->getTargetEntityTypeId();
  $fieldName = $fieldConfig
    ->get('field_name');
  $profile = $fieldConfig
    ->getSetting('profile');
  $halfLife = $fieldConfig
    ->getSetting('halflife');
  $cutoff = $fieldConfig
    ->getSetting('cutoff');

  /** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */
  $entities = $this->entityTypeManager
    ->getStorage($entityType)
    ->loadMultiple($entityIds);
  foreach ($entities as $entity) {
    $languages = $entity
      ->getTranslationLanguages();
    foreach ($languages as $language) {
      $entity = $entity
        ->getTranslation($language
        ->getId());

      // Do not decay energy for unpublished entities.
      if ($entity instanceof EntityPublishedInterface && !$entity
        ->isPublished()) {
        continue;
      }
      $field = $entity
        ->get($fieldName);
      $isReference = $field
        ->getFieldDefinition()
        ->getType() === 'radioactivity_reference';
      if ($isReference) {

        /** @var \Drupal\radioactivity\RadioactivityInterface $radioactivityEntity */
        $radioactivityEntity = $field->entity;
        if ($radioactivityEntity) {
          $newEnergy = $this
            ->calculateEnergy($radioactivityEntity
            ->getEnergy(), $radioactivityEntity
            ->getTimestamp(), $profile, $halfLife);
          if ($this
            ->energyBelowCutoff($newEnergy, $cutoff)) {
            $newEnergy = 0;
            $this
              ->dispatchBelowCutoffEvent($entity);
          }
          $radioactivityEntity
            ->setEnergy($newEnergy);
          $radioactivityEntity
            ->setTimestamp($this->requestTime);
          $radioactivityEntity
            ->save();
        }
      }
      else {
        $newEnergy = $this
          ->calculateEnergy($field->energy, $field->timestamp, $profile, $halfLife);
        if ($this
          ->energyBelowCutoff($newEnergy, $cutoff)) {
          $newEnergy = 0;
          $this
            ->dispatchBelowCutoffEvent($entity);
        }

        // Set the new energy level and update the timestamp.
        $entity
          ->get($fieldName)
          ->setValue([
          'energy' => $newEnergy,
          'timestamp' => $this->requestTime,
        ]);
        if ($entity
          ->getEntityType()
          ->isRevisionable()) {
          $entity
            ->setNewRevision(FALSE);
        }

        // Set flag so we can identify this entity save as one that just
        // updates the radioactivity value.
        $entity->radioactivityUpdate = TRUE;
        $entity
          ->save();
      }
    }
  }
}