<?php declare(strict_types=1);
namespace GlobusSW6\Subscriber\TextilData;
use GlobusSW6\Service\TextilApi\TextilApiService;
use Psr\Log\LoggerInterface;
use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\Adapter\Translation\Translator;
use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use GlobusSW6\Service\IaneoBusinessExtension;
class ProductTextilDataExtensionSubscriber implements EventSubscriberInterface
{
/** @var LoggerInterface */
private $logger;
/** @var IaneoBusinessExtension */
private $ianeoBusinessExtension;
/** @var TextilApiService */
private $textilApiService;
/** @var Translator */
private $translator;
/**
* ProductTextilDataExtensionSubscriber constructor.
* @param LoggerInterface $logger
* @param IaneoBusinessExtension $ianeoBusinessExtension
* @param TextilApiService $textilApiService
* @param Translator $translator
*/
public function __construct(LoggerInterface $logger, IaneoBusinessExtension $ianeoBusinessExtension, TextilApiService $textilApiService,Translator $translator)
{
$this->logger = $logger;
$this->ianeoBusinessExtension = $ianeoBusinessExtension;
$this->textilApiService = $textilApiService;
$this->translator = $translator;
}
public static function getSubscribedEvents(): array
{
return [
'product.search.result.loaded' => 'onProductLoaded'
];
}
public function onProductLoaded(EntitySearchResultLoadedEvent $event)
{
/** @var ProductCollection $products */
$products = $event->getResult()->getEntities();
if ($products === null) {
return;
}
if ($event->getContext()->getSource() instanceOf SalesChannelApiSource) {
try {
$salesChannelId = $event->getContext()->getSource()->getSalesChannelId();
$languageId = '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
$this->translator->injectSettings(
$salesChannelId,
$languageId,
'de-DE',// TODO-NGS: currently hardcoded
$event->getContext()
);
} catch (\Throwable $t) {
$this->logger->info('Unable to translate snippets for api calls using the following params: saleschannelId: ' . $salesChannelId . ' ; languageId: ' . $languageId . ' ; context: ' . json_encode($event->getContext())
);
}
$hinweisAnimalParts = $this->translator->trans("textil.animalParts");
$containsAnimalParts = false;
foreach ($products as $product) {
/** @var SalesChannelProductEntity $product */
try {
$textildata = $this->textilApiService->getTextilByProductId($product->getId(), $event->getContext());
if ($textildata) {
foreach ($textildata->getProductCompositionData() as $component)
{
if($component['other_animal_parts'] === '1')
{
$containsAnimalParts = true;
}
}
if ($containsAnimalParts){
$textildata->setHinweisContainsAnimalPartsFlag($containsAnimalParts);
$textildata->setHinweisContainsAnimalParts($hinweisAnimalParts);
}
$this->ianeoBusinessExtension->addSingleAttributeToProduct($product, $textildata, 'textilData');
}
} catch (\Exception $exception) {
$this->logger->error(__CLASS__ . ":" . __FUNCTION__ . ":" . __LINE__ . ":\$exception->getMessage(): " . $exception->getMessage());
}
}
}
}
}