You are here

protected function GdprTasksSarWorker::initialise 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::initialise()
  2. 3.0.x modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php \Drupal\gdpr_tasks\Plugin\QueueWorker\GdprTasksSarWorker::initialise()

Initialise our request.

Parameters

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

bool $build_now: Whether to build the entity data immediate or defer to cron.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 call to GdprTasksSarWorker::initialise()
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 175

Class

GdprTasksSarWorker
Processes SARs tasks when data processing is required.

Namespace

Drupal\gdpr_tasks\Plugin\QueueWorker

Code

protected function initialise(TaskInterface $task, $build_now = FALSE) {

  /* @var \Drupal\file\Plugin\Field\FieldType\FileFieldItemList $field */
  $field = $task
    ->get('sar_export');

  /* @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $field
    ->getFieldDefinition();
  $settings = $field_definition
    ->getSettings();
  $config = [
    'field_definition' => $field_definition,
    'name' => $field
      ->getName(),
    'parent' => $field
      ->getParent(),
  ];

  /* @var \Drupal\file\Plugin\Field\FieldType\FileItem $field_type */
  $field_type = $this->fieldTypePluginManager
    ->createInstance($field_definition
    ->getType(), $config);

  // Prepare destination.
  $directory = $field_type
    ->getUploadLocation();
  if (!\file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
    throw new \RuntimeException('GDPR SARs upload directory is not writable.');
  }

  // Get a suitable namespace for gathering our files.
  do {

    // Generate a UUID.
    $uuid = $this->uuid
      ->generate();

    // Check neither the file exists nor the directory.
    if (file_exists("{$directory}/{$uuid}.zip") || file_exists("{$directory}/{$uuid}/")) {
      continue;
    }

    // Generate the zip file to reserve our namespace.
    $file = _gdpr_tasks_file_save_data('', $task
      ->getOwner(), "{$directory}/{$uuid}.zip", FILE_EXISTS_ERROR);
  } while (!$file);

  // Prepare the directory for our sub-files.
  $content_directory = "{$directory}/{$uuid}";
  file_prepare_directory($content_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);

  // Store the file against the task.
  $values = [
    'target_id' => $file
      ->id(),
    'display' => (int) $settings['display_default'],
    'description' => '',
  ];
  $task->sar_export = $values;
  $task->status = 'building';
  $task
    ->save();

  // Start the build process.
  if ($build_now) {
    $this
      ->build($task);
  }
  else {

    // Queue for building.
    $this->queue
      ->createQueue();
    $this->queue
      ->createItem($task
      ->id());
  }
}