Skip to content

Commit

Permalink
Use package exception
Browse files Browse the repository at this point in the history
Throw an exception if PDFLib is not available
  • Loading branch information
8ctopus committed May 29, 2024
1 parent 36c7eea commit 914119c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use DateTime;
use Dompdf\Dompdf;
use Exception;
use Locale;
use stdClass;
use Twig\Environment;
Expand Down Expand Up @@ -122,14 +121,22 @@ public function renderHtml() : string
*
* @return string
*
* @throws Exception
* @throws InvoiceException
*/
public function renderPdf(array $options = []) : string
{
if (($options['engine'] ?? '') === 'wkhtmltopdf') {
$engine = $options['engine'] ?? '';

if ($engine === 'wkhtmltopdf') {
return $this->renderWK($options);
}

if ($engine === 'PDFLib') {
if (!extension_loaded('PDFLib')) {
throw new InvoiceException('PDFLib extension not found');
}
}

if ($options['debug'] ?? null) {
global $_dompdf_show_warnings, $_dompdf_debug, $_DOMPDF_DEBUG_TYPES;

Expand Down Expand Up @@ -201,7 +208,7 @@ public function renderPdf(array $options = []) : string
return $output;
}

throw new Exception('dompdf output');
throw new InvoiceException('dompdf output');
}

public function seller() : ?Entity
Expand All @@ -225,7 +232,7 @@ public function items() : array
public function date() : DateTime
{
if (!isset($this->date)) {
throw new Exception('date not set');
throw new InvoiceException('date not set');
}

return $this->date;
Expand Down Expand Up @@ -268,7 +275,7 @@ public function total() : float
$total = $this->subtotal() + $this->shipping?->price() - $this->discount?->price() + $this->taxAmount();

if ($total <= 0) {
throw new Exception('invoice total <= 0');
throw new InvoiceException('invoice total <= 0');
}

return $total;
Expand Down Expand Up @@ -391,7 +398,7 @@ private function renderWK(array $options) : string
}

if (!file_exists($exe)) {
throw new Exception('cannot find wkhtmltopdf executable');
throw new InvoiceException('cannot find wkhtmltopdf executable');
}

$descriptorSpec = [
Expand All @@ -407,7 +414,7 @@ private function renderWK(array $options) : string
$process = proc_open("{$exe} --enable-local-file-access --page-size {$options['paper']} --orientation {$options['orientation']} - -", $descriptorSpec, $pipes, null, null, null);

if (!is_resource($process)) {
throw new Exception('wkhtmltopdf open process');
throw new InvoiceException('wkhtmltopdf open process');
}

// write input data
Expand All @@ -427,6 +434,6 @@ private function renderWK(array $options) : string
return $pdf;
}

throw new Exception('wkhtmltopdf process return value');
throw new InvoiceException('wkhtmltopdf process return value');
}
}
11 changes: 11 additions & 0 deletions src/InvoiceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Oct8pus\Invoice;

use Exception;

class InvoiceException extends Exception
{
}

0 comments on commit 914119c

Please sign in to comment.