<?php declare(strict_types=1);/** * digitvision * * @category digitvision * @package Shopware\Plugins\DvsnBundle * @copyright (c) 2021 digitvision */namespace Dvsn\Bundle\Subscriber\Core\Content\Bundle;use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;use Shopware\Core\Framework\DataAbstractionLayer\Write\EntityExistence;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RequestStack;class BundleWrittenSubscriber implements EventSubscriberInterface{ /** * ... * * @var EntityRepositoryInterface */ protected $bundleRepository; /** * ... * * @var EntityRepositoryInterface */ protected $bundleProductRepository; /** * ... * * @var RequestStack */ protected $requestStack; /** * ... * * @param EntityRepositoryInterface $bundleRepository * @param EntityRepositoryInterface $bundleProductRepository * @param RequestStack $requestStack */ public function __construct( EntityRepositoryInterface $bundleRepository, EntityRepositoryInterface $bundleProductRepository, RequestStack $requestStack ) { // set params $this->bundleRepository = $bundleRepository; $this->bundleProductRepository = $bundleProductRepository; $this->requestStack = $requestStack; } /** * {@inheritDoc} */ public static function getSubscribedEvents(): array { return [ 'dvsn_bundle.written' => 'onBundleWritten', 'dvsn_bundle.deleted' => 'onBundleDeleted', 'dvsn_bundle_product.written' => 'onBundleProductWritten', 'dvsn_bundle_product.deleted' => 'onBundleProductDeleted', ]; } /** * ... * * @param EntityWrittenEvent $event */ public function onBundleWritten(EntityWrittenEvent $event) { } /** * ... * * @param EntityWrittenEvent $event */ public function onBundleDeleted(EntityWrittenEvent $event) { } /** * ... * * @param EntityWrittenEvent $event */ public function onBundleProductWritten(EntityWrittenEvent $event) { // loop every entity foreach ($event->getWriteResults() as $writeResult) { // has to be valid if (!$writeResult->getExistence() instanceof EntityExistence) { // ignore continue; } // switch if we created or updated an answer switch ($writeResult->getOperation()) { case EntityWriteResult::OPERATION_UPDATE: // get the payload $payload = $writeResult->getPayload(); // get the request $request = $this->requestStack->getCurrentRequest(); // do we even have valid products in the request? if (!$request->request->has('products')) { // we dont break; } // get them $products = (array) $request->request->get('products'); // the request params for our product $params = null; // find the request for our product foreach ($products as $product) { if ($product['id'] === $payload['id']) { $params = $product; break; } } // not found? if (!is_array($params)) { // weird... break; } // if we set bundle id to null, we want to delete our product $hasBundleId = false; $bundleId = null; // find the id foreach ($params as $key => $value) { if ($key === 'bundleId') { $hasBundleId = true; $bundleId = $value; } } // are we removing the bundle id?! this means we want to remove the product if ($hasBundleId === true && $bundleId === null) { try { // delete it $this->bundleProductRepository->delete([ ['id' => $payload['id']] ], $event->getContext()); } catch (\Exception $exception) {} } // done break; } } } /** * ... * * @param EntityWrittenEvent $event */ public function onBundleProductDeleted(EntityWrittenEvent $event) { }}