custom/plugins/PevrPayeverIntegration/src/EventListener/ProductDeleteEventListener.php line 92

Open in your IDE?
  1. <?php
  2. /**
  3.  * payever GmbH
  4.  *
  5.  * NOTICE OF LICENSE
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in all
  15.  * copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.  * SOFTWARE.
  24.  *
  25.  * DISCLAIMER
  26.  *
  27.  * Do not edit or add to this file if you wish to upgrade payever Shopware package
  28.  * to newer versions in the future.
  29.  *
  30.  * @category    Payever
  31.  * @author      payever GmbH <service@payever.de>
  32.  * @copyright   Copyright (c) 2021 payever GmbH (http://www.payever.de)
  33.  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  34.  */
  35. declare(strict_types=1);
  36. namespace Payever\PayeverPayments\EventListener;
  37. use Shopware\Core\Content\Product\ProductDefinition;
  38. use Shopware\Core\Content\Product\ProductEntity;
  39. use Shopware\Core\Content\Product\ProductEvents;
  40. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  41. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  42. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  43. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  44. class ProductDeleteEventListener extends AbstractProductEventListener implements EventSubscriberInterface
  45. {
  46.     /**
  47.      * @var ProductEntity[]
  48.      */
  49.     private $productsToRemove = [];
  50.     /**
  51.      * {@inheritDoc}
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             PreWriteValidationEvent::class => 'preValidate',
  57.             ProductEvents::PRODUCT_DELETED_EVENT => 'onProductDelete',
  58.         ];
  59.     }
  60.     /**
  61.      * @param PreWriteValidationEvent $event
  62.      */
  63.     public function preValidate(PreWriteValidationEvent $event): void
  64.     {
  65.         if (!$this->synchronizationManager->isProductsSyncEnabled()) {
  66.             return;
  67.         }
  68.         foreach ($event->getCommands() as $writeCommand) {
  69.             if ($writeCommand->getDefinition()->getClass() === ProductDefinition::class) {
  70.                 $primaryKey $writeCommand->getEntityExistence()->getPrimaryKey();
  71.                 $productId $primaryKey['id'] ?? null;
  72.                 if ($writeCommand instanceof DeleteCommand) {
  73.                     $product $this->getUncachedProduct($productId);
  74.                     if ($product) {
  75.                         $this->productsToRemove[$productId] = $product;
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     /**
  82.      * @param EntityDeletedEvent $event
  83.      */
  84.     public function onProductDelete(EntityDeletedEvent $event): void
  85.     {
  86.         if (!$this->synchronizationManager->isProductsSyncEnabled()) {
  87.             $this->productsToRemove = [];
  88.             return;
  89.         }
  90.         if (!$event->getErrors()) {
  91.             $writeResults $event->getWriteResults();
  92.             if (!empty($writeResults[0])) {
  93.                 $writeResult $writeResults[0];
  94.                 $existence $writeResult->getExistence();
  95.                 if ($existence) {
  96.                     $primaryKey $existence->getPrimaryKey();
  97.                     $productId $primaryKey['id'] ?? null;
  98.                     if (!empty($this->productsToRemove[$productId])) {
  99.                         $productToRemove $this->productsToRemove[$productId];
  100.                         $parentId $productToRemove->getParentId();
  101.                         $parentProduct $parentId $this->getUncachedProduct($parentId) : null;
  102.                         $parentProduct
  103.                             $this->synchronizationManager->handleProductSave($parentProduct)
  104.                             : $this->synchronizationManager->handleProductDelete($productToRemove);
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.         $this->productsToRemove = [];
  110.     }
  111.     /**
  112.      * @internal
  113.      * @param array $productsToRemove
  114.      */
  115.     public function setProductsToRemove(array $productsToRemove): void
  116.     {
  117.         $this->productsToRemove $productsToRemove;
  118.     }
  119. }