You are here

protected function GdprTasksSarWorker::initialise in General Data Protection Regulation 3.0.x

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. 8 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 196

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 (!$this->fileSystem
    ->prepareDirectory($directory, FileSystemInterface::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", FileSystemInterface::EXISTS_ERROR);
  } while (!$file);

  // Prepare the directory for our sub-files.
  $content_directory = "{$directory}/{$uuid}";
  $this->fileSystem
    ->prepareDirectory($content_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::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());
  }
}