vendor/store.shopware.com/netinexteasycoupon/src/Subscriber/EntitySubscriber.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCoupon\Subscriber;
  4. use Doctrine\DBAL\Connection;
  5. use Monolog\Logger;
  6. use NetInventors\NetiNextEasyCoupon\Core\Content\EasyCoupon\EasyCouponDefinition;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. use Shopware\Core\System\NumberRange\ValueGenerator\NumberRangeValueGenerator;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class EntitySubscriber implements EventSubscriberInterface
  12. {
  13.     private NumberRangeValueGenerator $numberRangeValueGenerator;
  14.     private Connection                $connection;
  15.     private Logger                    $logger;
  16.     public function __construct(
  17.         NumberRangeValueGenerator $numberRangeValueGenerator,
  18.         Connection                $connection,
  19.         Logger                    $logger
  20.     ) {
  21.         $this->numberRangeValueGenerator $numberRangeValueGenerator;
  22.         $this->connection                $connection;
  23.         $this->logger                    $logger;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             EasyCouponDefinition::ENTITY_NAME '.written' => 'onEasyCouponWritten',
  29.         ];
  30.     }
  31.     public function onEasyCouponWritten(EntityWrittenEvent $event): void
  32.     {
  33.         foreach ($event->getWriteResults() as $writeResult) {
  34.             if (
  35.                 $writeResult->getEntityName() !== EasyCouponDefinition::ENTITY_NAME
  36.                 || $writeResult->getOperation() !== 'insert'
  37.             ) {
  38.                 continue;
  39.             }
  40.             /**
  41.              * @var string $id This is okay because the entity has only one primary key
  42.              */
  43.             $id $writeResult->getPrimaryKey();
  44.             try {
  45.                 $number $this->numberRangeValueGenerator->getValue(
  46.                     EasyCouponDefinition::ENTITY_NAME,
  47.                     $event->getContext(),
  48.                     null
  49.                 );
  50.                 $sql '
  51.                 UPDATE neti_easy_coupon
  52.                 SET number = ?
  53.                 WHERE id = ?
  54.             ';
  55.                 $this->connection->executeStatement($sql, [
  56.                     $number,
  57.                     Uuid::fromHexToBytes($id),
  58.                 ]);
  59.             } catch (\Exception $ex) {
  60.                 $this->logger->warning(
  61.                     'Could not generate number for EasyCoupon',
  62.                     [
  63.                         'error' => $ex->getMessage(),
  64.                         'easyCouponId' => $id,
  65.                     ]
  66.                 );
  67.             }
  68.         }
  69.     }
  70. }