From 88ff329bb47a195f911bea386b0d93cf4b856427 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 2 Jun 2020 10:04:19 +0200 Subject: [PATCH] simplify registry configuration --- .../Exceptions/wrongPdfGeneratorInterface.php | 26 +++++++++++++ .../Model/Registries/registryAbstract.php | 39 +++++++++++++++++-- .../Registries/registryGenericInterface.php | 2 + .../Registries/registryOrderoverview.php | 15 ++++--- .../registryOrderoverviewInterface.php | 5 +-- 5 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 Application/Model/Exceptions/wrongPdfGeneratorInterface.php diff --git a/Application/Model/Exceptions/wrongPdfGeneratorInterface.php b/Application/Model/Exceptions/wrongPdfGeneratorInterface.php new file mode 100644 index 0000000..8b2572e --- /dev/null +++ b/Application/Model/Exceptions/wrongPdfGeneratorInterface.php @@ -0,0 +1,26 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\PdfDocuments\Application\Model\Exceptions; + +class wrongPdfGeneratorInterface extends pdfGeneratorExceptionAbstract +{ + public function __construct( $requiredInterface, $sMessage = "generator class doesn't fulfilled the interface", $iCode = 0, \Exception $previous = null ) { + + $sMessage .= $requiredInterface; + + parent::__construct( $sMessage, $iCode, $previous ); + } +} \ No newline at end of file diff --git a/Application/Model/Registries/registryAbstract.php b/Application/Model/Registries/registryAbstract.php index 433c642..1742afd 100644 --- a/Application/Model/Registries/registryAbstract.php +++ b/Application/Model/Registries/registryAbstract.php @@ -17,18 +17,49 @@ namespace D3\PdfDocuments\Application\Model\Registries; +use D3\PdfDocuments\Application\Model\Exceptions\wrongPdfGeneratorInterface; use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsGenericInterface; +use OxidEsales\Eshop\Core\Exception\StandardException; abstract class registryAbstract implements registryGenericInterface { protected $_aRegistry = array(); + /** + * @return string + */ + public function getRequiredGeneratorInterfaceClassName() + { + return pdfdocumentsGenericInterface::class; + } + + /** + * @param $className + */ + public function addGenerator($className) + { + if (false == $this->hasGenerator($className)) { + /** @var pdfdocumentsGenericInterface $generator */ + $generator = oxNew( $className ); + + $this->addItem( $className, $generator ); + } else { + throw oxNew(StandardException::class, 'generator still exists in registry'); + } + } + /** * @param $className * generator fully qualified class name * @param pdfdocumentsGenericInterface $item */ protected function addItem($className, pdfdocumentsGenericInterface $item) { + $requiredInterface = $this->getRequiredGeneratorInterfaceClassName(); + + if (false == $item instanceof $requiredInterface) { + throw oxNew(wrongPdfGeneratorInterface::class, $requiredInterface); + } + $this->_aRegistry[$className] = $item; } @@ -37,7 +68,9 @@ abstract class registryAbstract implements registryGenericInterface */ public function removeGenerator($className) { - // TODO: Implement removeGenerator() method. + if ($this->hasGenerator($className)) { + unset( $this->_aRegistry[ $className ] ); + } } /** @@ -45,7 +78,7 @@ abstract class registryAbstract implements registryGenericInterface */ public function hasGenerator($className) { - // TODO: Implement hasGenerator() method. + return array_key_exists($className, $this->_aRegistry); } /** @@ -58,6 +91,6 @@ abstract class registryAbstract implements registryGenericInterface public function clearList() { - // TODO: Implement clearList() method. + $this->_aRegistry = []; } } \ No newline at end of file diff --git a/Application/Model/Registries/registryGenericInterface.php b/Application/Model/Registries/registryGenericInterface.php index 37d752c..2d775ea 100644 --- a/Application/Model/Registries/registryGenericInterface.php +++ b/Application/Model/Registries/registryGenericInterface.php @@ -19,6 +19,8 @@ namespace D3\PdfDocuments\Application\Model\Registries; interface registryGenericInterface { + public function getRequiredGeneratorInterfaceClassName(); + /** * @param $className * generator fully qualified class name */ diff --git a/Application/Model/Registries/registryOrderoverview.php b/Application/Model/Registries/registryOrderoverview.php index 625a17e..4349665 100644 --- a/Application/Model/Registries/registryOrderoverview.php +++ b/Application/Model/Registries/registryOrderoverview.php @@ -27,19 +27,18 @@ class registryOrderoverview extends registryAbstract implements registryOrderove { public function __construct() { - $this->addGenerator(invoicePdf::class, oxNew(invoicePdf::class)); - $this->addGenerator(deliverynotePdf::class, oxNew(deliverynotePdf::class)); - $this->addGenerator(invoicewithoutlogoPdf::class, oxNew(invoicewithoutlogoPdf::class)); - $this->addGenerator(deliverynotewithoutlogoPdf::class, oxNew(deliverynotewithoutlogoPdf::class)); + $this->addGenerator(invoicePdf::class); + $this->addGenerator(deliverynotePdf::class); + $this->addGenerator(invoicewithoutlogoPdf::class); + $this->addGenerator(deliverynotewithoutlogoPdf::class); } /** - * @param $className - * @param pdfdocumentsOrderInterface $pdfGenerator + * @return string */ - public function addGenerator($className, pdfdocumentsOrderInterface $pdfGenerator) + public function getRequiredGeneratorInterfaceClassName() { - $this->addItem($className, $pdfGenerator); + return pdfdocumentsOrderInterface::class; } /** diff --git a/Application/Model/Registries/registryOrderoverviewInterface.php b/Application/Model/Registries/registryOrderoverviewInterface.php index 0b7db65..dc142a1 100644 --- a/Application/Model/Registries/registryOrderoverviewInterface.php +++ b/Application/Model/Registries/registryOrderoverviewInterface.php @@ -17,13 +17,10 @@ namespace D3\PdfDocuments\Application\Model\Registries; -use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsOrderInterface; - interface registryOrderoverviewInterface extends registryGenericInterface { /** * @param $className * generator fully qualified class name - * @param pdfdocumentsOrderInterface $pdfGenerator */ - public function addGenerator($className, pdfdocumentsOrderInterface $pdfGenerator); + public function addGenerator($className); } \ No newline at end of file