refactoring: improve code quality

This commit is contained in:
Daniel Seifert 2025-01-21 23:47:20 +01:00
parent 9666276018
commit 5cff867c06
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
20 changed files with 230 additions and 407 deletions

View File

@ -8,6 +8,8 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Controller; namespace D3\PdfDocuments\Application\Controller;
use D3\PdfDocuments\Application\Model\Exceptions\noPdfHandlerFoundException; use D3\PdfDocuments\Application\Model\Exceptions\noPdfHandlerFoundException;
@ -20,12 +22,10 @@ use OxidEsales\Eshop\Core\Registry;
class orderOverviewPdfGenerator class orderOverviewPdfGenerator
{ {
/** /**
* @param Order $order
* @param int $iSelLang
* @throws noPdfHandlerFoundException * @throws noPdfHandlerFoundException
* @throws wrongPdfGeneratorInterface * @throws wrongPdfGeneratorInterface
*/ */
public function generatePdf(Order $order, int $iSelLang = 0) public function generatePdf(Order $order, int $iSelLang = 0): void
{ {
$Pdf= $this->getPdfClass(); $Pdf= $this->getPdfClass();
@ -38,7 +38,7 @@ class orderOverviewPdfGenerator
* @throws noPdfHandlerFoundException * @throws noPdfHandlerFoundException
* @throws wrongPdfGeneratorInterface * @throws wrongPdfGeneratorInterface
*/ */
public function getPdfClass() public function getPdfClass(): pdfdocumentsOrderInterface
{ {
$requestedType = Registry::getRequest()->getRequestParameter('pdftype'); $requestedType = Registry::getRequest()->getRequestParameter('pdftype');
@ -51,7 +51,10 @@ class orderOverviewPdfGenerator
} }
/** @var noPdfHandlerFoundException $e */ /** @var noPdfHandlerFoundException $e */
$e = oxNew(noPdfHandlerFoundException::class, Registry::getRequest()->getRequestParameter('pdftype')); $e = oxNew(
noPdfHandlerFoundException::class,
Registry::getRequest()->getRequestParameter('pdftype')
);
throw($e); throw($e);
} }
} }

View File

