You are here

protected function InvoiceFileManager::loadExistingFile in Commerce Invoice 8.2

Load an existing generated PDF file for the given invoice if it exist.

Return value

\Drupal\file\FileInterface|null The invoice file, NULL if no matching invoice file was found or if it does not exist.

1 call to InvoiceFileManager::loadExistingFile()
InvoiceFileManager::getInvoiceFile in src/InvoiceFileManager.php
Download an invoice file.

File

src/InvoiceFileManager.php, line 115

Class

InvoiceFileManager
Default implementation of the invoice file manager.

Namespace

Drupal\commerce_invoice

Code

protected function loadExistingFile(InvoiceInterface $invoice) {

  /** @var \Drupal\File\FileStorageInterface $file_storage */
  $file_storage = $this->entityTypeManager
    ->getStorage('file');

  // In case the invoice doesn't reference a file, fallback to loading a
  // file matching the given filename.
  $filename = $this->printBuilder
    ->generateFilename($invoice);
  $langcode = $invoice
    ->language()
    ->getId();
  $files = $file_storage
    ->loadByProperties([
    'uri' => "private://{$filename}",
    'langcode' => $langcode,
  ]);
  if (!$files) {
    return NULL;
  }

  /** @var \Drupal\File\FileInterface $file */
  $file = $file_storage
    ->load(key($files));
  if (!file_exists($file
    ->getFileUri())) {
    $file
      ->delete();
    return NULL;
  }
  return $file;
}