<?php
declare(strict_types=1);
namespace Iwv\IwvDatevV6\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
use Iwv\IwvDatevV6\Core\Content\IwvDatev\ScheduledExportEvents;
use Iwv\IwvDatevV6\Core\Content\IwvDatev\ScheduledExportDefinition;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use League\Flysystem\FilesystemInterface;
class ScheduledExportSubscriber implements EventSubscriberInterface
{
/** @var FilesystemInterface $filesystemPrivate */
private $filesystemPrivate;
/**
* @param FilesystemInterface $filesystemPrivate
*/
public function __construct(FilesystemInterface $filesystemPrivate)
{
$this->filesystemPrivate = $filesystemPrivate;
}
public static function getSubscribedEvents(): array
{
return [
PreWriteValidationEvent::class => 'onPreWriteValidationEvent',
ScheduledExportEvents::ENTITY_DELETED_EVENT => 'onEntityDeleted',
];
}
public function onPreWriteValidationEvent(PreWriteValidationEvent $event): void
{
foreach ($event->getCommands() as $command) {
if ($command->getDefinition()->getEntityName() !== ScheduledExportDefinition::ENTITY_NAME) {
continue;
}
if (!$command instanceof DeleteCommand) {
continue;
}
$command->requestChangeSet();
}
}
public function onEntityDeleted(EntityDeletedEvent $event): void
{
foreach ($event->getIds() as $id) {
if ($this->filesystemPrivate->has($id)) {
$this->filesystemPrivate->delete($id);
}
}
}
}