custom/plugins/PevrPayeverIntegration/src/EventListener/ProductSaveEventListener.php line 95

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\ProductEvents;
  39. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  40. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  41. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
  42. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  43. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  44. class ProductSaveEventListener extends AbstractProductEventListener implements EventSubscriberInterface
  45. {
  46.     /**
  47.      * @var int[]
  48.      */
  49.     private $affectedProductStocks = [];
  50.     /**
  51.      * {@inheritDoc}
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             PreWriteValidationEvent::class => 'preValidate',
  57.             ProductEvents::PRODUCT_WRITTEN_EVENT => 'onProductSave',
  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.                 $payload $writeCommand->getPayload();
  73.                 if ($writeCommand instanceof UpdateCommand) {
  74.                     if (array_key_exists('stock'$payload)) {
  75.                         $product $this->getUncachedProduct($productId);
  76.                         if ($product) {
  77.                             $this->affectedProductStocks[$productId] = $product->getStock();
  78.                         }
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.     }
  84.     /**
  85.      * @param EntityWrittenEvent $event
  86.      */
  87.     public function onProductSave(EntityWrittenEvent $event): void
  88.     {
  89.         if (!$this->shouldProcessEvent($event)) {
  90.             $this->affectedProductStocks = [];
  91.             return;
  92.         }
  93.         $writeResults $event->getWriteResults();
  94.         $writeResult $writeResults[0];
  95.         $isNew $writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT;
  96.         $existence $writeResult->getExistence();
  97.         $primaryKey $existence->getPrimaryKey();
  98.         $productId $primaryKey['id'] ?? null;
  99.         $product $this->getUncachedProduct($productId);
  100.         if ($product) {
  101.             $parentId $product->getParentId();
  102.             $parentProduct $parentId $this->getUncachedProduct($parentId) : null;
  103.             $parentProduct
  104.                 $this->synchronizationManager->handleProductSave($parentProduct)
  105.                 : $this->synchronizationManager->handleProductSave($product$isNew);
  106.             $delta null;
  107.             if (array_key_exists($productId$this->affectedProductStocks)) {
  108.                 $stockOrigin $this->affectedProductStocks[$productId];
  109.                 $delta $product->getStock() - $stockOrigin;
  110.             }
  111.             if ($isNew || $delta) {
  112.                 $this->synchronizationManager->handleInventory($product$delta);
  113.             }
  114.         }
  115.         $this->affectedProductStocks = [];
  116.     }
  117.     /**
  118.      * @param EntityWrittenEvent $event
  119.      * @return bool
  120.      */
  121.     private function shouldProcessEvent(EntityWrittenEvent $event): bool
  122.     {
  123.         $result false;
  124.         if ($this->synchronizationManager->isProductsSyncEnabled() && !$event->getErrors()) {
  125.             $writeResults $event->getWriteResults();
  126.             if (!empty($writeResults[0])) {
  127.                 $writeResult $writeResults[0];
  128.                 $operations = [EntityWriteResult::OPERATION_INSERTEntityWriteResult::OPERATION_UPDATE];
  129.                 $result in_array($writeResult->getOperation(), $operations) && (bool) $writeResult->getExistence();
  130.             }
  131.         }
  132.         return $result;
  133.     }
  134.     /**
  135.      * @param array $affectedProductStocks
  136.      * @internal
  137.      */
  138.     public function setAffectedProductStocks(array $affectedProductStocks): void
  139.     {
  140.         $this->affectedProductStocks $affectedProductStocks;
  141.     }
  142. }