Commit 2fc65eeb authored by dpi's avatar dpi
Browse files

Issue #3210844 by dpi, larowlan:...

Issue #3210844 by dpi, larowlan: `\Drupal\auditfiles\Batch\AuditFilesMergeFileReferencesBatchProcess::auditfilesMergeFileReferencesBatchMergeProcessBatch` typehints files being kept as an array, but the code is called with an int
parent b3d0235d
Loading
Loading
Loading
Loading
+21 −39
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\auditfiles\Batch;

use Drupal\Component\Utility\Html;
@@ -11,43 +13,23 @@ use Drupal\auditfiles\ServiceAuditFilesMergeFileReferences;
 *
 * @todo Refactor to make a Factory Worker class.
 */
class AuditFilesMergeFileReferencesBatchProcess {

  /**
   * The File entity ID to delete.
   *
   * @var int
   */
  protected $fileId;

  /**
   * The File entity IDs to merge.
   *
   * @var array
   */
  protected $fileIds;

  /**
   * ManagedNotUsed service.
   *
   * @var \Drupal\auditfiles\ServiceAuditFilesMergeFileReferences
   */
  protected $mergeFileReferences;
final class AuditFilesMergeFileReferencesBatchProcess {

  /**
   * Class constructor.
   * Constructs a new AuditFilesMergeFileReferencesBatchProcess.
   *
   * @param \Drupal\auditfiles\ServiceAuditFilesMergeFileReferences $merge_file_references
   *   Injected ServiceAuditFilesManagedNotUsed service.
   * @param int $file_being_kept
   *   File entity ID to delete.
   * @param array $files_being_merged
   *   File IDs to merge.
   * @param \Drupal\auditfiles\ServiceAuditFilesMergeFileReferences $mergeFileReferences
   *   Injected auditfiles.merge_file_references service.
   * @param int $fileId
   *   The File entity ID to delete.
   * @param int $mergedFileId
   *   The File entity ID to merge.
   */
  public function __construct(ServiceAuditFilesMergeFileReferences $merge_file_references, $file_being_kept, array $files_being_merged) {
    $this->mergeFileReferences = $merge_file_references;
    $this->fileId = $file_being_kept;
    $this->fileIds = $files_being_merged;
  protected function __construct(
    protected ServiceAuditFilesMergeFileReferences $mergeFileReferences,
    protected int $fileId,
    protected int $mergedFileId,
  ) {
  }

  /**
@@ -55,15 +37,15 @@ class AuditFilesMergeFileReferencesBatchProcess {
   *
   * @param int $file_being_kept
   *   The file ID of the file to merge the other into.
   * @param array $files_being_merged
   * @param int $file_being_merged
   *   The file ID of the files to merge.
   * @param array $context
   *   Used by the Batch API to keep track of and pass data from one operation
   *   to the next.
   */
  public static function auditfilesMergeFileReferencesBatchMergeProcessBatch($file_being_kept, array $files_being_merged, array &$context) {
  public static function auditfilesMergeFileReferencesBatchMergeProcessBatch(int $file_being_kept, int $file_being_merged, array &$context) {
    $mergeFileReferences = \Drupal::service('auditfiles.merge_file_references');
    $worker = new static($mergeFileReferences, $file_being_kept, $files_being_merged);
    $worker = new static($mergeFileReferences, $file_being_kept, $file_being_merged);
    $worker->dispatch($context);
  }

@@ -74,14 +56,14 @@ class AuditFilesMergeFileReferencesBatchProcess {
   *   Batch context.
   */
  protected function dispatch(array &$context) {
    $this->mergeFileReferences->auditfilesMergeFileReferencesBatchMergeProcessFile($this->fileId, $this->fileIds);
    $this->mergeFileReferences->auditfilesMergeFileReferencesBatchMergeProcessFile($this->fileId, $this->mergedFileId);
    $context['results'][] = Html::escape($this->fileId);
    $context['results'][] = Html::escape($this->filesIds);
    $context['results'][] = Html::escape($this->mergedFileId);
    $context['message'] = new TranslatableMarkup(
      'Merged file ID %file_being_merged into file ID %file_being_kept.',
      [
        '%file_being_kept' => $this->fileId,
        '%file_being_merged' => $this->fileIds,
        '%file_being_merged' => $this->mergedFileId,
      ]
    );
  }
+6 −7
Original line number Diff line number Diff line
@@ -303,13 +303,12 @@ class AuditFilesMergeFileReferences extends FormBase implements ConfirmFormInter
      }

    }
    $file_names = $this->mergeFileReferences->auditfilesMergeFileReferencesGetFileList();
    if (!empty($file_names)) {
      $date_format = $config->get('auditfiles_report_options_date_format');
      foreach ($file_names as $file_name) {
        $rows[$file_name] = $this->mergeFileReferences->auditfilesMergeFileReferencesGetFileData($file_name, $date_format);
      }
    }
    $fileNames = $this->mergeFileReferences->auditfilesMergeFileReferencesGetFileList();
    $dateFormat = $config->get('auditfiles_report_options_date_format');
    $rows = array_reduce($fileNames, function (?array $rows, string $fileName) use ($dateFormat): array {
      $rows[$fileName] = $this->mergeFileReferences->auditfilesMergeFileReferencesGetFileData($fileName, $dateFormat);
      return $rows;
    });
    // Set up the pager.
    if (!empty($rows)) {
      $items_per_page = $config->get('auditfiles_report_options_items_per_page');
+15 −16
Original line number Diff line number Diff line
@@ -2,6 +2,9 @@

namespace Drupal\auditfiles;

use Drupal\auditfiles\Batch\AuditFilesBatchProcess;
use Drupal\auditfiles\Batch\AuditFilesMergeFileReferencesBatchProcess;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
@@ -76,7 +79,7 @@ class ServiceAuditFilesMergeFileReferences {
  /**
   * Retrieves the file IDs to operate on.
   *
   * @return array
   * @return string[]
   *   The file IDs.
   */
  public function auditfilesMergeFileReferencesGetFileList() {
@@ -193,24 +196,20 @@ class ServiceAuditFilesMergeFileReferences {
   *   The definition of the batch.
   */
  public function auditfilesMergeFileReferencesBatchMergeCreateBatch($file_being_kept, array $files_being_merged) {
    $batch['error_message'] = $this->stringTranslation->translate('One or more errors were encountered processing the files.');
    $batch['finished'] = '\Drupal\auditfiles\Batch\AuditFilesBatchProcess::finishBatch';
    $batch['progress_message'] = $this->stringTranslation->translate('Completed @current of @total operations.');
    $batch['title'] = $this->stringTranslation->translate('Merging files');
    $operations = [];
    $batch = (new BatchBuilder())
      ->setErrorMessage(\t('One or more errors were encountered processing the files.'))
      ->setFinishCallback([AuditFilesBatchProcess::class, 'finishBatch'])
      ->setProgressMessage('Completed @current of @total operations.')
      ->setTitle('Merging files');
    foreach ($files_being_merged as $file_id => $file_info) {
      if ($file_id != 0 && $file_id != $file_being_kept) {
        $operations[] = [
          '\Drupal\auditfiles\Batch\AuditFilesMergeFileReferencesBatchProcess::auditfilesMergeFileReferencesBatchMergeProcessBatch',
          [
            $file_being_kept,
            $file_id,
          ],
        ];
        $batch->addOperation([AuditFilesMergeFileReferencesBatchProcess::class, 'auditfilesMergeFileReferencesBatchMergeProcessBatch'], [
          (int) $file_being_kept,
          (int) $file_id,
        ]);
      }
    }
    $batch['operations'] = $operations;
    return $batch;
    return $batch->toArray();
  }

  /**
@@ -221,7 +220,7 @@ class ServiceAuditFilesMergeFileReferences {
   * @param int $file_being_merged
   *   The file ID of the file to merge.
   */
  public function auditfilesMergeFileReferencesBatchMergeProcessFile($file_being_kept, $file_being_merged) {
  public function auditfilesMergeFileReferencesBatchMergeProcessFile(int $file_being_kept, int $file_being_merged) {
    $connection = $this->connection;
    $file_being_kept_results = $connection->select('file_usage', 'fu')
      ->fields('fu', ['module', 'type', 'id', 'count'])