You are here

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

Same name and namespace in other branches
  1. 8 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.

Throws

\Drupal\Core\Entity\EntityStorageException

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 367

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($this
      ->t('SARs Export File not found for task @task_id.', [
      '@task_id' => $task
        ->id(),
    ]));
    return;
  }
  $filePath = $this->fileSystem
    ->realpath($file->uri->value);
  $zip = new ZipArchive();
  if (!$zip
    ->open($filePath, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {

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

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

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

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

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

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

    // @todo: Add error handling.
    $zip
      ->addFile($this->fileSystem
      ->realpath($assetFile->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 ($partFiles as $partFile) {
    $partFile
      ->delete();
  }

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