From 40fb77b3763f15f9e73abb0a9cf542afa976004b Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 10 Dec 2024 10:27:53 +0100 Subject: [PATCH] improve code --- .../Controller/orderOverviewPdfGenerator.php | 2 +- .../AbstractClasses/pdfdocumentsGeneric.php | 33 ++++--------- .../AbstractClasses/pdfdocumentsOrder.php | 2 +- .../pdfdocumentsGenericInterface.php | 10 ++-- .../Model/Registries/registryAbstract.php | 4 +- .../d3_overview_controller_pdfdocuments.php | 49 +++++++++++++------ composer.json | 2 +- 7 files changed, 52 insertions(+), 50 deletions(-) diff --git a/Application/Controller/orderOverviewPdfGenerator.php b/Application/Controller/orderOverviewPdfGenerator.php index 2318d6f..cfa0834 100644 --- a/Application/Controller/orderOverviewPdfGenerator.php +++ b/Application/Controller/orderOverviewPdfGenerator.php @@ -23,7 +23,7 @@ class orderOverviewPdfGenerator * @param int $iSelLang * @throws noPdfHandlerFoundException */ - public function generatePdf(Order $order, $iSelLang = 0) + public function generatePdf(Order $order, int $iSelLang = 0) { $Pdf= $this->getPdfClass(); diff --git a/Application/Model/AbstractClasses/pdfdocumentsGeneric.php b/Application/Model/AbstractClasses/pdfdocumentsGeneric.php index d0e9c8c..b4776d0 100644 --- a/Application/Model/AbstractClasses/pdfdocumentsGeneric.php +++ b/Application/Model/AbstractClasses/pdfdocumentsGeneric.php @@ -18,16 +18,13 @@ use OxidEsales\Eshop\Core\Base; use OxidEsales\Eshop\Core\Exception\StandardException; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\UtilsView; -use OxidEsales\EshopCommunity\Core\Di\ContainerFacade; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface; use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateEngineInterface; -use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRenderer; use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridgeInterface; use OxidEsales\Twig\Resolver\TemplateChain\TemplateNotInChainException; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; -use Smarty; use Spipu\Html2Pdf\Exception\Html2PdfException; use Spipu\Html2Pdf\Html2Pdf; use Twig\Error\Error; @@ -46,7 +43,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface public $filenameExtension = 'pdf'; - /** @var Smarty */ + /** @var TemplateEngineInterface */ public $oTemplateEngine; /** @var string */ @@ -90,7 +87,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface * @param $sFilename * @param int $iSelLang * @param string $target - * @return mixed|string|null + * @return string|null * @throws Html2PdfException */ public function genPdf($sFilename, $iSelLang = 0, $target = self::PDF_DESTINATION_STDOUT) @@ -118,10 +115,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface $this->genPdf($sFilename, $iLanguage, self::PDF_DESTINATION_DOWNLOAD); $this->runPostAction(); Registry::getUtils()->showMessageAndExit(''); - } catch (pdfGeneratorExceptionAbstract $e) { - Registry::get(UtilsView::class)->addErrorToDisplay($e); - Registry::getLogger()->error($e); - } catch (InvalidArgumentException $e) { + } catch (pdfGeneratorExceptionAbstract|InvalidArgumentException $e) { Registry::get(UtilsView::class)->addErrorToDisplay($e); Registry::getLogger()->error($e); } @@ -144,10 +138,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface self::PDF_DESTINATION_FILE ); $this->runPostAction(); - } catch (pdfGeneratorExceptionAbstract $e) { - Registry::get(UtilsView::class)->addErrorToDisplay($e); - Registry::getLogger()->error($e); - } catch (InvalidArgumentException $e) { + } catch (pdfGeneratorExceptionAbstract|InvalidArgumentException $e) { Registry::get(UtilsView::class)->addErrorToDisplay($e); Registry::getLogger()->error($e); } @@ -167,10 +158,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface $ret = $this->genPdf( $sFilename, $iLanguage, self::PDF_DESTINATION_STRING ); $this->runPostAction(); return $ret; - } catch (pdfGeneratorExceptionAbstract $e) { - Registry::get(UtilsView::class)->addErrorToDisplay($e); - Registry::getLogger()->error($e); - } catch (InvalidArgumentException $e) { + } catch (pdfGeneratorExceptionAbstract|InvalidArgumentException $e) { Registry::get(UtilsView::class)->addErrorToDisplay($e); Registry::getLogger()->error($e); } @@ -181,7 +169,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface /** * @param int $iSelLang */ - public function setTemplateEngineVars($iSelLang) + public function setTemplateEngineVars(int $iSelLang) { unset($iSelLang); @@ -198,7 +186,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface * @return mixed * @throws InvalidArgumentException */ - public function getHTMLContent($iSelLang = 0) + public function getHTMLContent(int $iSelLang = 0) { $blCurrentRenderFromAdmin = self::$_blIsAdmin; self::$_blIsAdmin = $this->renderTemplateFromAdmin(); @@ -216,8 +204,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface //Registry::getLogger()->error(dumpVar(__METHOD__." ".__LINE__), [$error->getFile()]); - $error = oxNew(StandardException::class, $error->getMessage()); - throw $error; + throw oxNew(StandardException::class, $error->getMessage()); } $lang->setTplLanguage($currTplLang); @@ -324,14 +311,12 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface // maximize filename length to 255 bytes $ext = pathinfo($filename, PATHINFO_EXTENSION); - $filename = mb_strcut( + return mb_strcut( pathinfo($filename, PATHINFO_FILENAME), 0, 255 - ($ext ? strlen($ext) + 1 : 0), mb_detect_encoding($filename) ) . ($ext ? '.' . $ext : ''); - - return $filename; } public function beautifyFilename($filename) diff --git a/Application/Model/AbstractClasses/pdfdocumentsOrder.php b/Application/Model/AbstractClasses/pdfdocumentsOrder.php index 6cd7a59..faf346f 100644 --- a/Application/Model/AbstractClasses/pdfdocumentsOrder.php +++ b/Application/Model/AbstractClasses/pdfdocumentsOrder.php @@ -116,7 +116,7 @@ abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInt } /** - * @return false|string + * @return false|int * @throws InvalidArgumentException */ public function getPayableUntilDate() diff --git a/Application/Model/Interfaces/pdfdocumentsGenericInterface.php b/Application/Model/Interfaces/pdfdocumentsGenericInterface.php index 8ca2867..009003b 100644 --- a/Application/Model/Interfaces/pdfdocumentsGenericInterface.php +++ b/Application/Model/Interfaces/pdfdocumentsGenericInterface.php @@ -35,29 +35,29 @@ interface pdfdocumentsGenericInterface /** * @param int $iLanguage */ - public function downloadPdf($iLanguage = 0); + public function downloadPdf(int $iLanguage = 0); /** * @param int $iLanguage * * @return string|null */ - public function getPdfContent($iLanguage = 0); + public function getPdfContent(int $iLanguage = 0); /** * @param string $path * @param int $iLanguage */ - public function savePdfFile($path, $iLanguage = 0); + public function savePdfFile(string $path, int $iLanguage = 0); /** - * @param $sFilename + * @param string $sFilename * @param int $iSelLang * @param string $target * * @return mixed */ - public function genPdf($sFilename, $iSelLang = 0, $target = 'I'); + public function genPdf(string $sFilename, int $iSelLang = 0, string $target = 'I'); /** * @param string $filename diff --git a/Application/Model/Registries/registryAbstract.php b/Application/Model/Registries/registryAbstract.php index 34c7111..ebdec22 100644 --- a/Application/Model/Registries/registryAbstract.php +++ b/Application/Model/Registries/registryAbstract.php @@ -31,7 +31,7 @@ abstract class registryAbstract implements registryGenericInterface */ public function addGenerator($className) { - if (false == $this->hasGenerator($className)) { + if ( ! $this->hasGenerator( $className ) ) { /** @var pdfdocumentsGenericInterface $generator */ $generator = oxNew( $className ); @@ -49,7 +49,7 @@ abstract class registryAbstract implements registryGenericInterface { $requiredInterface = $this->getRequiredGeneratorInterfaceClassName(); - if (false == $item instanceof $requiredInterface) { + if ( ! $item instanceof $requiredInterface ) { throw oxNew(wrongPdfGeneratorInterface::class, $requiredInterface); } diff --git a/Modules/Application/Controller/d3_overview_controller_pdfdocuments.php b/Modules/Application/Controller/d3_overview_controller_pdfdocuments.php index fc06e6e..c87bde2 100644 --- a/Modules/Application/Controller/d3_overview_controller_pdfdocuments.php +++ b/Modules/Application/Controller/d3_overview_controller_pdfdocuments.php @@ -14,37 +14,57 @@ use D3\PdfDocuments\Application\Controller\orderOverviewPdfGenerator; use D3\PdfDocuments\Application\Model\Exceptions\noPdfHandlerFoundException; use D3\PdfDocuments\Application\Model\Exceptions\pdfGeneratorExceptionAbstract; use D3\PdfDocuments\Application\Model\Registries\registryOrderoverview; -use OxidEsales\Eshop\Application\Controller\Admin\OrderOverview; +use Doctrine\DBAL\Driver\Exception; +use Doctrine\DBAL\Exception as DBALException; +use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Query\QueryBuilder; use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\TableViewNameGenerator; +use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; class d3_overview_controller_pdfdocuments extends d3_overview_controller_pdfdocuments_parent { - /** + /** * @return bool - * @throws DatabaseConnectionException + * @throws Exception */ - public function d3CanExport() + public function d3CanExport(): bool { - // We force reading from master to prevent issues with slow replications or open transactions (see ESDEV-3804). - $masterDb = DatabaseProvider::getMaster(); - $sOrderId = $this->getEditObjectId(); + try { + $sOrderId = $this->getEditObjectId(); - $viewNameGenerator = Registry::get(TableViewNameGenerator::class); - $sTable = $viewNameGenerator->getViewName("oxorderarticles"); + $viewNameGenerator = Registry::get( TableViewNameGenerator::class ); + $sTable = $viewNameGenerator->getViewName( "oxorderarticles" ); - $sQ = "select count(oxid) from $sTable where oxorderid = " . $masterDb->quote($sOrderId) . " and oxstorno = 0"; - return (bool) $masterDb->getOne($sQ); + /** @var QueryBuilder $queryBuilder */ + $queryBuilder = ContainerFactory::getInstance()->getContainer()->get( QueryBuilderFactoryInterface::class )->create(); + $queryBuilder + ->select( 'oxid' ) + ->from( $sTable ) + ->where( + $queryBuilder->expr()->and( + $queryBuilder->expr()->eq( 'oxorderid', $queryBuilder->createNamedParameter( $sOrderId ) ), + $queryBuilder->expr()->eq( 'oxstorno', $queryBuilder->createNamedParameter( 0, ParameterType::INTEGER ) ) + ) + ); + + return $queryBuilder->execute()->fetchOne(); + } catch (NotFoundExceptionInterface|ContainerExceptionInterface|DBALException) { + return false; + } } /** * @throws noPdfHandlerFoundException * @throws pdfGeneratorExceptionAbstract */ - public function d3CreatePDF() + public function d3CreatePDF(): void { $soxId = $this->getEditObjectId(); if ($soxId != "-1" && isset($soxId)) { @@ -57,10 +77,7 @@ class d3_overview_controller_pdfdocuments extends d3_overview_controller_pdfdocu } } - /** - * @return registryOrderoverview - */ - public function d3getGeneratorList() + public function d3getGeneratorList(): registryOrderoverview { return oxNew(registryOrderoverview::class); } diff --git a/composer.json b/composer.json index a81a793..5e256f2 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ { "name": "D3 Data Development (Inh. Thomas Dartsch)", "email": "info@shopmodule.com", - "homepage": "http://www.d3data.de", + "homepage": "https://www.d3data.de", "role": "Owner" } ],