You are here

public function GdprTasksSarWorker::compile in General Data Protection Regulation 8

Same name and namespace in other branches
  1. 8.2 modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php \Drupal\gdpr_tasks\Plugin\QueueWorker\GdprTasksSarWorker::compile()
  2. 3.0.x modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php \Drupal\gdpr_tasks\Plugin\QueueWorker\GdprTasksSarWorker::compile()

Compile the SAR into a downloadable zip.

Parameters

\Drupal\gdpr_tasks\Entity\TaskInterface $task: The task.

1 call to GdprTasksSarWorker::compile()
GdprTasksSarWorker::processItem in modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php
Works on a single queue item.

File

modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php, line 340

Class

GdprTasksSarWorker
Processes SARs tasks when data processing is required.

Namespace

Drupal\gdpr_tasks\Plugin\QueueWorker

Code

public function compile(TaskInterface $task) {

  // Compile all files into a single zip.

  /* @var \Drupal\file\Entity\File $file */
  $file = $task->sar_export->entity;
  if (NULL === $file) {
    $this->messenger
      ->addError(t('SARs Export File not found for task @task_id.', [
      '@task_id' => $task
        ->id(),
    ]));
    return;
  }
  $file_path = $this->fileSystem
    ->realpath($file->uri->value);
  $zip = new \ZipArchive();
  if (!$zip
    ->open($file_path, \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) {

    // @todo: Improve error handling.
    $this->messenger
      ->addError(t('Error opening file.'));
    return;
  }

  // Gather all the files we need to include in this package.
  $part_files = [];
  foreach ($task->sar_export_parts as $item) {

    /* @var \Drupal\file\Entity\File $part_file */
    $part_file = $item->entity;
    $part_files[] = $part_file;

    // Add the file to the zip.
    // @todo: Add error handling.
    $zip
      ->addFile($this->fileSystem
      ->realpath($part_file->uri->value), $part_file->filename->value);
  }

  // Add in any attached files that need including.
  foreach ($task->sar_export_assets as $item) {
    $asset_file = $item->entity;

    // Add the file to the zip.
    $filename = "assets/{$asset_file->fid->value}." . pathinfo($asset_file->uri->value, PATHINFO_EXTENSION);

    // @todo: Add error handling.
    $zip
      ->addFile($this->fileSystem
      ->realpath($asset_file->uri->value), $filename);
  }

  // Clear our parts and assets file lists.
  $task->sar_export_parts = NULL;
  $task->sar_export_assets = NULL;

  // Close the zip to write it to disk.
  // @todo: Add error handling.
  $zip
    ->close();

  // Save the file to update the file size.
  $file
    ->save();

  // Remove the partial files.
  foreach ($part_files as $part_file) {
    $part_file
      ->delete();
  }

  // @todo Clean up the parts directory.
  // Update the status as completed.
  $task->status = 'closed';
  $task
    ->save();
}