You are here

protected function GdprTasksSarWorker::compile in General Data Protection Regulation 7

Compile the SAR into a downloadable zip.

Parameters

\GDPRTask $task: The task.

1 call to GdprTasksSarWorker::compile()
GdprTasksSarWorker::processItem in modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php
Process the SARs request.

File

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

Class

GdprTasksSarWorker
Queue worker callback for processing SARs requests.

Code

protected function compile(\GDPRTask $task) {

  // Compile all files into a single zip.
  $wrapper = $task
    ->wrapper();
  $file = $wrapper->gdpr_tasks_sar_export->file;
  $file_path = drupal_realpath($file
    ->value()->uri);
  $zip = new ZipArchive();
  if (!$zip
    ->open($file_path, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {

    // @todo: Improve error handling.
    drupal_set_message(t('Error opening file.'), 'error');
    return;
  }

  // Gather all the files we need to include in this package.
  $part_files = array();
  foreach ($wrapper->gdpr_tasks_sar_export_parts as $item) {
    $part_file = $item->file
      ->value();
    $part_files[] = $part_file;

    // Re-write the file to remove the header.
    $data = $this
      ->readCsv($part_file->uri);
    array_shift($data);
    $this
      ->writeCsv($part_file->uri, $data);
    file_save($part_file);

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

  // Add in any attached files that need including.
  foreach ($wrapper->gdpr_tasks_sar_export_assets as $item) {
    $asset_file = $item->file
      ->value();

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

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

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

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

  // Make sure the user owns the compiled zip file.
  $file
    ->value()->uid = $task
    ->getOwner()->uid;

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

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

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

  // Send confirmation email.
  gdpr_tasks_send_mail('task_processed', $task);
}