custom/plugins/DvsnBundle/src/Subscriber/Core/Content/Bundle/BundleWrittenSubscriber.php line 97

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * digitvision
  4.  *
  5.  * @category  digitvision
  6.  * @package   Shopware\Plugins\DvsnBundle
  7.  * @copyright (c) 2021 digitvision
  8.  */
  9. namespace Dvsn\Bundle\Subscriber\Core\Content\Bundle;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Write\EntityExistence;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. class BundleWrittenSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * ...
  20.      *
  21.      * @var EntityRepositoryInterface
  22.      */
  23.     protected $bundleRepository;
  24.     /**
  25.      * ...
  26.      *
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     protected $bundleProductRepository;
  30.     /**
  31.      * ...
  32.      *
  33.      * @var RequestStack
  34.      */
  35.     protected $requestStack;
  36.     /**
  37.      * ...
  38.      *
  39.      * @param EntityRepositoryInterface $bundleRepository
  40.      * @param EntityRepositoryInterface $bundleProductRepository
  41.      * @param RequestStack $requestStack
  42.      */
  43.     public function __construct(
  44.         EntityRepositoryInterface $bundleRepository,
  45.         EntityRepositoryInterface $bundleProductRepository,
  46.         RequestStack $requestStack
  47.     ) {
  48.         // set params
  49.         $this->bundleRepository $bundleRepository;
  50.         $this->bundleProductRepository $bundleProductRepository;
  51.         $this->requestStack $requestStack;
  52.     }
  53.     /**
  54.      * {@inheritDoc}
  55.      */
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             'dvsn_bundle.written' => 'onBundleWritten',
  60.             'dvsn_bundle.deleted' => 'onBundleDeleted',
  61.             'dvsn_bundle_product.written' => 'onBundleProductWritten',
  62.             'dvsn_bundle_product.deleted' => 'onBundleProductDeleted',
  63.         ];
  64.     }
  65.     /**
  66.      * ...
  67.      *
  68.      * @param EntityWrittenEvent $event
  69.      */
  70.     public function onBundleWritten(EntityWrittenEvent $event)
  71.     {
  72.     }
  73.     /**
  74.      * ...
  75.      *
  76.      * @param EntityWrittenEvent $event
  77.      */
  78.     public function onBundleDeleted(EntityWrittenEvent $event)
  79.     {
  80.     }
  81.     /**
  82.      * ...
  83.      *
  84.      * @param EntityWrittenEvent $event
  85.      */
  86.     public function onBundleProductWritten(EntityWrittenEvent $event)
  87.     {
  88.         // loop every entity
  89.         foreach ($event->getWriteResults() as $writeResult) {
  90.             // has to be valid
  91.             if (!$writeResult->getExistence() instanceof EntityExistence) {
  92.                 // ignore
  93.                 continue;
  94.             }
  95.             // switch if we created or updated an answer
  96.             switch ($writeResult->getOperation()) {
  97.                 case EntityWriteResult::OPERATION_UPDATE:
  98.                     // get the payload
  99.                     $payload $writeResult->getPayload();
  100.                     // get the request
  101.                     $request $this->requestStack->getCurrentRequest();
  102.                     // do we even have valid products in the request?
  103.                     if (!$request->request->has('products')) {
  104.                         // we dont
  105.                         break;
  106.                     }
  107.                     // get them
  108.                     $products = (array) $request->request->get('products');
  109.                     // the request params for our product
  110.                     $params null;
  111.                     // find the request for our product
  112.                     foreach ($products as $product) {
  113.                         if ($product['id'] === $payload['id']) {
  114.                             $params $product;
  115.                             break;
  116.                         }
  117.                     }
  118.                     // not found?
  119.                     if (!is_array($params)) {
  120.                         // weird...
  121.                         break;
  122.                     }
  123.                     // if we set bundle id to null, we want to delete our product
  124.                     $hasBundleId false;
  125.                     $bundleId null;
  126.                     // find the id
  127.                     foreach ($params as $key => $value) {
  128.                         if ($key === 'bundleId') {
  129.                             $hasBundleId true;
  130.                             $bundleId $value;
  131.                         }
  132.                     }
  133.                     // are we removing the bundle id?! this means we want to remove the product
  134.                     if ($hasBundleId === true && $bundleId === null) {
  135.                         try {
  136.                             // delete it
  137.                             $this->bundleProductRepository->delete([
  138.                                 ['id' => $payload['id']]
  139.                             ], $event->getContext());
  140.                         } catch (\Exception $exception) {}
  141.                     }
  142.                     // done
  143.                     break;
  144.             }
  145.         }
  146.     }
  147.     /**
  148.      * ...
  149.      *
  150.      * @param EntityWrittenEvent $event
  151.      */
  152.     public function onBundleProductDeleted(EntityWrittenEvent $event)
  153.     {
  154.     }
  155. }