<?php declare(strict_types=1);
namespace NewsletterSendinblue\Subscriber;
use Shopware\Core\Content\Category\Aggregate\CategoryTranslation\CategoryTranslationDefinition;
use Shopware\Core\Content\Category\CategoryDefinition;
use Shopware\Core\Content\Product\Aggregate\ProductTranslation\ProductTranslationDefinition;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\ChangeSetAware;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EntitySubscriber implements EventSubscriberInterface
{
/**
* @return string[]
*/
public static function getSubscribedEvents()
{
return [
PreWriteValidationEvent::class => 'triggerChangeSet',
];
}
/**
* @param PreWriteValidationEvent $event
* @return void
*/
public function triggerChangeSet(PreWriteValidationEvent $event): void
{
if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
return;
}
foreach ($event->getCommands() as $command) {
if (!$command instanceof ChangeSetAware) {
continue;
}
if (($command->getDefinition()->getEntityName() === ProductDefinition::ENTITY_NAME
|| $command->getDefinition()->getEntityName() === ProductTranslationDefinition::ENTITY_NAME
|| $command->getDefinition()->getEntityName() === CategoryDefinition::ENTITY_NAME
|| $command->getDefinition()->getEntityName() === CategoryTranslationDefinition::ENTITY_NAME)
&& $command instanceof UpdateCommand
) {
$command->requestChangeSet();
}
}
}
}