From 00d6509a27d5b32abc2da955f9a80f190350c91a Mon Sep 17 00:00:00 2001 From: Lukas Stuffer Date: Fri, 7 Jun 2024 13:19:45 +0200 Subject: [PATCH] PAC-344: eval clean up --- .../CleanUpBundleProductRelationsObserver.php | 127 ++++++++++++++++++ src/Utils/ConfigurationKeys.php | 27 ++++ symfony/Resources/config/services.xml | 6 +- 3 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 src/Observers/CleanUpBundleProductRelationsObserver.php create mode 100644 src/Utils/ConfigurationKeys.php diff --git a/src/Observers/CleanUpBundleProductRelationsObserver.php b/src/Observers/CleanUpBundleProductRelationsObserver.php new file mode 100644 index 0000000..3de2587 --- /dev/null +++ b/src/Observers/CleanUpBundleProductRelationsObserver.php @@ -0,0 +1,127 @@ + - TechDivision GmbH + * @link https://www.techdivision.com/ + * @author MET + */ +class CleanUpBundleProductRelationsObserver extends AbstractProductImportObserver +{ + /** + * @var ProductBundleProcessorInterface + */ + protected $productBundleProcessor; + + /** + * @param ProductBundleProcessorInterface $productBundleProcessor + * @param StateDetectorInterface|null $stateDetector + */ + public function __construct( + ProductBundleProcessorInterface $productBundleProcessor, + StateDetectorInterface $stateDetector = null + ) { + parent::__construct($stateDetector); + $this->productBundleProcessor = $productBundleProcessor; + } + + /** + * @return ProductBundleProcessorInterface + */ + protected function getProductBundleProcessor() + { + return $this->productBundleProcessor; + } + + /** + * @return void + * @throws Exception + */ + protected function process() + { + + // query whether we've found a configurable product or not + if ($this->getValue(ColumnKeys::PRODUCT_TYPE) !== ProductTypes::BUNDLE) { + return; + } + + // query whether the media gallery has to be cleaned up or not + if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_BUNDLE) && + $this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_BUNDLE) + ) { + + $this->cleanUpBundles(); + + $this->getSubject() + ->getSystemLogger() + ->debug( + $this->getSubject()->appendExceptionSuffix( + sprintf( + 'Successfully clean up variants for product with SKU "%s"', + $this->getValue(ColumnKeys::SKU) + ) + ) + ); + } + } + + /** + * @return void + * @throws Exception Is thrown, if either the variant children und attributes can not be deleted + */ + protected function cleanUpBundles() + { + // TODO herausfinden wie + } + + /** + * Delete not exists import variants from database. + * + * @param int $parentProductId The ID of the parent product + * @param array $childData The array of variants + * + * @return void + * @throws Exception + */ + protected function cleanUpVariantChildren($parentProductId, array $childData) + { + if (empty($childData)) { + return; + } + + $parentSku = $this->getValue(ColumnKeys::SKU); + + // TODO remove the old links + } + + /** + * Return's the PK to create the product => variant relation. + * + * @return integer The PK to create the relation with + */ + protected function getLastPrimaryKey() + { + return $this->getLastEntityId(); + } +} diff --git a/src/Utils/ConfigurationKeys.php b/src/Utils/ConfigurationKeys.php new file mode 100644 index 0000000..fcb0269 --- /dev/null +++ b/src/Utils/ConfigurationKeys.php @@ -0,0 +1,27 @@ + - TechDivision GmbH + * @link https://www.techdivision.com/ + * @author MET + */ +class ConfigurationKeys extends \TechDivision\Import\Utils\ConfigurationKeys +{ + /** + * @var string + */ + const CLEAN_UP_BUNDLE = 'clean-up-bundle'; +} diff --git a/symfony/Resources/config/services.xml b/symfony/Resources/config/services.xml index a96b4d3..0a09156 100644 --- a/symfony/Resources/config/services.xml +++ b/symfony/Resources/config/services.xml @@ -206,6 +206,10 @@ %import_product_bundle.param.sku.column.name% + + + +