From 1e91990a28c4404f42d02f30a70d36f53b7e2c16 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 19 Sep 2023 23:32:55 +0200 Subject: [PATCH] handle unset order in order based document types --- .../AbstractClasses/pdfdocumentsGeneric.php | 13 ++++++- .../AbstractClasses/pdfdocumentsOrder.php | 37 ++++++++++--------- Application/Model/Documents/invoicePdf.php | 20 +++++++++- .../Exceptions/noBaseObjectSetException.php | 21 ----------- .../pdfdocumentsGenericInterface.php | 3 -- .../Interfaces/pdfdocumentsOrderInterface.php | 2 + .../tpl/documents/inc/elements/foldmarks.tpl | 2 +- .../views/tpl/documents/inc/helper/rulers.tpl | 2 +- .../views/tpl/documents/inc/page/base.tpl | 2 +- .../d3_overview_controller_pdfdocuments.php | 7 +--- README.md | 2 +- composer.json | 3 +- metadata.php | 2 +- 13 files changed, 61 insertions(+), 55 deletions(-) delete mode 100644 Application/Model/Exceptions/noBaseObjectSetException.php diff --git a/Application/Model/AbstractClasses/pdfdocumentsGeneric.php b/Application/Model/AbstractClasses/pdfdocumentsGeneric.php index 110d277..2d0a463 100644 --- a/Application/Model/AbstractClasses/pdfdocumentsGeneric.php +++ b/Application/Model/AbstractClasses/pdfdocumentsGeneric.php @@ -10,6 +10,7 @@ namespace D3\PdfDocuments\Application\Model\AbstractClasses; +use Assert\InvalidArgumentException; use D3\ModCfg\Application\Model\d3filesystem; use D3\PdfDocuments\Application\Model\Exceptions\pdfGeneratorExceptionAbstract; use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsGenericInterface as genericInterface; @@ -94,6 +95,9 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface } catch (pdfGeneratorExceptionAbstract $e) { Registry::get(UtilsView::class)->addErrorToDisplay($e); Registry::getLogger()->error($e); + } catch (InvalidArgumentException $e) { + Registry::get(UtilsView::class)->addErrorToDisplay($e); + Registry::getLogger()->error($e); } } @@ -117,6 +121,9 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface } catch (pdfGeneratorExceptionAbstract $e) { Registry::get(UtilsView::class)->addErrorToDisplay($e); Registry::getLogger()->error($e); + } catch (InvalidArgumentException $e) { + Registry::get(UtilsView::class)->addErrorToDisplay($e); + Registry::getLogger()->error($e); } } @@ -137,6 +144,9 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface } catch (pdfGeneratorExceptionAbstract $e) { Registry::get(UtilsView::class)->addErrorToDisplay($e); Registry::getLogger()->error($e); + } catch (InvalidArgumentException $e) { + Registry::get(UtilsView::class)->addErrorToDisplay($e); + Registry::getLogger()->error($e); } return null; @@ -159,6 +169,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface * @param int $iSelLang * * @return mixed + * @throws InvalidArgumentException */ public function getHTMLContent($iSelLang = 0) { @@ -242,7 +253,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface { $extension = $this->filenameExtension; $extensionLength = (strlen($extension) + 1) * -1; - if ((bool) strlen($extension) && substr($filename, $extensionLength) != '.'.$extension) { + if (strlen($extension) && substr($filename, $extensionLength) != '.'.$extension) { $filename .= '.'.$extension; } diff --git a/Application/Model/AbstractClasses/pdfdocumentsOrder.php b/Application/Model/AbstractClasses/pdfdocumentsOrder.php index 98235b9..491e867 100644 --- a/Application/Model/AbstractClasses/pdfdocumentsOrder.php +++ b/Application/Model/AbstractClasses/pdfdocumentsOrder.php @@ -10,9 +10,10 @@ namespace D3\PdfDocuments\Application\Model\AbstractClasses; -use D3\PdfDocuments\Application\Model\Exceptions\noBaseObjectSetException; +use Assert\Assert; +use Assert\InvalidArgumentException; use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsOrderInterface as orderInterface; -use \OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\Payment; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Registry; @@ -22,6 +23,14 @@ abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInt /** @var Order */ public $oOrder; + /** + * don't use order as constructor argument because of same method interface for all document types + */ + public function __construct() + { + parent::__construct(); + } + /** * @param Order $order */ @@ -31,15 +40,22 @@ abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInt } /** + * @throws InvalidArgumentException * @return Order */ public function getOrder() { + Assert::lazy() + ->that($this->oOrder)->isInstanceOf(Order::class, 'no order for pdf generator set') + ->that($this->oOrder->isLoaded())->true('given order is not loaded') + ->verifyNow(); + return $this->oOrder; } /** * @param int $iSelLang + * @throws InvalidArgumentException */ public function setSmartyVars($iSelLang) { @@ -58,6 +74,7 @@ abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInt /** * @return string + * @throws InvalidArgumentException */ public function getFilename() { @@ -86,21 +103,6 @@ abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInt ); } - /** - * @param $sFilename - * @param int $iSelLang - * @param string $target - * @return mixed|string|null - */ - public function genPdf($sFilename, $iSelLang = 0, $target = 'I') - { - if (false == $this->getOrder()) { - throw oxNew(noBaseObjectSetException::class); - } - - return parent::genPdf($sFilename, $iSelLang, $target); - } - /** * @return int */ @@ -115,6 +117,7 @@ abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInt /** * @return false|string + * @throws InvalidArgumentException */ public function getPayableUntilDate() { diff --git a/Application/Model/Documents/invoicePdf.php b/Application/Model/Documents/invoicePdf.php index e8d32a4..599c821 100644 --- a/Application/Model/Documents/invoicePdf.php +++ b/Application/Model/Documents/invoicePdf.php @@ -10,10 +10,9 @@ namespace D3\PdfDocuments\Application\Model\Documents; +use Assert\InvalidArgumentException; use D3\PdfDocuments\Application\Model\AbstractClasses\pdfdocumentsOrder; -use D3\PdfDocuments\Application\Model\Exceptions\noBaseObjectSetException; use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsOrderinvoiceInterface; -use Spipu\Html2Pdf\Exception\Html2PdfException; class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceInterface { @@ -43,6 +42,10 @@ class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceIn return 'invoice'; } + /** + * @return void + * @throws InvalidArgumentException + */ public function runPreAction() { parent::runPreAction(); @@ -52,6 +55,10 @@ class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceIn $this->saveOrderOnChanges(); } + /** + * @return void + * @throws InvalidArgumentException + */ public function setInvoiceNumber() { if (!$this->getOrder()->getFieldData('oxbillnr')) { @@ -61,6 +68,10 @@ class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceIn } } + /** + * @return void + * @throws InvalidArgumentException + */ public function setInvoiceDate() { if ($this->getOrder()->getFieldData('oxbilldate') == '0000-00-00') { @@ -72,6 +83,10 @@ class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceIn } } + /** + * @return void + * @throws InvalidArgumentException + */ public function saveOrderOnChanges() { if ($this->blIsNewOrder) { @@ -85,6 +100,7 @@ class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceIn /** * @return string + * @throws InvalidArgumentException */ public function getFilename() { diff --git a/Application/Model/Exceptions/noBaseObjectSetException.php b/Application/Model/Exceptions/noBaseObjectSetException.php deleted file mode 100644 index 2ec97e7..0000000 --- a/Application/Model/Exceptions/noBaseObjectSetException.php +++ /dev/null @@ -1,21 +0,0 @@ - - * @link http://www.oxidmodule.com - */ - -namespace D3\PdfDocuments\Application\Model\Exceptions; - -use Exception; - -class noBaseObjectSetException extends pdfGeneratorExceptionAbstract -{ - public function __construct( $sMessage = "no base object (e.g. order) for pdf generator set", $iCode = 0, Exception $previous = null ) - { - parent::__construct( $sMessage, $iCode, $previous ); - } -} \ No newline at end of file diff --git a/Application/Model/Interfaces/pdfdocumentsGenericInterface.php b/Application/Model/Interfaces/pdfdocumentsGenericInterface.php index 152d053..8ca2867 100644 --- a/Application/Model/Interfaces/pdfdocumentsGenericInterface.php +++ b/Application/Model/Interfaces/pdfdocumentsGenericInterface.php @@ -10,8 +10,6 @@ namespace D3\PdfDocuments\Application\Model\Interfaces; -use D3\PdfDocuments\Application\Model\Exceptions\noBaseObjectSetException; - interface pdfdocumentsGenericInterface { /** @@ -58,7 +56,6 @@ interface pdfdocumentsGenericInterface * @param string $target * * @return mixed - * @throws noBaseObjectSetException */ public function genPdf($sFilename, $iSelLang = 0, $target = 'I'); diff --git a/Application/Model/Interfaces/pdfdocumentsOrderInterface.php b/Application/Model/Interfaces/pdfdocumentsOrderInterface.php index 2254beb..b9d000c 100644 --- a/Application/Model/Interfaces/pdfdocumentsOrderInterface.php +++ b/Application/Model/Interfaces/pdfdocumentsOrderInterface.php @@ -10,6 +10,7 @@ namespace D3\PdfDocuments\Application\Model\Interfaces; +use Assert\InvalidArgumentException; use OxidEsales\Eshop\Application\Model\Order; interface pdfdocumentsOrderInterface extends pdfdocumentsGenericInterface @@ -20,6 +21,7 @@ interface pdfdocumentsOrderInterface extends pdfdocumentsGenericInterface public function setOrder(Order $order); /** + * @throws InvalidArgumentException * @return Order */ public function getOrder(); diff --git a/Application/views/tpl/documents/inc/elements/foldmarks.tpl b/Application/views/tpl/documents/inc/elements/foldmarks.tpl index 9b99fff..522884b 100644 --- a/Application/views/tpl/documents/inc/elements/foldmarks.tpl +++ b/Application/views/tpl/documents/inc/elements/foldmarks.tpl @@ -1,7 +1,7 @@ [{assign var="defaultPagePadding" value=","|explode:"45,15,25,25"}] [{* top, right, bottom, left *}] [{assign var="pagePadding" value=$pagePadding|default:$defaultPagePadding}] -