custom/plugins/PickwareErpStarter/src/Product/PickwareProductInitializer.php line 38

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\PickwareErpStarter\Product;
  11. use Doctrine\DBAL\Connection;
  12. use Pickware\DalBundle\Sql\SqlUuid;
  13. use Shopware\Core\Content\Product\ProductEvents;
  14. use Shopware\Core\Defaults;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class PickwareProductInitializer implements EventSubscriberInterface
  19. {
  20.     private Connection $db;
  21.     public function __construct(Connection $db)
  22.     {
  23.         $this->db $db;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             ProductEvents::PRODUCT_WRITTEN_EVENT => 'productWritten',
  29.         ];
  30.     }
  31.     public function productWritten(EntityWrittenEvent $entityWrittenEvent): void
  32.     {
  33.         if ($entityWrittenEvent->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  34.             return;
  35.         }
  36.         $productIds = [];
  37.         foreach ($entityWrittenEvent->getWriteResults() as $writeResult) {
  38.             // $writeResult->getExistence() can be null, but we have no idea why and also not what this means.
  39.             $existence $writeResult->getExistence();
  40.             if (($existence === null && $writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT)
  41.                 || ($existence !== null && !$existence->exists())
  42.             ) {
  43.                 $productIds[] = $writeResult->getPrimaryKey();
  44.             }
  45.         }
  46.         $this->ensurePickwareProductsExist($productIds);
  47.     }
  48.     public function ensurePickwareProductsExist(array $productIds): void
  49.     {
  50.         if (count($productIds) === 0) {
  51.             return;
  52.         }
  53.         $this->db->executeStatement(
  54.             'INSERT INTO `pickware_erp_pickware_product` (
  55.                 id,
  56.                 product_id,
  57.                 product_version_id,
  58.                 incoming_stock,
  59.                 created_at
  60.             ) SELECT
  61.                 ' SqlUuid::UUID_V4_GENERATION ',
  62.                 product.id,
  63.                 product.version_id,
  64.                 0,
  65.                 NOW(3)
  66.             FROM `product`
  67.             WHERE `product`.`id` IN (:ids) AND `product`.`version_id` = :liveVersionId
  68.             ON DUPLICATE KEY UPDATE `pickware_erp_pickware_product`.`id` = `pickware_erp_pickware_product`.`id`',
  69.             [
  70.                 'ids' => array_map('hex2bin'$productIds),
  71.                 'liveVersionId' => hex2bin(Defaults::LIVE_VERSION),
  72.             ],
  73.             [
  74.                 'ids' => Connection::PARAM_STR_ARRAY,
  75.             ],
  76.         );
  77.     }
  78. }