@ -8,25 +8,25 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\AbstractClasses; namespace D3\PdfDocuments\Application\Model\AbstractClasses;
use Assert\InvalidArgumentException; use Assert\InvalidArgumentException;
use D3\PdfDocuments\Application\Model\Constants; use D3\PdfDocuments\Application\Model\Constants;
use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsGenericInterface as genericInterface; use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsGenericInterface as genericInterface;
use Exception;
use OxidEsales\Eshop\Core\Base; use OxidEsales\Eshop\Core\Base;
use OxidEsales\Eshop\Core\Exception\StandardException;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\Eshop\Core\UtilsView;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface; use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRenderer; use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRenderer;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridgeInterface; use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridgeInterface;
use OxidEsales\Twig\Resolver\TemplateChain\TemplateNotInChainException;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Spipu\Html2Pdf\Exception\Html2PdfException; use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Html2Pdf; use Spipu\Html2Pdf\Html2Pdf;
use Twig\Error\Error;
abstract class pdfdocumentsGeneric extends Base implements genericInterface abstract class pdfdocumentsGeneric extends Base implements genericInterface
{ {
@ -40,10 +40,8 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
const PDF_ORIENTATION_PORTRAIT = 'P'; const PDF_ORIENTATION_PORTRAIT = 'P';
const PDF_ORIENTATION_LANDSCAPE = 'L'; const PDF_ORIENTATION_LANDSCAPE = 'L';
public $filenameExtension = 'pdf'; public string $filenameExtension = 'pdf';
public string $filename;
/** @var string */
public $filename;
public function runPreAction() public function runPreAction()
{ {
@ -54,47 +52,34 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
} }
/** /**
* @param string $sFilename
* @param int $iSelLang
* @param string $target
*
* @return string|null
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws Html2PdfException * @throws Html2PdfException
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public function genPdf($sFilename, $iSelLang = 0, $target = self::PDF_DESTINATION_STDOUT) public function genPdf(string $filename, int $language = 0, string $target = self::PDF_DESTINATION_STDOUT): ?string
{ {
$oPdf = oxNew(Html2Pdf::class, ...$this->getPdfProperties()); $oPdf = oxNew(Html2Pdf::class, ...$this->getPdfProperties());
$oPdf->setTestIsImage(false); $oPdf->setTestIsImage(false);
$htmlContent = $this->getHTMLContent($iSelLang); $htmlContent = $this->getHTMLContent($language);
$oPdf->writeHTML($htmlContent); $oPdf->writeHTML($htmlContent);
$oPdf->pdf->setAuthor( Registry::getConfig()->getActiveShop()->getFieldData( 'oxname')); $oPdf->pdf->setAuthor( Registry::getConfig()->getActiveShop()->getFieldData( 'oxname'));
$oPdf->pdf->setTitle( Registry::getLang()->translateString( $this->getTitleIdent())); $oPdf->pdf->setTitle( Registry::getLang()->translateString( $this->getTitleIdent()));
$oPdf->pdf->setCreator( 'D³ PDF Documents for OXID eShop'); $oPdf->pdf->setCreator( 'D³ PDF Documents for OXID eShop');
$oPdf->pdf->setSubject( NULL); $oPdf->pdf->setSubject( NULL);
return $this->output($oPdf, $sFilename, $target, $htmlContent); return $this->output($oPdf, $filename, $target, $htmlContent);
} }
/** /**
* @param int $iLanguage
* @throws Html2PdfException
*/
/**
* @param $iLanguage
*
* @return void
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws Html2PdfException * @throws Html2PdfException
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public function downloadPdf($iLanguage = 0) public function downloadPdf(int $language = 0): void
{ {
try { try {
$this->runPreAction(); $this->runPreAction();
$sFilename = $this->getFilename(); $sFilename = $this->getFilename();
$this->genPdf($sFilename, $iLanguage, self::PDF_DESTINATION_DOWNLOAD); $this->genPdf($sFilename, $language, self::PDF_DESTINATION_DOWNLOAD);
$this->runPostAction(); $this->runPostAction();
Registry::getUtils()->showMessageAndExit(''); Registry::getUtils()->showMessageAndExit('');
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
@ -104,22 +89,18 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
} }
/** /**
* @param string $path
* @param int $iLanguage
*
* @return void
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws Html2PdfException * @throws Html2PdfException
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public function savePdfFile($path, $iLanguage = 0) public function savePdfFile(string $path, int $language = 0): void
{ {
try { try {
$this->runPreAction(); $this->runPreAction();
$sFilename = $this->getFilename(); $sFilename = $this->getFilename();
$this->genPdf( $this->genPdf(
rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$sFilename, rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$sFilename,
$iLanguage, $language,
self::PDF_DESTINATION_FILE self::PDF_DESTINATION_FILE
); );
$this->runPostAction(); $this->runPostAction();
@ -130,19 +111,16 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
} }
/** /**
* @param int $iLanguage
*
* @return string|null
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws Html2PdfException * @throws Html2PdfException
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public function getPdfContent($iLanguage = 0) public function getPdfContent(int $language = 0): ?string
{ {
try { try {
$this->runPreAction(); $this->runPreAction();
$sFilename = $this->getFilename(); $sFilename = $this->getFilename();
$ret = $this->genPdf( $sFilename, $iLanguage, self::PDF_DESTINATION_STRING ); $ret = $this->genPdf( $sFilename, $language, self::PDF_DESTINATION_STRING );
$this->runPostAction(); $this->runPostAction();
return $ret; return $ret;
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
@ -153,47 +131,37 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
return null; return null;
} }
public function getTemplateEngineVars(int $iSelLang): array public function getTemplateEngineVars(int $language): array
{ {
unset($iSelLang); unset($language);
return [ return [
'config' => Registry::getConfig(), 'config' => Registry::getConfig(),
'oViewConf' => Registry::getConfig()->getActiveView()->getViewConfig(), 'oViewConf' => Registry::getConfig()->getActiveView()->getViewConfig(),
'shop' => Registry::getConfig()->getActiveShop(), 'shop' => Registry::getConfig()->getActiveShop(),
'lang' => Registry::getLang(), 'lang' => Registry::getLang(),
'document' => $this 'document' => $this
]; ];
} }
/** /**
* @param int $iSelLang * @throws ContainerExceptionInterface
* * @throws NotFoundExceptionInterface
* @return mixed
* @throws InvalidArgumentException
*/ */
public function getHTMLContent(int $iSelLang = 0) public function getHTMLContent(int $language = 0): string
{ {
$blCurrentRenderFromAdmin = self::$_blIsAdmin; $blCurrentRenderFromAdmin = self::$_blIsAdmin;
self::$_blIsAdmin = $this->renderTemplateFromAdmin(); self::$_blIsAdmin = $this->renderTemplateFromAdmin();
$lang = Registry::getLang(); $lang = Registry::getLang();
$currTplLang = $lang->getTplLanguage(); $currTplLang = $lang->getTplLanguage();
$lang->setTplLanguage($iSelLang); $lang->setTplLanguage($language);
$content = $this->getTemplateRenderer()->renderTemplate(
$this->getTemplate(),
$this->getTemplateEngineVars($language)
);
try {
$content = $this->getTemplateRenderer()->renderTemplate(
$this->getTemplate(),
$this->getTemplateEngineVars($iSelLang)
);
} catch (Error|TemplateNotInChainException $error) {
//Registry::getLogger()->error(dumpVar(__METHOD__." ".__LINE__), [$error->getFile()]);
throw oxNew(StandardException::class, $error->getMessage());
}
$lang->setTplLanguage($currTplLang); $lang->setTplLanguage($currTplLang);
self::$_blIsAdmin = $blCurrentRenderFromAdmin; self::$_blIsAdmin = $blCurrentRenderFromAdmin;
@ -201,6 +169,10 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
return $content; return $content;
} }
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function getTemplateRenderer(): TemplateRenderer protected function getTemplateRenderer(): TemplateRenderer
{ {
return ContainerFactory::getInstance()->getContainer() return ContainerFactory::getInstance()->getContainer()
@ -208,41 +180,25 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
->getTemplateRenderer(); ->getTemplateRenderer();
} }
/** public function getPdfProperties(): array
* arguments for Html2Pdf class constructor
* - $orientation = 'P',
* - $format = 'A4',
* - $lang = 'fr',
* - $unicode = true,
* - $encoding = 'UTF-8',
* - $margins = array(5, 5, 5, 8),
* - $pdfa = false
* @return string[]
*/
public function getPdfProperties()
{ {
$orientation = self::PDF_ORIENTATION_PORTRAIT; return [
$format = 'A4'; 'orientation' => self::PDF_ORIENTATION_PORTRAIT,
$lang = 'de'; 'format' => 'A4',
$unicode = true; 'lang' => 'de',
$encoding = 'UTF-8'; 'unicode' => true,
$margins = [0, 0, 0, 0]; 'encoding' => 'UTF-8',
$pdfa = true; 'margins' => [0, 0, 0, 0],
return [$orientation, $format, $lang, $unicode, $encoding, $margins, $pdfa]; 'pdfa' => true
];
} }
/** public function setFilename(string $filename): void
* @param $filename
*/
public function setFilename($filename)
{ {
$this->filename = $filename; $this->filename = $filename;
} }
/** public function getFilename(): string
* @return string
*/
public function getFilename()
{ {
// forced filename from setFilename() // forced filename from setFilename()
if ($this->filename) { if ($this->filename) {
@ -260,12 +216,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
); );
} }
/** public function addFilenameExtension(string $filename): string
* @param string $filename
*
* @return string
*/
public function addFilenameExtension($filename)
{ {
$extension = $this->filenameExtension; $extension = $this->filenameExtension;
$extensionLength = (strlen($extension) + 1) * -1; $extensionLength = (strlen($extension) + 1) * -1;
@ -278,12 +229,8 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
/** /**
* Gets proper file name * Gets proper file name
*
* @param $filename
*
* @return string
*/ */
public function makeValidFileName($filename) public function makeValidFileName(string $filename): string
{ {
// sanitize filename // sanitize filename
$filename = preg_replace( $filename = preg_replace(
@ -306,14 +253,14 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
// maximize filename length to 255 bytes // maximize filename length to 255 bytes
$ext = pathinfo($filename, PATHINFO_EXTENSION); $ext = pathinfo($filename, PATHINFO_EXTENSION);
return mb_strcut( return mb_strcut(
pathinfo($filename, PATHINFO_FILENAME), pathinfo($filename, PATHINFO_FILENAME),
0, 0,
255 - ($ext ? strlen($ext) + 1 : 0), 255 - ($ext ? strlen($ext) + 1 : 0),
mb_detect_encoding($filename) mb_detect_encoding($filename)
) . ($ext ? '.' . $ext : ''); ) . ($ext ? '.' . $ext : '');
} }
public function beautifyFilename($filename) public function beautifyFilename(string $filename): string
{ {
// reduce consecutive characters // reduce consecutive characters
$filename = preg_replace([ $filename = preg_replace([
@ -341,64 +288,52 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
return trim($filename); return trim($filename);
} }
/** public function renderTemplateFromAdmin(): bool
* @return bool
*/
public function renderTemplateFromAdmin()
{ {
return false; return false;
} }
/** /**
* @param Html2Pdf $oPdf
* @param $sFilename
* @param $target
* @param $html
*
* @return string|null
* @throws Html2PdfException * @throws Html2PdfException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws Exception
*/ */
public function output(Html2Pdf $oPdf, $sFilename, $target, $html) public function output(Html2Pdf $pdf, string $filename, string $target, string $html): ?string
{ {
$moduleSettings = ContainerFactory::getInstance()->getContainer()->get(ModuleSettingServiceInterface::class); $moduleSettings = ContainerFactory::getInstance()->getContainer()->get(ModuleSettingServiceInterface::class);
if ($moduleSettings->getBoolean( 'd3PdfDocumentsbDev', Constants::OXID_MODULE_ID )) { if ($moduleSettings->getBoolean( 'd3PdfDocumentsbDev', Constants::OXID_MODULE_ID )) {
return $this->outputDev($oPdf, $sFilename, $target, $html); return $this->outputDev($pdf, $filename, $target, $html);
} else { } else {
return $oPdf->output($sFilename, $target); return $pdf->output($filename, $target);
} }
} }
/** /**
* @param Html2Pdf $oPdf * @throws Exception
* @param $sFilename
* @param $target
* @param $html
* @return null
*/ */
public function outputDev(Html2Pdf $oPdf, $sFilename, $target, $html) public function outputDev(Html2Pdf $pdf, string $filename, string $target, string $html): ?string
{ {
$sFilename = str_replace('.pdf', '.html', $sFilename); $filename = str_replace('.pdf', '.html', $filename);
switch($target) { switch($target) {
case 'I': { case 'I': {
// Send PDF to the standard output // Send PDF to the standard output
if (ob_get_contents()) { if (ob_get_contents()) {
$oPdf->pdf->Error('Some data has already been output, can\'t send PDF file'); $pdf->pdf->Error('Some data has already been output, can\'t send PDF file');
} }
if (substr(php_sapi_name(), 0, 3) != 'cli') { if (!str_starts_with(php_sapi_name(), 'cli')) {
//We send to a browser // send to browser
header('Content-Type: text/html'); header('Content-Type: text/html');
if (headers_sent()) { if (headers_sent()) {
$oPdf->pdf->Error('Some data has already been output to browser, can\'t send PDF file'); $pdf->pdf->Error('Some data has already been output to browser, can\'t send PDF file');
} }
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1 header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public'); header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Length: '.strlen($html)); header('Content-Length: '.strlen($html));
header('Content-Disposition: inline; filename="'.basename($sFilename).'";'); header('Content-Disposition: inline; filename="'.basename($filename).'";');
} }
echo $html; echo $html;
break; break;
@ -406,11 +341,11 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
case 'D': { case 'D': {
// Download PDF as file // Download PDF as file
if (ob_get_contents()) { if (ob_get_contents()) {
$oPdf->pdf->Error('Some data has already been output, can\'t send PDF file'); $pdf->pdf->Error('Some data has already been output, can\'t send PDF file');
} }
header('Content-Description: File Transfer'); header('Content-Description: File Transfer');
if (headers_sent()) { if (headers_sent()) {
$oPdf->pdf->Error('Some data has already been output to browser, can\'t send PDF file'); $pdf->pdf->Error('Some data has already been output to browser, can\'t send PDF file');
} }
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1 header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public'); header('Pragma: public');
@ -422,7 +357,7 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
header('Content-Type: application/download', false); header('Content-Type: application/download', false);
header('Content-Type: text/html', false); header('Content-Type: text/html', false);
// use the Content-Disposition header to supply a recommended filename // use the Content-Disposition header to supply a recommended filename
header('Content-Disposition: attachment; filename="'.basename($sFilename).'";'); header('Content-Disposition: attachment; filename="'.basename($filename).'";');
header('Content-Transfer-Encoding: binary'); header('Content-Transfer-Encoding: binary');
header('Content-Length: '.strlen($html)); header('Content-Length: '.strlen($html));
echo $html; echo $html;
@ -430,20 +365,20 @@ abstract class pdfdocumentsGeneric extends Base implements genericInterface
} }
case 'F': { case 'F': {
// Save PDF to a local file // Save PDF to a local file
$f = fopen($sFilename, 'wb'); $f = fopen($filename, 'wb');
if (!$f) { if (!$f) {
$oPdf->pdf->Error('Unable to create output file: '.$sFilename); $pdf->pdf->Error('Unable to create output file: '.$filename);
} }
fwrite($f, $html, strlen($html)); fwrite($f, $html, strlen($html));
fclose($f); fclose($f);
break; break;
} }
case 'S': { case 'S': {
// Returns PDF as a string // Return PDF as a string
return $html; return $html;
} }
default: { default: {
$oPdf->pdf->Error('Incorrect output destination: '.$target); $pdf->pdf->Error('Incorrect output destination: '.$target);
} }
} }

View File

@ -8,6 +8,8 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\AbstractClasses; namespace D3\PdfDocuments\Application\Model\AbstractClasses;
use Assert\Assert; use Assert\Assert;
@ -20,8 +22,7 @@ use OxidEsales\Eshop\Core\Registry;
abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInterface abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInterface
{ {
/** @var Order */ public Order $order;
public $oOrder;
/** /**
* don't use order as constructor argument because of same method interface for all document types * don't use order as constructor argument because of same method interface for all document types
@ -31,55 +32,49 @@ abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInt
parent::__construct(); parent::__construct();
} }
/** public function setOrder(Order $order): void
* @param Order $order
*/
public function setOrder(Order $order)
{ {
$this->oOrder = $order; $this->order = $order;
} }
/** /**
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @return Order
*/ */
public function getOrder() public function getOrder(): Order
{ {
Assert::lazy() Assert::lazy()
->that($this->oOrder)->isInstanceOf(Order::class, 'no order for pdf generator set') ->that($this->order)->isInstanceOf(Order::class, 'no order for pdf generator set')
->that($this->oOrder->isLoaded())->true('given order is not loaded') ->that($this->order->isLoaded())->true('given order is not loaded')
->verifyNow(); ->verifyNow();
return $this->oOrder; return $this->order;
} }
/** /**
* @param int $iSelLang
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function getTemplateEngineVars($iSelLang): array public function getTemplateEngineVars(int $language): array
{ {
$oUser = oxNew(User::Class); $oUser = oxNew(User::Class);
$oUser->load($this->getOrder()->getFieldData('oxuserid')); $oUser->load($this->getOrder()->getFieldData('oxuserid'));
$oPayment = oxNew(Payment::class); $oPayment = oxNew(Payment::class);
$oPayment->loadInLang($iSelLang, $this->getOrder()->getFieldData('oxpaymenttype')); $oPayment->loadInLang($language, $this->getOrder()->getFieldData('oxpaymenttype'));
return array_merge( return array_merge(
parent::getTemplateEngineVars($iSelLang), parent::getTemplateEngineVars($language),
[ [
'order' => $this->getOrder(), 'order' => $this->getOrder(),
'user' => $oUser, 'user' => $oUser,
'payment' => $oPayment 'payment' => $oPayment
] ]
); );
} }
/** /**
* @return string
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function getFilename() public function getFilename(): string
{ {
// forced filename from setFilename() // forced filename from setFilename()
if ($this->filename) { if ($this->filename) {
@ -106,23 +101,12 @@ abstract class pdfdocumentsOrder extends pdfdocumentsGeneric implements orderInt
); );
} }
/** public function getPaymentTerm(): int
* @return int
*/
public function getPaymentTerm()
{ {
if (null === $iPaymentTerm = Registry::getConfig()->getConfigParam('iPaymentTerm')) { return (int) Registry::getConfig()->getConfigParam('iPaymentTerm') ?? 7;
$iPaymentTerm = 7;
}
return $iPaymentTerm;
} }
/** public function getPayableUntilDate(): false|int
* @return false|int
* @throws InvalidArgumentException
*/
public function getPayableUntilDate()
{ {
return strtotime( return strtotime(
'+' . $this->getPaymentTerm() . ' day', '+' . $this->getPaymentTerm() . ' day',

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace D3\PdfDocuments\Application\Model; namespace D3\PdfDocuments\Application\Model;
class Constants class Constants
{ {
public const OXID_MODULE_ID = 'd3PdfDocuments'; public const OXID_MODULE_ID = 'd3PdfDocuments';

View File

@ -8,40 +8,30 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Documents; namespace D3\PdfDocuments\Application\Model\Documents;
use D3\PdfDocuments\Application\Model\AbstractClasses\pdfdocumentsOrder; use D3\PdfDocuments\Application\Model\AbstractClasses\pdfdocumentsOrder;
class deliverynotePdf extends pdfdocumentsOrder class deliverynotePdf extends pdfdocumentsOrder
{ {
/** public function getRequestId(): string
* @return string
*/
public function getRequestId()
{ {
return 'dnote'; return 'dnote';
} }
/** public function getTitleIdent(): string
* @return string
*/
public function getTitleIdent()
{ {
return "D3_PDFDOCUMENTS_DELIVERYNOTE"; return "D3_PDFDOCUMENTS_DELIVERYNOTE";
} }
/** public function getTypeForFilename(): string
* @return string
*/
public function getTypeForFilename()
{ {
return 'delnote'; return 'delnote';
} }
/** public function getTemplate(): string
* @return string
*/
public function getTemplate()
{ {
return '@d3PdfDocuments/documents/deliverynote/deliverynote'; return '@d3PdfDocuments/documents/deliverynote/deliverynote';
} }

View File

@ -8,38 +8,28 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Documents; namespace D3\PdfDocuments\Application\Model\Documents;
class deliverynotewithoutlogoPdf extends deliverynotePdf class deliverynotewithoutlogoPdf extends deliverynotePdf
{ {
/** public function getRequestId(): string
* @return string
*/
public function getRequestId()
{ {
return 'dnote_without_logo'; return 'dnote_without_logo';
} }
/** public function getTitleIdent(): string
* @return string
*/
public function getTitleIdent()
{ {
return "D3_PDFDOCUMENTS_DELIVERYNOTE_WITHOUT_LOGO"; return "D3_PDFDOCUMENTS_DELIVERYNOTE_WITHOUT_LOGO";
} }
/** public function getTypeForFilename(): string
* @return string
*/
public function getTypeForFilename()
{ {
return 'delnote-nl'; return 'delnote-nl';
} }
/** public function getTemplate(): string
* @return string
*/
public function getTemplate()
{ {
return '@d3PdfDocuments/documents/deliverynote/deliverynoteNoLogo'; return '@d3PdfDocuments/documents/deliverynote/deliverynoteNoLogo';
} }

View File

@ -8,45 +8,33 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Documents; namespace D3\PdfDocuments\Application\Model\Documents;
use Assert\InvalidArgumentException;
use D3\PdfDocuments\Application\Model\AbstractClasses\pdfdocumentsOrder; use D3\PdfDocuments\Application\Model\AbstractClasses\pdfdocumentsOrder;
use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsOrderinvoiceInterface; use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsOrderinvoiceInterface;
class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceInterface class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceInterface
{ {
protected $blIsNewOrder = false; protected bool $isNewOrder = false;
/** public function getRequestId(): string
* @return string
*/
public function getRequestId()
{ {
return 'invoice'; return 'invoice';
} }
/** public function getTitleIdent(): string
* @return string
*/
public function getTitleIdent()
{ {
return "D3_PDFDOCUMENTS_INVOICE"; return "D3_PDFDOCUMENTS_INVOICE";
} }
/** public function getTypeForFilename(): string
* @return string
*/
public function getTypeForFilename()
{ {
return 'invoice'; return 'invoice';
} }
/** public function runPreAction(): void
* @return void
* @throws InvalidArgumentException
*/
public function runPreAction()
{ {
parent::runPreAction(); parent::runPreAction();
@ -55,54 +43,39 @@ class invoicePdf extends pdfdocumentsOrder implements pdfdocumentsOrderinvoiceIn
$this->saveOrderOnChanges(); $this->saveOrderOnChanges();
} }
/** public function setInvoiceNumber(): void
* @return void
* @throws InvalidArgumentException
*/
public function setInvoiceNumber()
{ {
if (!$this->getOrder()->getFieldData('oxbillnr')) { if (!$this->getOrder()->getFieldData('oxbillnr')) {
$this->getOrder()->assign(['oxbillnr' => $this->getOrder()->getNextBillNum()]); $this->getOrder()->assign(['oxbillnr' => $this->getOrder()->getNextBillNum()]);
$this->blIsNewOrder = true; $this->isNewOrder = true;
} }
} }
/** public function setInvoiceDate(): void
* @return void
* @throws InvalidArgumentException
*/
public function setInvoiceDate()
{ {
if ($this->getOrder()->getFieldData('oxbilldate') == '0000-00-00') { if ($this->getOrder()->getFieldData('oxbilldate') == '0000-00-00') {
$this->getOrder()->assign([ $this->getOrder()->assign([
"oxbilldate" => date('Y-m-d', mktime(0, 0, 0, date('m'), date('d'), date('Y'))) "oxbilldate" => date('Y-m-d')
]); ]);
$this->blIsNewOrder = true; $this->isNewOrder = true;
} }
} }
/** public function saveOrderOnChanges(): void
* @return void
* @throws InvalidArgumentException
*/
public function saveOrderOnChanges()
{ {
if ($this->blIsNewOrder) { if ($this->isNewOrder) {
$this->getOrder()->save(); $this->getOrder()->save();
} }
} }
public function getTemplate(){ public function getTemplate(): string
{
return '@d3PdfDocuments/documents/invoice/invoice'; return '@d3PdfDocuments/documents/invoice/invoice';
} }
/** public function getFilename(): string
* @return string
* @throws InvalidArgumentException
*/
public function getFilename()
{ {
$filename = parent::getFilename(); $filename = parent::getFilename();

View File

@ -8,38 +8,28 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Documents; namespace D3\PdfDocuments\Application\Model\Documents;
class invoicewithoutlogoPdf extends invoicePdf class invoicewithoutlogoPdf extends invoicePdf
{ {
/** public function getRequestId(): string
* @return string
*/
public function getRequestId()
{ {
return 'invoice_without_logo'; return 'invoice_without_logo';
} }
/** public function getTitleIdent(): string
* @return string
*/
public function getTitleIdent()
{ {
return "D3_PDFDOCUMENTS_INVOICE_WITHOUT_LOGO"; return "D3_PDFDOCUMENTS_INVOICE_WITHOUT_LOGO";
} }
/** public function getTypeForFilename(): string
* @return string
*/
public function getTypeForFilename()
{ {
return 'invoice-nl'; return 'invoice-nl';
} }
/** public function getTemplate(): string
* @return string
*/
public function getTemplate()
{ {
return '@d3PdfDocuments/documents/invoice/invoiceNoLogo'; return '@d3PdfDocuments/documents/invoice/invoiceNoLogo';
} }

View File

@ -8,23 +8,21 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Exceptions; namespace D3\PdfDocuments\Application\Model\Exceptions;
use Exception; use Exception;
class noPdfHandlerFoundException extends pdfGeneratorExceptionAbstract class noPdfHandlerFoundException extends pdfGeneratorExceptionAbstract
{ {
/** public function __construct(
* d3noPdfHandlerFoundException constructor. $requestId,
* string $message = "no pdf handler defined for given request id",
* @param $requestId int $code = 0,
* @param string $sMessage ?Exception $previous = null
* @param int $iCode ) {
* @param Exception|null $previous $message .= ' "'.$requestId.'"';
*/ parent::__construct( $message, $code, $previous );
public function __construct( $requestId, $sMessage = "no pdf handler defined for given request id", $iCode = 0, Exception $previous = null )
{
$sMessage .= ' "'.$requestId.'"';
parent::__construct( $sMessage, $iCode, $previous );
} }
} }

View File

@ -8,6 +8,8 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Exceptions; namespace D3\PdfDocuments\Application\Model\Exceptions;
use OxidEsales\Eshop\Core\Exception\StandardException; use OxidEsales\Eshop\Core\Exception\StandardException;

View File

@ -8,16 +8,21 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Exceptions; namespace D3\PdfDocuments\Application\Model\Exceptions;
use Exception; use Exception;
class wrongPdfGeneratorInterface extends pdfGeneratorExceptionAbstract class wrongPdfGeneratorInterface extends pdfGeneratorExceptionAbstract
{ {
public function __construct( $requiredInterface, $sMessage = "generator class doesn't fulfilled the interface", $iCode = 0, Exception $previous = null ) { public function __construct(
string $requiredInterface,
$sMessage .= $requiredInterface; string $message = "generator class doesn't fulfilled the interface",
int $code = 0,
parent::__construct( $sMessage, $iCode, $previous ); Exception $previous = null
) {
$message .= $requiredInterface;
parent::__construct( $message, $code, $previous );
} }
} }

View File

@ -8,71 +8,31 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Interfaces; namespace D3\PdfDocuments\Application\Model\Interfaces;
interface pdfdocumentsGenericInterface interface pdfdocumentsGenericInterface
{ {
/** public function getRequestId(): string;
* @return string
*/
public function getRequestId();
/** public function getTitleIdent(): string;
* @return string
*/
public function getTitleIdent();
/** public function getTemplate(): string;
* @return string
*/
public function getTemplate();
/** public function getHTMLContent(): string;
* @return mixed
*/
public function getHTMLContent();
/** public function downloadPdf(int $language = 0): void;
* @param int $iLanguage
*/
public function downloadPdf(int $iLanguage = 0);
/** public function getPdfContent(int $language = 0): ?string;
* @param int $iLanguage
*
* @return string|null
*/
public function getPdfContent(int $iLanguage = 0);
/** public function savePdfFile(string $path, int $language = 0): void;
* @param string $path
* @param int $iLanguage
*/
public function savePdfFile(string $path, int $iLanguage = 0);
/** public function genPdf(string $filename, int $language = 0, string $target = 'I'): ?string;
* @param string $sFilename
* @param int $iSelLang
* @param string $target
*
* @return mixed
*/
public function genPdf(string $sFilename, int $iSelLang = 0, string $target = 'I');
/** public function setFilename(string $filename): void;
* @param string $filename
*/
public function setFilename($filename);
/** public function getFilename(): string;
* @return string
*/
public function getFilename();
/** public function addFilenameExtension(string $filename): string;
* @param string $filename
*
* @return string
*/
public function addFilenameExtension($filename);
} }

View File

@ -8,6 +8,8 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Interfaces; namespace D3\PdfDocuments\Application\Model\Interfaces;
use Assert\InvalidArgumentException; use Assert\InvalidArgumentException;
@ -15,19 +17,9 @@ use OxidEsales\Eshop\Application\Model\Order;
interface pdfdocumentsOrderInterface extends pdfdocumentsGenericInterface interface pdfdocumentsOrderInterface extends pdfdocumentsGenericInterface
{ {
/** public function setOrder(Order $order): void;
* @param Order $order
*/
public function setOrder(Order $order);
/** public function getOrder(): Order;
* @throws InvalidArgumentException
* @return Order
*/
public function getOrder();
/** public function getTypeForFilename(): string;
* @return string
*/
public function getTypeForFilename();
} }

View File

@ -8,11 +8,13 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Interfaces; namespace D3\PdfDocuments\Application\Model\Interfaces;
interface pdfdocumentsOrderinvoiceInterface extends pdfdocumentsOrderInterface interface pdfdocumentsOrderinvoiceInterface extends pdfdocumentsOrderInterface
{ {
public function setInvoiceNumber(); public function setInvoiceNumber(): void;
public function setInvoiceDate(); public function setInvoiceDate(): void;
} }

View File

@ -8,6 +8,8 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Registries; namespace D3\PdfDocuments\Application\Model\Registries;
use D3\PdfDocuments\Application\Model\Exceptions\wrongPdfGeneratorInterface; use D3\PdfDocuments\Application\Model\Exceptions\wrongPdfGeneratorInterface;
@ -16,22 +18,17 @@ use OxidEsales\Eshop\Core\Exception\StandardException;
abstract class registryAbstract implements registryGenericInterface abstract class registryAbstract implements registryGenericInterface
{ {
protected $_aRegistry = array(); protected array $registry = [];
/** public function getRequiredGeneratorInterfaceClassName(): string
* @return string
*/
public function getRequiredGeneratorInterfaceClassName()
{ {
return pdfdocumentsGenericInterface::class; return pdfdocumentsGenericInterface::class;
} }
/** /**
* @param $className
*
* @throws wrongPdfGeneratorInterface * @throws wrongPdfGeneratorInterface
*/ */
public function addGenerator($className) public function addGenerator(string $className): void
{ {
if ( ! $this->hasGenerator( $className ) ) { if ( ! $this->hasGenerator( $className ) ) {
/** @var pdfdocumentsGenericInterface $generator */ /** @var pdfdocumentsGenericInterface $generator */
@ -44,28 +41,30 @@ abstract class registryAbstract implements registryGenericInterface
} }
/** /**
* @param $className * generator fully qualified class name * @param string $className * generator fully qualified class name
* @param pdfdocumentsGenericInterface $item * @param pdfdocumentsGenericInterface $item
* @throws wrongPdfGeneratorInterface * @throws wrongPdfGeneratorInterface
*/ */
protected function addItem($className, pdfdocumentsGenericInterface $item) protected function addItem(string $className, pdfdocumentsGenericInterface $item): void
{ {
$requiredInterface = $this->getRequiredGeneratorInterfaceClassName(); $requiredInterface = $this->getRequiredGeneratorInterfaceClassName();
if ( ! $item instanceof $requiredInterface ) { if ( ! $item instanceof $requiredInterface ) {
throw oxNew(wrongPdfGeneratorInterface::class, $requiredInterface); /** @var wrongPdfGeneratorInterface $exception */
$exception = oxNew(wrongPdfGeneratorInterface::class, $requiredInterface);
throw $exception;
} }
$this->_aRegistry[$className] = $item; $this->registry[$className] = $item;
} }
/** /**
* @param $className * generator fully qualified class name * @param string $className * generator fully qualified class name
*/ */
public function removeGenerator($className) public function removeGenerator(string $className): void
{ {
if ($this->hasGenerator($className)) { if ($this->hasGenerator($className)) {
unset( $this->_aRegistry[ $className ] ); unset( $this->registry[ $className ] );
} }
} }
@ -73,21 +72,18 @@ abstract class registryAbstract implements registryGenericInterface
* @param $className * generator fully qualified class name * @param $className * generator fully qualified class name
* @return bool * @return bool
*/ */
public function hasGenerator($className) public function hasGenerator(string $className): bool
{ {
return array_key_exists($className, $this->_aRegistry); return array_key_exists($className, $this->registry);
} }
/** public function getList(): array
* @return array
*/
public function getList()
{ {
return $this->_aRegistry; return $this->registry;
} }
public function clearList() public function clearList(): void
{ {
$this->_aRegistry = []; $this->registry = [];
} }
} }

View File

@ -8,31 +8,32 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Registries; namespace D3\PdfDocuments\Application\Model\Registries;
use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsGenericInterface;
interface registryGenericInterface interface registryGenericInterface
{ {
public function getRequiredGeneratorInterfaceClassName(); public function getRequiredGeneratorInterfaceClassName(): string;
/** /**
* @param $className * generator fully qualified class name * @param $className * generator fully qualified class name
*/ */
public function removeGenerator($className); public function removeGenerator(string $className): void;
/** /**
* @param $className * generator fully qualified class name * @param $className * generator fully qualified class name
*/ */
public function hasGenerator($className); public function hasGenerator(string $className): bool;
/** /**
* @param $className * generator fully qualified class name * @param $className * generator fully qualified class name
*/ */
public function getGenerator($className); public function getGenerator(string $className): pdfdocumentsGenericInterface;
/** public function getList(): array;
* @return array
*/
public function getList();
public function clearList(); public function clearList(): void;
} }

View File

@ -8,6 +8,8 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Registries; namespace D3\PdfDocuments\Application\Model\Registries;
use D3\PdfDocuments\Application\Model\Documents\deliverynotePdf; use D3\PdfDocuments\Application\Model\Documents\deliverynotePdf;
@ -15,6 +17,7 @@ use D3\PdfDocuments\Application\Model\Documents\deliverynotewithoutlogoPdf;
use D3\PdfDocuments\Application\Model\Documents\invoicePdf; use D3\PdfDocuments\Application\Model\Documents\invoicePdf;
use D3\PdfDocuments\Application\Model\Documents\invoicewithoutlogoPdf; use D3\PdfDocuments\Application\Model\Documents\invoicewithoutlogoPdf;
use D3\PdfDocuments\Application\Model\Exceptions\wrongPdfGeneratorInterface; use D3\PdfDocuments\Application\Model\Exceptions\wrongPdfGeneratorInterface;
use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsGenericInterface;
use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsOrderInterface; use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsOrderInterface;
class registryOrdermanagerActions extends registryAbstract implements registryOrdermanagerActionsInterface class registryOrdermanagerActions extends registryAbstract implements registryOrdermanagerActionsInterface
@ -30,20 +33,17 @@ class registryOrdermanagerActions extends registryAbstract implements registryOr
$this->addGenerator(deliverynotewithoutlogoPdf::class); $this->addGenerator(deliverynotewithoutlogoPdf::class);
} }
/** public function getRequiredGeneratorInterfaceClassName(): string
* @return string
*/
public function getRequiredGeneratorInterfaceClassName()
{ {
return pdfdocumentsOrderInterface::class; return pdfdocumentsOrderInterface::class;
} }
/** /**
* @param $className * generator fully qualified class name * @param string $className * generator fully qualified class name
* @return pdfdocumentsOrderInterface * @return pdfdocumentsOrderInterface
*/ */
public function getGenerator($className) public function getGenerator(string $className): pdfdocumentsOrderInterface
{ {
return $this->_aRegistry[$className]; return $this->registry[$className];
} }
} }

View File

@ -8,12 +8,14 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Registries; namespace D3\PdfDocuments\Application\Model\Registries;
interface registryOrdermanagerActionsInterface extends registryGenericInterface interface registryOrdermanagerActionsInterface extends registryGenericInterface
{ {
/** /**
* @param $className * generator fully qualified class name * @param string $className * generator fully qualified class name
*/ */
public function addGenerator($className); public function addGenerator(string $className): void;
} }

View File

@ -8,6 +8,8 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Registries; namespace D3\PdfDocuments\Application\Model\Registries;
use D3\PdfDocuments\Application\Model\Documents\deliverynotePdf; use D3\PdfDocuments\Application\Model\Documents\deliverynotePdf;
@ -30,20 +32,17 @@ class registryOrderoverview extends registryAbstract implements registryOrderove
$this->addGenerator(deliverynotewithoutlogoPdf::class); $this->addGenerator(deliverynotewithoutlogoPdf::class);
} }
/** public function getRequiredGeneratorInterfaceClassName(): string
* @return string
*/
public function getRequiredGeneratorInterfaceClassName()
{ {
return pdfdocumentsOrderInterface::class; return pdfdocumentsOrderInterface::class;
} }
/** /**
* @param $className * generator fully qualified class name * @param string $className * generator fully qualified class name
* @return pdfdocumentsOrderInterface * @return pdfdocumentsOrderInterface
*/ */
public function getGenerator($className) public function getGenerator(string $className): pdfdocumentsOrderInterface
{ {
return $this->_aRegistry[$className]; return $this->registry[$className];
} }
} }

View File

@ -8,12 +8,14 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types = 1);
namespace D3\PdfDocuments\Application\Model\Registries; namespace D3\PdfDocuments\Application\Model\Registries;
interface registryOrderoverviewInterface extends registryGenericInterface interface registryOrderoverviewInterface extends registryGenericInterface
{ {
/** /**
* @param $className * generator fully qualified class name * @param string $className * generator fully qualified class name
*/ */
public function addGenerator($className); public function addGenerator(string $className): void;
} }