<?php declare(strict_types=1);
namespace EnnoDigitalProducts\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use EnnoDigitalProducts\EnnoDownload\Aggregate\EnnoDownloadSetProductRelations\EnnoDownloadSetProductRelationsEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Context;
class EnnoDownloadSetProductRelationsWritten implements EventSubscriberInterface
{
private $requestStack;
private $ennoDigitalProductSettingsRepository;
public function __construct(EntityRepositoryInterface $ennoDigitalProductSettingsRepository)
{
$this->ennoDigitalProductSettingsRepository = $ennoDigitalProductSettingsRepository;
}
public static function getSubscribedEvents()
{
return [
EnnoDownloadSetProductRelationsEvents::ENNO_DOWNLOAD_SET_PRODUCT_RELATIONS_WRITTEN_EVENT => 'onEnnoDownloadSetProductRelationWritten'
];
}
public function onEnnoDownloadSetProductRelationWritten(EntityWrittenEvent $event)
{
$errors = $event->getErrors();
if(is_array($errors) && !empty($errors)) return;
$transactions = $event->getPayloads();
if(!(is_array($transactions) && !empty($transactions))) return;
$context = Context::createDefaultContext();
foreach ($transactions as $transaction)
{
$this->ennoDigitalProductSettingsRepository->upsert(
[
['productId' => $transaction['productId'], 'downloadsInheritanceId' => $transaction['productId']]
],
$context
);
}
}
}