Skip to content

Commit

Permalink
Added more info on debug logger
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGrimmChester committed Nov 20, 2024
1 parent 55b0453 commit a9f672c
Showing 1 changed file with 112 additions and 14 deletions.
126 changes: 112 additions & 14 deletions src/Processor/ProductAttribute/AssetProductAttributeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public function process(
}
}

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function handleAsset(
ProductInterface $model,
string $assetFamilyCode,
Expand All @@ -91,7 +94,7 @@ private function handleAsset(
array $queryParam,
array $assetAttributeResource,
): void {
if (!\method_exists($model, 'addAsset')) {
if (!\method_exists($model, 'addAsset') || !\method_exists($model, 'getAssets')) {
return;
}

Expand All @@ -100,18 +103,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,
Expand All @@ -129,28 +120,135 @@ 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);
$this->addAssetToProduct(
$model,
$asset,
$assetFamilyCode,
$attributeCode,
$assetCode,
$queryParam,
$data,
);

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(),
'familyCode' => $assetFamilyCode,
'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,
'scope' => $queryParam['scope'],
'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,
]);
}
}

0 comments on commit a9f672c

Please sign in to comment.