You are here

public function Anonymizer::run in General Data Protection Regulation 8

Same name and namespace in other branches
  1. 8.2 modules/gdpr_tasks/src/Anonymizer.php \Drupal\gdpr_tasks\Anonymizer::run()
  2. 3.0.x modules/gdpr_tasks/src/Anonymizer.php \Drupal\gdpr_tasks\Anonymizer::run()

Runs anonymization routines against a user.

Parameters

\Drupal\gdpr_tasks\Entity\TaskInterface $task: The current task being executed.

Return value

array Returns array containing any error messages.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\TypedData\Exception\ReadOnlyException

File

modules/gdpr_tasks/src/Anonymizer.php, line 100

Class

Anonymizer
Anonymizes or removes field values for GDPR.

Namespace

Drupal\gdpr_tasks

Code

public function run(TaskInterface $task) {

  // Make sure we load a fresh copy of the entity (bypassing the cache)
  // so we don't end up affecting any other references to the entity.
  $user = $task
    ->getOwner();
  $errors = [];
  if (!$this
    ->checkExportDirectoryExists()) {
    $errors[] = $this
      ->t('An export directory has not been set. Please set this %link.', [
      '%link' => Link::fromTextAndUrl('here', Url::fromRoute('gdpr_tasks.remove_settings'))
        ->toString(),
    ]);
    return $errors;
  }

  // Traverser does the actual anonymizing.
  $traverser = $this->traversalFactory
    ->getTraversal($user);
  $result = $traverser
    ->getResults();
  $log = $result['log'];
  $errors = $result['errors'];
  $successes = $result['successes'];
  $failures = $result['failures'];
  $deletions = $result['to_delete'];
  $task
    ->get('removal_log')
    ->setValue($log);
  if (count($failures) === 0) {
    $transaction = $this->database
      ->startTransaction();
    try {

      /* @var \Drupal\Core\Entity\EntityInterface $entity */
      foreach ($successes as $entity) {
        $entity
          ->save();
      }
      foreach ($deletions as $entity) {
        $entity
          ->delete();
      }

      // Re-fetch the user so we see any changes that were made.
      $user = $this
        ->refetchUser($task
        ->getOwnerId());
      $user
        ->block();
      $user
        ->save();
      $this
        ->writeLogToFile($task, $log);
    } catch (\Exception $e) {
      $transaction
        ->rollBack();
      $errors[] = $e
        ->getMessage();
    }
  }
  return $errors;
}