<?php
declare(strict_types=1);
namespace NetInventors\NetiNextStoreLocator\Subscriber;
use Shopware\Core\Content\ImportExport\Event\ImportExportAfterImportRecordEvent;
use Shopware\Core\Content\ImportExport\Event\ImportExportBeforeImportRecordEvent;
use Shopware\Core\Content\ImportExport\Event\ImportExportExceptionImportRecordEvent;
use Shopware\Core\Framework\Context;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ImportExportSubscriber implements EventSubscriberInterface
{
public const STATE_IMPORT = 'neti_sl_import';
public static function getSubscribedEvents(): array
{
return [
ImportExportBeforeImportRecordEvent::class => 'onBeforeImport',
ImportExportAfterImportRecordEvent::class => 'onAfterImport',
ImportExportExceptionImportRecordEvent::class => 'onExceptionImport',
];
}
public function onBeforeImport(ImportExportBeforeImportRecordEvent $event): void
{
$this->setState($event->getContext());
}
public function onAfterImport(ImportExportAfterImportRecordEvent $event): void
{
$this->removeState($event->getContext());
}
public function onExceptionImport(ImportExportExceptionImportRecordEvent $event): void
{
$this->removeState($event->getContext());
}
protected function setState(Context $context): void
{
$context->addState(self::STATE_IMPORT);
}
protected function removeState(Context $context): void
{
if ($context->hasState(self::STATE_IMPORT)) {
$context->removeState(self::STATE_IMPORT);
}
}
}