You are here

public function OrderAnonymize::anonymizeEntities in Commerce Bulk 8

1 call to OrderAnonymize::anonymizeEntities()
OrderAnonymize::submitConfigurationForm in src/Plugin/Action/OrderAnonymize.php
Form submission handler.

File

src/Plugin/Action/OrderAnonymize.php, line 114

Class

OrderAnonymize
Delete terms.

Namespace

Drupal\commerce_bulk\Plugin\Action

Code

public function anonymizeEntities(array $entities, array $fields, int $age = 0) {
  $age = $age ? $age * 24 * 60 * 60 : $age;
  $completed = \time() - $age;
  $time = current($entities) instanceof OrderInterface ? 'Completed' : 'Changed';
  foreach ($entities as $entity) {
    if ($age && $entity
      ->{'get' . $time . 'Time'}() > $completed) {
      continue;
    }
    $save = FALSE;
    foreach ($fields as $name) {
      if (!$entity
        ->hasField($name)) {
        continue;
      }
      $item = $entity
        ->get($name);
      if ($value = $item
        ->getValue()) {
        $save = TRUE;
        $this
          ->anonymizeData($value);
        $entity
          ->set($name, $value);
      }
    }
    $save && $entity
      ->save();
  }
}