You are here

class InvoicePrintBuilder in Commerce Invoice 8.2

The print builder service.

Hierarchy

Expanded class hierarchy of InvoicePrintBuilder

1 string reference to 'InvoicePrintBuilder'
commerce_invoice.services.yml in ./commerce_invoice.services.yml
commerce_invoice.services.yml
1 service uses InvoicePrintBuilder
commerce_invoice.print_builder in ./commerce_invoice.services.yml
Drupal\commerce_invoice\InvoicePrintBuilder

File

src/InvoicePrintBuilder.php, line 19

Namespace

Drupal\commerce_invoice
View source
class InvoicePrintBuilder implements InvoicePrintBuilderInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The entity storage for the 'file' entity type.
   *
   * @var \Drupal\file\FileStorageInterface
   */
  protected $fileStorage;

  /**
   * The Entity print builder.
   *
   * @var \Drupal\entity_print\PrintBuilderInterface
   */
  protected $printBuilder;

  /**
   * The Entity print filename generator.
   *
   * @var \Drupal\entity_print\FilenameGeneratorInterface
   */
  protected $filenameGenerator;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * The Current User object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Constructs a new InvoicePrintBuilder object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\entity_print\PrintBuilderInterface $print_builder
   *   The Entity print builder.
   * @param \Drupal\entity_print\FilenameGeneratorInterface $filename_generator
   *   The Entity print filename generator.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, PrintBuilderInterface $print_builder, FilenameGeneratorInterface $filename_generator, EventDispatcherInterface $event_dispatcher, AccountInterface $current_user) {
    $this->configFactory = $config_factory;
    $this->fileStorage = $entity_type_manager
      ->getStorage('file');
    $this->printBuilder = $print_builder;
    $this->filenameGenerator = $filename_generator;
    $this->eventDispatcher = $event_dispatcher;
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public function generateFilename(InvoiceInterface $invoice) {
    $filename = $this->filenameGenerator
      ->generateFilename([
      $invoice,
    ]);
    $filename .= '-' . $invoice
      ->language()
      ->getId() . '-' . str_replace('_', '', $invoice
      ->getState()
      ->getId());

    // Let the filename be altered.
    $event = new InvoiceFilenameEvent($filename, $invoice);
    $this->eventDispatcher
      ->dispatch(InvoiceEvents::INVOICE_FILENAME, $event);
    $filename = $event
      ->getFilename() . '.pdf';
    return $filename;
  }

  /**
   * {@inheritdoc}
   */
  public function savePrintable(InvoiceInterface $invoice, PrintEngineInterface $print_engine, $scheme = 'private') {
    $filename = $this
      ->generateFilename($invoice);
    $config = $this->configFactory
      ->get('entity_print.settings');
    $uri = $this->printBuilder
      ->savePrintable([
      $invoice,
    ], $print_engine, $scheme, $filename, $config
      ->get('default_css'));
    if (!$uri) {
      return FALSE;
    }
    $file = $this->fileStorage
      ->create([
      'uri' => $uri,
      'uid' => $this->currentUser
        ->id(),
      'langcode' => $invoice
        ->language()
        ->getId(),
      'status' => FILE_STATUS_PERMANENT,
    ]);
    $file
      ->save();
    return $file;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InvoicePrintBuilder::$configFactory protected property The config factory.
InvoicePrintBuilder::$currentUser protected property The Current User object.
InvoicePrintBuilder::$eventDispatcher protected property The event dispatcher.
InvoicePrintBuilder::$filenameGenerator protected property The Entity print filename generator.
InvoicePrintBuilder::$fileStorage protected property The entity storage for the 'file' entity type.
InvoicePrintBuilder::$printBuilder protected property The Entity print builder.
InvoicePrintBuilder::generateFilename public function Generates a filename for the given invoice. Overrides InvoicePrintBuilderInterface::generateFilename
InvoicePrintBuilder::savePrintable public function Renders the invoice as a printed document and save to disk. Overrides InvoicePrintBuilderInterface::savePrintable
InvoicePrintBuilder::__construct public function Constructs a new InvoicePrintBuilder object.