You are here

class FancyFileDeleteBatch in Fancy File Delete 2.0.x

Class FancyFileDeleteBatch.

Hierarchy

Expanded class hierarchy of FancyFileDeleteBatch

2 files declare their use of FancyFileDeleteBatch
DeleteFilesAction.php in src/Plugin/Action/DeleteFilesAction.php
FancyFileDeleteCommands.php in src/Commands/FancyFileDeleteCommands.php
1 string reference to 'FancyFileDeleteBatch'
fancy_file_delete.services.yml in ./fancy_file_delete.services.yml
fancy_file_delete.services.yml
1 service uses FancyFileDeleteBatch
fancy_file_delete.batch in ./fancy_file_delete.services.yml
Drupal\fancy_file_delete\FancyFileDeleteBatch

File

src/FancyFileDeleteBatch.php, line 20

Namespace

Drupal\fancy_file_delete
View source
class FancyFileDeleteBatch {
  use StringTranslationTrait;
  use DependencySerializationTrait;

  /**
   * The active database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $stringTranslation;

  /**
   * The file system.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Constructs a new FancyFileDeleteBatch.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The current database connection.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   Messenger service.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system.
   */
  public function __construct(Connection $database, EntityTypeManagerInterface $entityTypeManager, MessengerInterface $messenger, TranslationInterface $string_translation, FileSystemInterface $file_system) {
    $this->database = $database;
    $this->entityTypeManager = $entityTypeManager;
    $this->messenger = $messenger;
    $this->stringTranslation = $string_translation;
    $this->fileSystem = $file_system;
  }

  /**
   * Sets the batch operations.
   *
   * @param array $values
   *   The array of values we are looking to set in the batch.
   * @param bool $force
   *   If we are forcing the delete.
   * @param bool $ui
   *   If we are running this through the UI or CLI.
   */
  public function setBatch($values, $force, $ui = TRUE) {

    // Sets up our batch.
    $batch_builder = new BatchBuilder();
    $batch_builder
      ->setTitle($this
      ->t('Deleting Files...'))
      ->setInitMessage($this
      ->t('Fun Stuff is Happening...'))
      ->setErrorMessage($this
      ->t('Fancy File Delete has encountered an error.'))
      ->setFinishCallback([
      $this,
      'finish',
    ]);
    foreach ($values as $value) {
      $batch_builder
        ->addOperation([
        $this,
        'process',
      ], [
        $value,
        $force,
      ]);
    }

    // Engage.
    batch_set($batch_builder
      ->toArray());
    if (!$ui) {
      $batch =& batch_get();
      $batch['progressive'] = FALSE;

      // Start the process.
      drush_backend_batch_process();
    }
  }

  /**
   * Batch process function.
   */
  public function process($fid, $force, &$context) {

    // Update our progress information.
    if (!isset($context['sandbox']['progress'])) {
      $context['sandbox']['progress'] = 0;
    }
    $context['sandbox']['progress']++;

    // Manual / Orphan Delete.
    if (is_numeric($fid)) {
      $file = File::load($fid);
      if ($file) {
        if ($force) {

          // Remove these from the DB.
          $this->database
            ->delete('file_managed')
            ->condition('fid', $fid)
            ->execute();
          $this->database
            ->delete('file_usage')
            ->condition('fid', $fid)
            ->execute();

          // Now Delete the file completely.
          // Skip file api and just delete the entity, quicker.
          $controller = $this->entityTypeManager
            ->getStorage('file');
          $entity = $controller
            ->loadMultiple([
            $fid,
          ]);
          $controller
            ->delete($entity);
        }
        else {
          $result = $file
            ->delete();
          if (is_array($result)) {

            // The file is still being referenced.
            // So it can not be forcefully deleted.
            // Notify the user instead.
            $context['results']['error'][] = array(
              'fid' => $fid,
              'message' => $this
                ->t('The file with fid#%fid cannot be delete because it
            is still referenced in the file_usage table. %file_usage', array(
                '%fid' => $fid,
                '%file_usage' => print_r($result, TRUE),
              )),
            );
          }
          else {
            $context['results'][] = $fid;
          }
        }
      }
    }
    else {

      // @todo fix this to be the new way.
      $this->database
        ->delete('unmanaged_files')
        ->condition('path', $fid)
        ->execute();
      $this->fileSystem
        ->delete($fid);
      $context['results'][] = $fid;
    }

    // Set the processing message.
    $context['message'] = $this
      ->t('Now cleansing the system of fid#%fid', [
      '%fid' => $fid,
    ]);
  }

  /**
   * Batch finished.
   */
  public function finish($success, $results, $operations) {
    if ($success) {

      // Reset the cache.
      $this->entityTypeManager
        ->getStorage('file')
        ->resetCache();
      $message = $this->stringTranslation
        ->formatPlural(count($results), 'One file cleansed.', '@count files cleansed.');
    }
    else {
      $message = $this
        ->t('Assimilation was futile!');
    }
    $this->messenger
      ->addMessage($message);
    return new RedirectResponse(Url::fromRoute('fancy_file_delete.info')
      ->toString());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FancyFileDeleteBatch::$database protected property The active database connection.
FancyFileDeleteBatch::$entityTypeManager protected property The entity type manager.
FancyFileDeleteBatch::$fileSystem protected property The file system.
FancyFileDeleteBatch::$messenger protected property The messenger service.
FancyFileDeleteBatch::$stringTranslation protected property The messenger service. Overrides StringTranslationTrait::$stringTranslation
FancyFileDeleteBatch::finish public function Batch finished.
FancyFileDeleteBatch::process public function Batch process function.
FancyFileDeleteBatch::setBatch public function Sets the batch operations.
FancyFileDeleteBatch::__construct public function Constructs a new FancyFileDeleteBatch.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.