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 b7d680f
Showing 1 changed file with 101 additions and 14 deletions.
115 changes: 101 additions & 14 deletions src/Processor/ProductAttribute/AssetProductAttributeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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,
Expand All @@ -129,28 +117,127 @@ 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(),
'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 b7d680f

Please sign in to comment.