From b7d680f6a081d3b0e374f87cd4c25ea685d97bb3 Mon Sep 17 00:00:00 2001 From: Nicolas MELONI Date: Tue, 19 Nov 2024 14:34:46 +0100 Subject: [PATCH] Added more info on debug logger --- .../AssetProductAttributeProcessor.php | 115 +++++++++++++++--- 1 file changed, 101 insertions(+), 14 deletions(-) diff --git a/src/Processor/ProductAttribute/AssetProductAttributeProcessor.php b/src/Processor/ProductAttribute/AssetProductAttributeProcessor.php index 2e6128cc..b6ed1836 100644 --- a/src/Processor/ProductAttribute/AssetProductAttributeProcessor.php +++ b/src/Processor/ProductAttribute/AssetProductAttributeProcessor.php @@ -91,7 +91,7 @@ private function handleAsset( array $queryParam, array $assetAttributeResource, ): void { - if (!\method_exists($model, 'addAsset')) { + if (!\method_exists($model, 'addAsset') || !\method_exists($model, 'getAssets')) { return; } @@ -100,18 +100,6 @@ private function handleAsset( try { $asset = $this->assetRepository->findOneBy($queryParam); - if (!$asset instanceof Asset) { - /** @var Asset $asset */ - $asset = $this->assetFactory->createNew(); - $this->entityManager->persist($asset); - $asset->setFamilyCode($assetFamilyCode); - $asset->setAssetCode($assetCode); - $asset->setAttributeCode($attributeCode); - $asset->setLocale($queryParam['locale']); - $asset->setScope($queryParam['scope']); - $asset->setType($this->akeneoAssetAttributePropertiesProvider->getType($assetFamilyCode, $attributeCode)); - } - $assetAttributeValue = $this->akeneoAssetAttributeDataProvider->getData( $assetFamilyCode, $attributeCode, @@ -129,8 +117,66 @@ private function handleAsset( $assetAttributeValue, ); + if (!$asset instanceof Asset) { + $this->akeneoLogger->info('Created asset for product', [ + 'product' => $model->getCode(), + 'familyCode' => $assetFamilyCode, + 'attributeCode' => $attributeCode, + 'assetCode' => $assetCode, + 'locale' => $queryParam['locale'], + 'content' => $data, + ]); + + /** @var Asset $asset */ + $asset = $this->assetFactory->createNew(); + $this->entityManager->persist($asset); + $asset->setFamilyCode($assetFamilyCode); + $asset->setAssetCode($assetCode); + $asset->setAttributeCode($attributeCode); + $asset->setLocale($queryParam['locale']); + $asset->setScope($queryParam['scope']); + $asset->setType($this->akeneoAssetAttributePropertiesProvider->getType($assetFamilyCode, $attributeCode)); + $asset->setContent($data); + $model->addAsset($asset); + + return; + } + + $oldContent = $asset->getContent(); + + if ($oldContent === $data) { + $this->akeneoLogger->info('Skipped asset for product as it has same content', [ + 'product' => $model->getCode(), + 'familyCode' => $assetFamilyCode, + 'attributeCode' => $attributeCode, + 'assetCode' => $assetCode, + 'locale' => $queryParam['locale'], + 'content' => $data, + ]); + + $this->addAssetToProduct( + $model, + $asset, + $assetFamilyCode, + $attributeCode, + $assetCode, + $queryParam, + $data, + ); + + return; + } + $asset->setContent($data); - $model->addAsset($asset); + $this->addAssetToProduct( + $model, + $asset, + $assetFamilyCode, + $attributeCode, + $assetCode, + $queryParam, + $data, + ); $this->akeneoLogger->info('Updated asset for product', [ 'product' => $model->getCode(), @@ -138,10 +184,12 @@ private function handleAsset( 'attributeCode' => $attributeCode, 'assetCode' => $assetCode, 'locale' => $queryParam['locale'], + 'old_content' => $oldContent, 'content' => $data, ]); } catch (MissingLocaleTranslationException|MissingLocaleTranslationOrScopeException|MissingScopeException $e) { $this->akeneoLogger->debug('Error processing asset', [ + 'product' => $model->getCode(), 'family_code' => $assetFamilyCode, 'attribute_code' => $attributeCode, 'asset_code' => $assetCode, @@ -149,8 +197,47 @@ private function handleAsset( 'akeneo_locale' => $akeneoLocale, 'resource' => $assetAttributeResource, 'exception' => $e->getMessage(), + 'exception_type' => $e::class, 'trace' => $e->getTraceAsString(), ]); } } + + private function addAssetToProduct( + ProductInterface $model, + Asset $asset, + string $assetFamilyCode, + string $attributeCode, + string $assetCode, + array $queryParam, + array $data, + ): void { + if (!\method_exists($model, 'addAsset') || !\method_exists($model, 'getAssets')) { + return; + } + + if ($model->getAssets()->contains($asset)) { + $this->akeneoLogger->info('Asset already associated to product', [ + 'product' => $model->getCode(), + 'familyCode' => $assetFamilyCode, + 'attributeCode' => $attributeCode, + 'assetCode' => $assetCode, + 'locale' => $queryParam['locale'], + 'content' => $data, + ]); + + return; + } + + $model->addAsset($asset); + + $this->akeneoLogger->info('Associated asset to product', [ + 'product' => $model->getCode(), + 'familyCode' => $assetFamilyCode, + 'attributeCode' => $attributeCode, + 'assetCode' => $assetCode, + 'locale' => $queryParam['locale'], + 'content' => $data, + ]); + } }