You are here

public function InvoiceFileManager::getInvoiceFile in Commerce Invoice 8.2

Download an invoice file.

Parameters

\Drupal\commerce_invoice\Entity\InvoiceInterface $invoice: The invoice.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Thrown when the file was not found and could not be generated.

Overrides InvoiceFileManagerInterface::getInvoiceFile

File

src/InvoiceFileManager.php, line 61

Class

InvoiceFileManager
Default implementation of the invoice file manager.

Namespace

Drupal\commerce_invoice

Code

public function getInvoiceFile(InvoiceInterface $invoice) {
  if ($invoice
    ->getFile()) {
    return $invoice
      ->getFile();
  }

  // Check if an invoice was already generated for the given invoice,
  // that is not referenced by the invoice.
  $file = $this
    ->loadExistingFile($invoice);

  // If the invoice file hasn't been generated yet, generate it.
  if (!$file) {
    $file = $this
      ->generateInvoiceFile($invoice);
  }
  if (!$file) {
    throw new NotFoundHttpException();
  }

  // Sets the PDF file reference field on the invoice.
  if (!$invoice
    ->getFile() || $invoice
    ->getFile()
    ->id() !== $file
    ->id()) {
    $invoice
      ->setFile($file);
    $invoice
      ->save();
  }
  return $file;
}