custom/plugins/WbfkITScope/src/Subscriber/ProductSavedSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\ITScope\Subscriber;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Content\Product\ProductDefinition;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ProductSavedSubscriber implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             EntityWrittenContainerEvent::class => 'onProductWritten',
  17.         ];
  18.     }
  19.     public function __construct(
  20.         protected Connection $connection,
  21.         protected EntityRepository $wbfkItScopeProductExtRepo
  22.     )
  23.     {
  24.     }
  25.     public function onProductWritten(EntityWrittenContainerEvent $event): void
  26.     {
  27.         if (!$event $event->getEventByEntityName(ProductDefinition::ENTITY_NAME)) {
  28.             return;
  29.         }
  30.         if (!$this->hasProductWbfkItScopeEntityExtention($event->getWriteResults())) {
  31.             $this->createWbfkItScopeEntityExtension($event->getWriteResults());
  32.         }
  33.     }
  34.     protected function hasProductWbfkItScopeEntityExtention(array $writeResults): bool
  35.     {
  36.         return $this->executeFunctionForProductWriteResult($writeResults, function ($writeResult) {
  37.             $stmt $this->connection->prepare('SELECT id FROM wbfk_it_scope_product_extension WHERE product_id = UNHEX(?)');
  38.             $stmt->executeQuery([$writeResult->getPrimaryKey()]);
  39.             return $stmt->rowCount() > 0;
  40.         });
  41.     }
  42.     protected function createWbfkItScopeEntityExtension(array $writeResults): void
  43.     {
  44.         $this->executeFunctionForProductWriteResult($writeResults, function ($writeResult) {
  45.             $payload $writeResult->getPayload();
  46.             if (array_key_exists('id'$payload) && array_key_exists('versionId'$payload)) {
  47.                 $this->wbfkItScopeProductExtRepo->create([
  48.                     [
  49.                         'productId' => $payload['id'],
  50.                         'productVersionId' => $payload['versionId'],
  51.                     ]
  52.                 ], Context::createDefaultContext());
  53.                 return true;
  54.             }
  55.             return false;
  56.         });
  57.     }
  58.     protected function executeFunctionForProductWriteResult(array $writeResults, callable $callback): bool
  59.     {
  60.         foreach ($writeResults as $writeResult) {
  61.             if (
  62.                 $writeResult->getEntityName() === ProductDefinition::ENTITY_NAME &&
  63.                 ($writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT || $writeResult->getOperation() === EntityWriteResult::OPERATION_UPDATE)) {
  64.                 return $callback($writeResult);
  65.             }
  66.         }
  67.         return false;
  68.     }
  69. }