custom/plugins/PickwareErpStarter/src/Supplier/ProductSupplierConfigurationInitializer.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\Supplier;
  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 ProductSupplierConfigurationInitializer 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.             if ($writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT) {
  39.                 $productIds[] = $writeResult->getPrimaryKey();
  40.             }
  41.         }
  42.         $this->ensureProductSupplierConfigurationExistsForProducts($productIds);
  43.     }
  44.     public function ensureProductSupplierConfigurationExistsForProducts(array $productIds): void
  45.     {
  46.         if (count($productIds) === 0) {
  47.             return;
  48.         }
  49.         $this->db->executeStatement(
  50.             'INSERT INTO `pickware_erp_product_supplier_configuration` (
  51.                 `id`,
  52.                 `product_id`,
  53.                 `product_version_id`,
  54.                 `created_at`
  55.             ) SELECT
  56.                 ' SqlUuid::UUID_V4_GENERATION ',
  57.                 `id`,
  58.                 `version_id`,
  59.                 NOW(3)
  60.             FROM `product`
  61.             WHERE `id` IN (:productIds) AND `version_id` = :liveVersionId
  62.             ON DUPLICATE KEY UPDATE `pickware_erp_product_supplier_configuration`.`id` = `pickware_erp_product_supplier_configuration`.`id`',
  63.             [
  64.                 'productIds' => array_map('hex2bin'$productIds),
  65.                 'liveVersionId' => hex2bin(Defaults::LIVE_VERSION),
  66.             ],
  67.             [
  68.                 'productIds' => Connection::PARAM_STR_ARRAY,
  69.             ],
  70.         );
  71.     }
  72.     public function ensureProductSupplierConfigurationExistsForAllProducts(): void
  73.     {
  74.         $this->db->executeStatement(
  75.             'INSERT INTO `pickware_erp_product_supplier_configuration` (
  76.                 `id`,
  77.                 `product_id`,
  78.                 `product_version_id`,
  79.                 `created_at`
  80.             ) SELECT
  81.                 ' SqlUuid::UUID_V4_GENERATION ',
  82.                 `id`,
  83.                 `version_id`,
  84.                 NOW(3)
  85.             FROM `product`
  86.             WHERE `version_id` = :liveVersionId
  87.             ON DUPLICATE KEY UPDATE `pickware_erp_product_supplier_configuration`.`id` = `pickware_erp_product_supplier_configuration`.`id`',
  88.             ['liveVersionId' => hex2bin(Defaults::LIVE_VERSION)],
  89.         );
  90.     }
  91. }